Enly

导航

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

运行结果:

 

 

posted on 2023-12-27 09:43  Enly_321  阅读(5)  评论(0编辑  收藏  举报