返回

凌空飞翔,纵横管道森林:用Pygame打造你的专属Flappy Bird

后端

Flappy Bird:打造你的第一款 Pygame 游戏

在这个激动人心的指南中,我们将带领你踏上创建经典游戏 Flappy Bird 的旅程,使用 Python 和强大的 Pygame 库。无论你是游戏开发新手还是经验丰富的程序员,本教程将一步一步地指导你,让你成为 Flappy Bird 游戏大师!

准备工作

在你开始之前,确保你的机器已安装 Pygame。只需在命令行中输入以下命令:

pip install pygame

安装完成后,让我们开始吧!

创建游戏窗口

首先,我们需要一个画布来展示我们的游戏。使用 Pygame 的 display 模块,我们可以创建具有特定宽高和标题的游戏窗口:

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Flappy Bird")

这将创建宽 640 像素、高 480 像素的窗口,标题为 "Flappy Bird"。

加载图像和声音

接下来,让我们为游戏加载必需的图像和声音。我们将使用 Pygame 的 imagemixer 模块:

bird_image = pygame.image.load("bird.png")
pipe_image = pygame.image.load("pipe.png")
background_image = pygame.image.load("background.png")
jump_sound = pygame.mixer.Sound("jump.wav")
hit_sound = pygame.mixer.Sound("hit.wav")

这些变量现在包含了鸟、管道、背景、跳跃音效和撞击音效的图像和声音。

定义游戏变量

我们需要一些变量来跟踪游戏状态,例如鸟的位置和速度、管道的坐标、分数和游戏是否结束:

bird_x = 100
bird_y = 100
bird_vel = 0
pipe_x = 600
pipe_y = 0
pipe_vel = -5
score = 0
game_over = False

游戏循环

游戏循环是游戏的核心,它不断运行,直到游戏结束。在这里,我们将不断更新游戏状态、处理玩家输入和绘制游戏画面:

while not game_over:
    # 处理玩家输入
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_vel = -10

    # 更新游戏状态
    bird_y += bird_vel
    bird_vel += 1
    pipe_x += pipe_vel

    # 撞击检测
    if bird_y < 0 or bird_y > 480:
        game_over = True
    if bird_x + 50 > pipe_x and bird_x < pipe_x + 100:
        if bird_y < pipe_y + 100 or bird_y > pipe_y + 400:
            game_over = True

    # 得分检测
    if bird_x > pipe_x + 100:
        score += 1
        pipe_x = 600
        pipe_y = random.randint(0, 300)

    # 绘制游戏画面
    screen.fill((255, 255, 255))
    screen.blit(background_image, (0, 0))
    screen.blit(bird_image, (bird_x, bird_y))
    screen.blit(pipe_image, (pipe_x, pipe_y))
    pygame.display.update()

显示游戏结束画面

当游戏结束时,我们显示游戏结束画面,显示分数并等待玩家退出:

pygame.display.set_caption("Flappy Bird - Game Over")
font = pygame.font.SysFont("Arial", 30)
text = font.render("Score: " + str(score), True, (0, 0, 0))
screen.blit(text, (200, 200))
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

完整代码

以下是你可以在自己机器上运行的完整代码:

import pygame
import sys
import random

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Flappy Bird")

bird_image = pygame.image.load("bird.png")
pipe_image = pygame.image.load("pipe.png")
background_image = pygame.image.load("background.png")
jump_sound = pygame.mixer.Sound("jump.wav")
hit_sound = pygame.mixer.Sound("hit.wav")

bird_x = 100
bird_y = 100
bird_vel = 0
pipe_x = 600
pipe_y = 0
pipe_vel = -5
score = 0
game_over = False

while not game_over:
    # 处理玩家输入
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_vel = -10

    # 更新游戏状态
    bird_y += bird_vel
    bird_vel += 1
    pipe_x += pipe_vel

    # 撞击检测
    if bird_y < 0 or bird_y > 480:
        game_over = True
    if bird_x + 50 > pipe_x and bird_x < pipe_x + 100:
        if bird_y < pipe_y + 100 or bird_y > pipe_y + 400:
            game_over = True

    # 得分检测
    if bird_x > pipe_x + 100:
        score += 1
        pipe_x = 600
        pipe_y = random.randint(0, 300)

    # 绘制游戏画面
    screen.fill((255, 255, 255))
    screen.blit(background_image, (0, 0))
    screen.blit(bird_image, (bird_x, bird_y))
    screen.blit(pipe_image, (pipe_x, pipe_y))
    pygame.display.update()

# 显示游戏结束画面
pygame.display.set_caption("Flappy Bird - Game Over")
font = pygame.font.SysFont("Arial", 30)
text = font.render("Score: " + str(score), True, (0, 0, 0))
screen.blit(text, (200, 200))
pygame.display.update()

# 等待玩家退出
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

常见问题解答

1. 如何让管道随机生成?

pipe_y = random.randint(0, 300)

2. 如何让鸟跳得更高?
更改 bird_vel 变量的初始值。值越小,鸟跳得越高。

3. 如何调整管道的速度?
更改 pipe_vel 变量的值。负值表示管道向左移动,正值表示管道向右移动。

4. 如何添加更多管道?
在游戏循环中添加以下代码:

if pipe_x < -100:
    pipe_x = 600
    pipe_y = random.randint(0, 300)

5. 如何添加音效?

jump_sound.play()

在需要的地方调用 play() 方法以播放音效。

享受 Flappy Bird 的世界,并尽情探索 Pygame 的无限可能!