fly bird小游戏
代码
1 import pygame 2 import sys 3 import random 4 # 素材参考地址:https://www.aigei.com/s?q=flappy+bird&type=2d 5 6 class Bird(object): 7 """定义一个鸟类""" 8 def __init__(self): 9 """定义初始化方法""" 10 self.birdRect = pygame.Rect(30,30,30,30) # 鸟的矩形 11 # 定义鸟的3种状态列表 12 bird = pygame.image.load("bird.png") 13 #bird2 = pygame.image.load("bird2.png") 14 #bird3 = pygame.image.load("bird3.png") 15 bird = pygame.transform.scale(bird, (35,35)) 16 #bird2 = pygame.transform.scale(bird2, (35,35)) 17 #bird3 = pygame.transform.scale(bird3, (35,35)) 18 self.birdStatus = [bird,bird,bird] 19 self.status = 0 # 默认飞行状态 20 self.birdX = 120 # 鸟所在X轴坐标 21 self.birdY = 350 # 鸟所在Y轴坐标,即上下飞行高度 22 self.jump = False # 默认情况小鸟自动降落 23 self.jumpSpeed = 10 # 跳跃高度 24 self.gravity = 1.5 # 重力 25 self.dead = False # 默认小鸟生命状态为活着 26 27 def birdUpdate(self): 28 if self.jump: 29 # 小鸟跳跃 30 self.jumpSpeed -= 1 # 速度递减,上升越来越慢 31 self.birdY -= self.jumpSpeed # 鸟Y轴坐标减小,小鸟上升 32 else: 33 # 小鸟坠落 34 self.gravity += 0.001 # 重力递增,下降越来越快 35 self.birdY += self.gravity # 鸟Y轴坐标增加,小鸟下降 36 self.birdRect[1] = self.birdY # 更改Y轴位置 37 38 class Pipeline(object): 39 """定义一个管道类""" 40 def __init__(self): 41 """定义初始化方法""" 42 self.wallx = 400 # 管道所在X轴坐标 43 self.pineUp = pygame.image.load("top guandao.png") # 加载上管道图片 44 self.pineDown = pygame.image.load("down guandao.png") # 加载下管道图片 45 46 def updatePipeline(self): 47 """管道水平移动方法""" 48 self.wallx -= 3 # 管道X轴坐标递减,即管道向左移动 49 # 当管道运行到一定位置,即小鸟飞越管道,分数加1,并且管道重置 50 if self.wallx < -80: 51 global score 52 score += 1 53 self.wallx = 400 54 55 def createMap(screen, background, font): 56 """定义创建地图的方法""" 57 screen.fill((255, 255, 255)) # 填充颜色 58 screen.blit(background, (0, 0)) # 填入到背景 59 60 # 显示管道 61 screen.blit(Pipeline.pineUp, (Pipeline.wallx, -100)) # 上管道坐标位置(X,Y) 62 screen.blit(Pipeline.pineDown, (Pipeline.wallx, 500)) # 下管道坐标位置(X,Y) 63 Pipeline.updatePipeline() # 管道移动 64 65 # 显示小鸟 66 if Bird.dead: # 撞管道状态 67 Bird.status = 2 68 elif Bird.jump: # 起飞状态 69 Bird.status = 1 70 screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY)) # 设置小鸟的坐标 71 Bird.birdUpdate() # 鸟移动 72 73 # 显示分数 74 screen.blit(font.render("score: " + str(score), -1, (255, 255, 255)), (230, 20)) # 设置颜色及坐标位置 75 76 pygame.display.update() # 更新显示 77 78 def checkDead(): 79 # 上方管子的矩形位置 80 upRect = pygame.Rect(Pipeline.wallx, -100, Pipeline.pineUp.get_width() - 10, Pipeline.pineUp.get_height()) 81 # 下方管子的矩形位置 82 downRect = pygame.Rect(Pipeline.wallx, 500, Pipeline.pineDown.get_width() - 10, Pipeline.pineDown.get_height()) 83 # 检测小鸟与上下方管子是否碰撞 84 if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect): 85 Bird.dead = True 86 return True 87 else: 88 return False 89 90 def getResult(): 91 final_text1 = "Game over" 92 final_text2 = "Your final score is: " + str(score) 93 ft1_font = pygame.font.SysFont("Arial", 70) # 设置第一行文字字体 94 ft1_surf = ft1_font.render(final_text1, 1, (242, 3, 36)) # 设置第一行文字的颜色 95 ft2_font = pygame.font.SysFont("Arial", 50) # 设置第二行文字字体 96 ft2_surf = ft2_font.render(final_text2, 1, (253, 177, 6)) # 设置第二行文字颜色 97 # 设置第一行文字显示位置 98 screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100]) 99 # 设置第二行文字显示位置 100 screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200]) 101 102 pygame.display.flip() # 更新整个待显示的Surface对象到屏幕上 103 104 if __name__ == "__main__": 105 """主程序""" 106 pygame.init() # 初始化pygame 107 pygame.font.init() # 初始化字体 108 font = pygame.font.SysFont(None, 50) # 设置默认字体和大小 109 size = width, height = 400, 680 # 设置窗口 110 screen = pygame.display.set_mode(size) # 显示窗口 111 clock = pygame.time.Clock() # 设置时钟 112 Pipeline = Pipeline() # 实例化管道类 113 Bird = Bird() # 实例化鸟类 114 score = 0 # 初始化分数 115 while True: 116 clock.tick(60) # 每秒执行60次 117 # 轮询事件 118 for event in pygame.event.get(): 119 if event.type == pygame.QUIT: 120 sys.exit() 121 if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead: 122 Bird.jump = True # 跳跃 123 Bird.gravity = 5 # 重力 124 Bird.jumpSpeed = 10 # 跳跃速度 125 background = pygame.image.load("background.png") # 加载背景图片 126 if checkDead(): # 检测小鸟生命状态 127 getResult() # 如果小鸟死亡,游戏结束,显示游戏总分数 128 else: 129 createMap(screen, background, font) # 绘制地图 130 pygame.quit() # 退出
运行图片

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号