博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
与孩子一起学编程10章
阅读量:6292 次
发布时间:2019-06-22

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

这一次,来个有意思的小游戏

滑雪游戏,树是障碍物,拿到旗子有奖励,运行文件需要几个图片

上传在这儿,滑雪者,旗子,树,还有滑雪者左方向的两个,第一个,第二个,有方向的两个,第一个,第二个,滑雪者碰撞树的图片。图片的名称和代码里要一致。

代码如下

# encoding=utf-8# 与孩子一起学编程10章# Skier 代码清单 10-1# 2013年8月31日10:08:25import 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 obstacles# 做好准备pygame.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.Font(None, 50)# 开始主循环while True:    clock.tick(30)  # 每秒更新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()

你也可以试试看,挺有意思的,哈哈,总于有个比较完整的演示代码了,后面的也要有点难度了,不过不要紧,慢慢学,一点一点的试着写一些代码,就会有些收获的。一起来吧,python的世界。

 

你可能感兴趣的文章
微信支付
查看>>
CodeBlocks中的OpenGL
查看>>
短址(short URL)
查看>>
第十三章 RememberMe——《跟我学Shiro》
查看>>
mysql 时间函数 时间戳转为日期
查看>>
索引失效 ORA-01502
查看>>
Oracle取月份,不带前面的0
查看>>
Linux Network Device Name issue
查看>>
IP地址的划分实例解答
查看>>
如何查看Linux命令源码
查看>>
运维基础命令
查看>>
入门到进阶React
查看>>
SVN 命令笔记
查看>>
检验手机号码
查看>>
重叠(Overlapped)IO模型
查看>>
Git使用教程
查看>>
使用shell脚本自动监控后台进程,并能自动重启
查看>>
Flex&Bison手册
查看>>
solrCloud+tomcat+zookeeper集群配置
查看>>
/etc/fstab,/etc/mtab,和 /proc/mounts
查看>>