Python小游戏27——飞翔的小鸟

首先,你需要确保已经安装了Pygame库。如果还没有安装,可以通过以下命令进行安装:

 

【bash】

 pip install pygame

7d38f50b463b4383ae3c994ee256f442.webp

 

游戏的代码:

 

【python】

 import pygame

import random

 

# 初始化Pygame

pygame.init()

 

# 设置屏幕大小和标题

screen_width = 288

screen_height = 512

screen = pygame.display.set_mode((screen_width, screen_height))

pygame.display.set_caption("飞翔的小鸟")

 

# 定义颜色

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

 

# 加载图像

bird_img = pygame.image.load('bird.png') # 你需要有一张名为bird.png的小鸟图像

pipe_top_img = pygame.image.load('pipe_top.png') # 管道上半部分图像

pipe_bottom_img = pygame.image.load('pipe_bottom.png') # 管道下半部分图像

 

# 定义小鸟类

class Bird:

    def __init__(self):

        self.x = screen_width // 2

        self.y = screen_height // 2

        self.gravity = 0.5

        self.jump = -10

        self.img = bird_img

        self.rect = self.img.get_rect()

        self.rect.centerx = self.x

        self.rect.bottom = self.y

 

    def update(self):

        self.gravity += 0.1

        self.y += self.jump + self.gravity

        self.jump = 0

        self.rect.centerx = self.x

        self.rect.bottom = self.y

 

        # 碰到地面则重置位置

        if self.rect.bottom >= screen_height:

            self.y = screen_height // 2

            self.gravity = 0.5

            self.jump = 0

 

    def draw(self, screen):

        screen.blit(self.img, self.rect)

 

    def jump_action(self):

        self.jump = -10

 

# 定义管道类

class Pipe:

    def __init__(self):

        self.x = screen_width + random.randint(0, 100)

        self.y = random.randint(100, 300)

        self.gap = random.randint(100, 200)

        self.top = pygame.transform.scale(pipe_top_img, (screen_width // 10, self.y))

        self.bottom = pygame.transform.scale(pipe_bottom_img, (screen_width // 10, screen_height – self.y – self.gap))

 

    def update(self):

        self.x -= 5

 

    def draw(self, screen):

        screen.blit(self.top, (self.x, 0))

        screen.blit(self.bottom, (self.x, self.y + self.gap))

 

    def check_collision(self, bird):

        bird_rect = pygame.Rect(bird.rect.x – bird.rect.width // 4, bird.rect.y, bird.rect.width // 2, bird.rect.height)

        if (bird_rect.colliderect(pygame.Rect(self.x, 0, self.top.get_width(), self.top.get_height())) or

            bird_rect.colliderect(pygame.Rect(self.x, self.y + self.gap, self.bottom.get_width(), self.bottom.get_height()))):

            return True

        return False

 

# 创建小鸟和管道对象

bird = Bird()

pipes = [Pipe() for _ in range(5)] # 初始生成5个管道

 

# 游戏主循环

running = True

clock = pygame.time.Clock()

 

while running:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False

 

    # 检测按键

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE] and bird.y == screen_height // 2: # 只有当小鸟在地面上时才能跳跃

        bird.jump_action()

 

    # 更新小鸟和管道位置

    bird.update()

    for pipe in pipes:

        pipe.update()

 

    # 检查碰撞

    if any(pipe.check_collision(bird) for pipe in pipes):

        running = False # 碰到管道则游戏结束

 

    # 移除屏幕外的管道

    pipes = [pipe for pipe in pipes if pipe.x > -screen_width // 10]

    if not pipes:

        pipes.append(Pipe()) # 当所有管道都移出屏幕时,生成新的管道

 

    # 绘制背景(这里简单用白色填充)

    screen.fill(WHITE)

 

    # 绘制小鸟和管道

    bird.draw(screen)

    for pipe in pipes:

        pipe.draw(screen)

 

    # 更新屏幕显示

    pygame.display.flip()

 

    # 控制帧率

    clock.tick(30)

 

# 游戏结束处理

pygame.quit()

 

注意事项:

 

1. 你需要准备三张图像文件:bird.png(小鸟图像)、pipe_top.png(管道上半部分图像)和pipe_bottom.png(管道下半部分图像)。这些图像应该放在与你的Python脚本相同的目录中。

 

2. 游戏逻辑相对简单,只实现了基本的跳跃和碰撞检测功能。你可以根据需要进一步扩展和优化游戏功能,比如添加分数系统、增加更多的管道和障碍物等。

 

3. 游戏的帧率和管道生成速度等参数可以根据你的需要进行调整。

涉及代码知识总结:

 

1. 导入库和模块:

 

    • import pgzrun:导入pgzrun库,用于简化Pygame游戏的编写和运行。

 

    • import random:导入随机库,用于生成随机数和随机位置。

 

    • import pygame及其子模块:用于游戏开发,包括图形显示、声音处理等。

 

2. 游戏设置和初始化:

 

    • 设置窗口的宽度和高度。

 

    • 加载和显示背景、小鸟、障碍物等图片。

 

    • 初始化游戏变量,如得分、速度等。

 

3. 游戏循环:

 

    • draw()函数:负责绘制游戏的每一帧,包括背景、障碍物和小鸟。

 

    • update()函数:更新游戏状态,包括小鸟的下落与上升、障碍物的移动等。

 

    • 游戏循环中不断调用draw()和update()函数,形成游戏的动态效果。

 

4. 事件处理:

 

    • 检测鼠标点击事件,控制小鸟的上升。

 

    • 检测游戏结束条件,如小鸟碰到障碍物或超出屏幕边界。

 

5. 碰撞检测:

 

    • 使用Pygame的碰撞检测功能,判断小鸟是否与障碍物发生碰撞。

 

6. 得分系统:

 

    • 通过穿越障碍物间的空隙来增加得分。

 

    • 可以设置得分增加的条件和规则。

 

7. 游戏重置:

 

    • 当游戏失败时,重置游戏状态,包括得分、速度、小鸟和障碍物的位置等。

 

8. 变量和数据类型:

 

    • 使用整型、浮点型等数据类型存储游戏变量。

 

    • 使用列表、字典等数据结构存储和管理游戏元素。

 

9. 控制结构:

 

    • 使用if语句进行条件判断,如检测游戏结束条件。

 

    • 使用while循环实现游戏的主循环。

 

10. 函数和模块:

 

    • 定义函数来封装游戏逻辑和绘制代码,提高代码的可读性和可维护性。

 

    • 可以将游戏的不同部分拆分成多个模块,如背景模块、小鸟模块、障碍物模块等。

 

11. 物理效果(可选):

 

    • 对于进阶玩家,可以尝试将小鸟设定为受重力作用下落,实现更自然的游戏效果。这需要使用物理公式和计算来模拟小鸟的下落和上升过程。

 

12. 图形和音效(可选):

 

    • 使用Pygame的图形和音效功能,为游戏添加更丰富的视觉效果和听觉体验。

作者:虞书欣的C

物联沃分享整理
物联沃-IOTWORD物联网 » Python小游戏27——飞翔的小鸟

发表回复