ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Pygame《Flappy Bird(像素飞鸟)》游戏

Pygame《Flappy Bird(像素飞鸟)》游戏 这段代码完整实现了 Flappy Bird 的几个核心底层物理机制小鸟的重力加速度下坠、按键瞬间向上跃升、管道阵列在右侧源源不断地随机高度生成、完美的矩形与掩码碰撞检测以及穿越管道时的实时计分。️ 运行前的准备工作请确保电脑里安装了 pygame 库bashpip install pygame请谨慎使用此类代码。 游戏核心源码 (flappy_bird.py)代码同样采用免图片素材设计完全用矢量色块动态渲染小鸟圆圆的像素萌鸟和管道。复制到 .py 文件后即可直接开玩pythonimport pygameimport sysimport random# 1. 初始化与全局配置 pygame.init()# 屏幕尺寸SCREEN_WIDTH 400SCREEN_HEIGHT 600screen pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))pygame.display.set_caption( 纯代码版冲吧像素飞鸟)# 颜色配置 (RGB)COLOR_SKY (113, 197, 207) # 经典原版天蓝色COLOR_BIRD (255, 200, 0) # 小鸟亮黄COLOR_BIRD_EYE (255, 255, 255)# 鸟眼白COLOR_PIPE (115, 191, 46) # 管道复古绿COLOR_TEXT (255, 255, 255) # 纯白文字# 游戏物理参数配置GRAVITY 0.45 # 重力加速度数值越大下坠越快FLAP_STRENGTH -7.5 # 每次振翅向上飞的瞬间初速度PIPE_SPEED 3.5 # 管道向左滚动的速度PIPE_GAP 140 # 上下管道之间的安全通过间隙数值越小越难PIPE_FREQUENCY 1400 # 每隔多少毫秒1.4秒生成一根新管道clock pygame.time.Clock()# 2. 游戏核心类定义 class Bird:小鸟物理实体类def __init__(self):self.x 80self.y SCREEN_HEIGHT // 2self.radius 16self.velocity 0def flap(self):翅膀煽动获得向上的瞬时速度self.velocity FLAP_STRENGTHdef update(self):受重力影响更新速度与位置self.velocity GRAVITYself.y self.velocity# 天花板与地板越界限制防止飞出宇宙if self.y self.radius:self.y self.radiusself.velocity 0def draw(self):# 绘制圆形小鸟身体pygame.draw.circle(screen, COLOR_BIRD, (int(self.x), int(self.y)), self.radius)# 绘制眼睛更具神采pygame.draw.circle(screen, COLOR_BIRD_EYE, (int(self.x 6), int(self.y - 4)), 5)pygame.draw.circle(screen, (0, 0, 0), (int(self.x 7), int(self.y - 4)), 2)# 绘制橘红色鸟嘴pygame.draw.polygon(screen, (255, 100, 0), [(self.x 12, self.y - 2),(self.x 22, self.y 2),(self.x 12, self.y 6)])class Pipe:障碍物管道类def __init__(self):self.width 65# 随机计算上方管道的底端高度位置self.top_height random.randint(50, SCREEN_HEIGHT - PIPE_GAP - 100)self.bottom_y self.top_height PIPE_GAPself.x SCREEN_WIDTHself.passed False # 标记小鸟是否已经成功穿越这根管道def move(self):向左滚动self.x - PIPE_SPEEDdef draw(self):# 1. 绘制上方管道top_rect pygame.Rect(self.x, 0, self.width, self.top_height)pygame.draw.rect(screen, COLOR_PIPE, top_rect)pygame.draw.rect(screen, (80, 140, 30), top_rect, 3) # 管道深色内边框# 2. 绘制下方管道bottom_rect pygame.Rect(self.x, self.bottom_y, self.width, SCREEN_HEIGHT - self.bottom_y)pygame.draw.rect(screen, COLOR_PIPE, bottom_rect)pygame.draw.rect(screen, (80, 140, 30), bottom_rect, 3)def collides_with(self, bird):精确矩形边界碰撞检测# 计算小鸟的矩形包围盒bird_rect pygame.Rect(bird.x - bird.radius 3, bird.y - bird.radius 3, bird.radius * 2 - 6, bird.radius * 2 - 6)top_rect pygame.Rect(self.x, 0, self.width, self.top_height)bottom_rect pygame.Rect(self.x, self.bottom_y, self.width, SCREEN_HEIGHT - self.bottom_y)if bird_rect.colliderect(top_rect) or bird_rect.colliderect(bottom_rect):return Truereturn False# 3. 主程序逻辑循环 def main_game():bird Bird()pipes []score 0game_state START # START, PLAYING, GAME_OVERfont pygame.font.SysFont(SimHei, 24, boldTrue)# 管道定时生成器事件SPAWN_PIPE_EVENT pygame.USEREVENT 1pygame.time.set_timer(SPAWN_PIPE_EVENT, PIPE_FREQUENCY)while True:# ---- A. 事件监听与操控 ----for event in pygame.event.get():if event.type pygame.QUIT:pygame.quit()sys.exit()if event.type SPAWN_PIPE_EVENT and game_state PLAYING:pipes.append(Pipe())if event.type pygame.KEYDOWN:if event.key pygame.K_ESCAPE:pygame.quit()sys.exit()if event.key pygame.K_SPACE:if game_state START:game_state PLAYINGbird.flap()elif game_state PLAYING:bird.flap()elif game_state GAME_OVER:main_game() # 重新初始化开局# ---- B. 物理状态更新 ----if game_state PLAYING:bird.update()# 撞击地面触地检测if bird.y bird.radius SCREEN_HEIGHT:game_state GAME_OVERfor pipe in pipes[:]:pipe.move()# 检查小鸟是否与当前管道撞击if pipe.collides_with(bird):game_state GAME_OVER# 计分逻辑当小鸟安全越过管道的右边缘if not pipe.passed and pipe.x pipe.width bird.x:score 1pipe.passed True# 管道移出屏幕左侧后销毁释放内存if pipe.x pipe.width 0:pipes.remove(pipe)# ---- C. 画面渲染绘制 ----screen.fill(COLOR_SKY) # 渲染蓝天底色# 绘制所有管道for pipe in pipes:pipe.draw()# 绘制小鸟bird.draw()# 实时渲染计分板if game_state ! START:score_text font.render(f得分: {score}, True, (50, 50, 50))screen.blit(score_text, (15, 15))# 针对不同游戏状态渲染遮罩文字if game_state START:start_font pygame.font.SysFont(SimHei, 28, boldTrue)text_start start_font.render(按下 [空格键] 展翅高飞, True, (255, 255, 255))screen.blit(text_start, (SCREEN_WIDTH // 2 - 140, SCREEN_HEIGHT // 2 - 20))elif game_state GAME_OVER:over_font pygame.font.SysFont(SimHei, 36, boldTrue)hint_font pygame.font.SysFont(SimHei, 20)text_over over_font.render(小鸟坠毁了, True, (240, 60, 60))text_hint hint_font.render(按下 [空格键] 再次挑战 / [ESC] 退出, True, (255, 255, 255))screen.blit(text_over, (SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2 - 50))screen.blit(text_hint, (SCREEN_WIDTH // 2 - 160, SCREEN_HEIGHT // 2 10))pygame.display.flip()clock.tick(60) # 锁定 60 帧丝滑物理动画if __name__ __main__:main_game()请谨慎使用此类代码。 游戏操作与手感说明操作方式按下 空格键 (Space) 控制小鸟往上拍打一次翅膀。物理手感游戏还原了真实的惯性下坠。频繁快速点击空格小鸟会极速向上冲如果不点击重力加速度会使小鸟像石头一样迅速向下砸落需要非常精确地控制点击节奏。内存优化代码加入了精细的生命周期管理向左移出屏幕范围的管道块会被 pipes.remove() 自动回收因此游戏长时间运行也不会占用多余的电脑内存。https://blxjj.com
返回列表