首先,你需要确保已经安装了Pygame库。如果还没有安装,可以通过以下命令进行安装:
【bash】
pip install pygame
游戏的代码:
【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.cent