博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【和孩子一起学编程】 python笔记--第三天
阅读量:6207 次
发布时间:2019-06-21

本文共 4654 字,大约阅读时间需要 15 分钟。

第十章

游戏时间:Skier

首先安装pygame,直接在cmd命令控制框里键入pip install pygame就可以了

代码:

import pygame, sys, randomskier_image = ["skier_down.png", "skier_right1.png", "skier_right2.png",                "skier_left2.png", "skier_left1.png"]class SkierClass(pygame.sprite.Sprite):    def __init__(self):        pygame.sprite.Sprite.__init__(self)        self.image = pygame.image.load("skier_down.png")        self.rect = self.image.get_rect()        self.rect.center = [320, 100]        self.angle = 0    def turn(self, direction):        self.angle = self.angle + direction        if self.angle < -2:  self.angle = -2        if self.angle >  2:  self.angle =  2        center = self.rect.center        self.image = pygame.image.load(skier_image[self.angle])        self.rect = self.image.get_rect()        self.rect.center = center        speed = [self.angle, 6 - abs(self.angle) * 2]        return speed    def move(self, speed):        self.rect.centerx = self.rect.centerx + speed[0]        if self.rect.centerx < 20:  self.rect.centerx = 20        if self.rect.centerx > 620: self.rect.centerx = 620class ObstacleClass(pygame.sprite.Sprite):    def __init__(self, image_file, location, type):        pygame.sprite.Sprite.__init__(self)        self.image_file = image_file        self.image = pygame.image.load(image_file)        self.location = location        self.rect = self.image.get_rect()        self.rect.center = location        self.type = type        self.passed = False    def scroll(self, t_ptr):        self.rect.centery = self.location[1] - t_ptrdef create_map(start, end):    obstacles = pygame.sprite.Group()    locations = []    gates = pygame.sprite.Group()    for i in range(10):        row = random.randint(start, end)        col = random.randint(0, 9)        location = [col * 64 + 20, row * 64 + 20]        if not (location in locations):            locations.append(location)            type = random.choice(["tree", "flag"])            if type == "tree": img = "skier_tree.png"            elif type == "flag":  img = "skier_flag.png"            obstacle = ObstacleClass(img, location, type)            obstacles.add(obstacle)    return obstaclesdef animate():    screen.fill([255, 255, 255])    pygame.display.update(obstacles.draw(screen))    screen.blit(skier.image, skier.rect)    screen.blit(score_text, [10, 10])    pygame.display.flip()def updateObstacleGroup(map0, map1):    obstacles = pygame.sprite.Group()    for ob in map0:  obstacles.add(ob)    for ob in map1:  obstacles.add(ob)    return obstaclespygame.init()screen = pygame.display.set_mode([640,640])clock = pygame.time.Clock()skier = SkierClass()speed = [0, 6]map_position = 0points = 0map0 = create_map(20, 29)map1 = create_map(10, 19)activeMap = 0obstacles = updateObstacleGroup(map0, map1)font = pygame.font.SysFont('arial', 50)while True:    clock.tick(30)      for event in pygame.event.get():        if event.type == pygame.QUIT: sys.exit()        if event.type == pygame.KEYDOWN:            if event.key == pygame.K_LEFT:                speed = skier.turn(-1)            elif event.key == pygame.K_RIGHT:                speed = skier.turn(1)    skier.move(speed)      map_position += speed[1]        if map_position >=640 and activeMap == 0:        activeMap = 1        map0 = create_map(20, 29)        obstacles = updateObstacleGroup(map0, map1)    if map_position >=1280 and activeMap == 1:        activeMap = 0        for ob in map0:            ob.location[1] = ob.location[1] - 1280        map_position = map_position -1280        map1 = create_map(10, 19)        obstacles = updateObstacleGroup(map0, map1)    for obstacle in obstacles:        obstacle.scroll(map_position)      hit = pygame.sprite.spritecollide(skier, obstacles, False)    if hit:        if hit[0].type == "tree" and not hit[0].passed:            points = points - 100            skier.image = pygame.image.load("skier_crash.png")            animate()            pygame.time.delay(1000)            skier.image = pygame.image.load("skier_down.png")            skier.angle = 0            speed = [0, 6]            hit[0].passed = True        elif hit[0].type == "flag" and not hit[0].passed:            points += 10            obstacles.remove(hit[0])    score_text = font.render("Score: " +str(points), 1, (0, 0, 0))    animate()

自己照着书上的代码敲了半天...

结果敲出来一堆的bug..

这就很气了,然后改了半天

对着程序看了又看。。字母好多敲错顺序了。。

最后还是没有把自己的程序调通,很难受

最后只能参考别人的程序了qaq

但发现程序都好像是2.7版的

好像一条控制字体显示的语句在3.6改了。。。如下

font = pygame.font.SysFont('arial', 50)#这是调好了的! #原文中它是这样的 :
font = pygame.font.Font(None, 50)

也百度找了好久。。还好找出来了。。不然就真的要崩溃了

参考博客:http://blog.sina.com.cn/s/blog_466af1830101e6d3.html

转载于:https://www.cnblogs.com/zhouya1/p/9168862.html

你可能感兴趣的文章
查看修改swap空间大小
查看>>
Django REST framework
查看>>
CSS 如何让Table的里面TD全有边框 而Table的右左边框没有
查看>>
如何让帝国CMS7.2搜索模板支持动态标签调用
查看>>
被吐嘈的NodeJS的异常处理
查看>>
apache 虚拟主机详细配置:http.conf配置详解
查看>>
BABOK - 开篇:业务分析知识体系介绍
查看>>
Java入门系列-22-IO流
查看>>
Template、ItemsPanel、ItemContainerStyle、ItemTemplate
查看>>
MySQL:Innodb page clean 线程 (二) :解析
查看>>
图嵌入综述 (arxiv 1709.07604) 译文五、六、七
查看>>
垃圾回收算法优缺点对比
查看>>
正则表达式 匹配常用手机号 (13、15\17\18开头的十一位手机号)
查看>>
GitLab 11.9 正式发布,自动化工具 ChatOps 已开源
查看>>
交换机的基本原理配置(一)
查看>>
android baidupush
查看>>
Lottie 站在巨人的肩膀上实现 Android 酷炫动画效果
查看>>
Linux_异常_08_本机无法访问虚拟机web等工程
查看>>
“陪护机器人”研报:距离真正“陪护”还差那么一点
查看>>
深入框架本源系列 —— Virtual Dom
查看>>