愤怒的小鸟python代码
时间: 2023-07-10 11:11:52 浏览: 201
Python版经典小游戏愤怒的小鸟源代码,基于pygame+pymunk
5星 · 资源好评率100%
这是一个基于 Pygame 的愤怒的小鸟游戏的 Python 代码:
```python
import pygame
import random
pygame.init()
# 游戏窗口大小
WIDTH = 288
HEIGHT = 512
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 加载图片
BACKGROUND_IMAGE = pygame.image.load("assets/background-day.png").convert()
GROUND_IMAGE = pygame.image.load("assets/base.png").convert()
BIRD_IMAGES = [pygame.image.load("assets/yellowbird-downflap.png").convert_alpha(),
pygame.image.load("assets/yellowbird-midflap.png").convert_alpha(),
pygame.image.load("assets/yellowbird-upflap.png").convert_alpha()]
PIPE_IMAGE = pygame.image.load("assets/pipe-green.png").convert()
# 设置帧率
CLOCK = pygame.time.Clock()
FPS = 60
# 鸟类
class Bird(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = BIRD_IMAGES[0]
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH // 2
self.rect.centery = HEIGHT // 2
self.speed = 0
self.gravity = 0.5
def update(self):
self.speed += self.gravity
self.rect.centery += self.speed
if self.rect.bottom >= 400:
self.rect.bottom = 400
def jump(self):
self.speed = -10
# 管道类
class Pipe(pygame.sprite.Sprite):
def __init__(self, x, y, position):
super().__init__()
self.image = PIPE_IMAGE
self.rect = self.image.get_rect()
self.rect.centerx = x
if position == "top":
self.image = pygame.transform.flip(self.image, False, True)
self.rect.bottom = y - 100
else:
self.rect.top = y + 100
self.rect.centerx = x
self.speed = -2
def update(self):
self.rect.centerx += self.speed
if self.rect.right < 0:
self.kill()
# 地面类
class Ground(pygame.sprite.Sprite):
def __init__(self, x):
super().__init__()
self.image = GROUND_IMAGE
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = 400
self.speed = -2
def update(self):
self.rect.x += self.speed
if self.rect.right < 0:
self.rect.x = WIDTH
# 创建精灵组
bird_group = pygame.sprite.Group()
pipe_group = pygame.sprite.Group()
ground_group = pygame.sprite.Group()
# 创建鸟
bird = Bird()
bird_group.add(bird)
# 创建地面
for i in range(2):
ground = Ground(i * WIDTH)
ground_group.add(ground)
# 游戏主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.jump()
# 添加管道
if len(pipe_group) < 3:
pipe_x = WIDTH + 100
offset = random.randint(-100, 100)
pipe_y = HEIGHT / 2 + offset
top_pipe = Pipe(pipe_x, pipe_y, "top")
bottom_pipe = Pipe(pipe_x, pipe_y, "bottom")
pipe_group.add(top_pipe)
pipe_group.add(bottom_pipe)
# 更新精灵组
bird_group.update()
pipe_group.update()
ground_group.update()
# 绘制背景
SCREEN.blit(BACKGROUND_IMAGE, (0, 0))
# 绘制精灵组
bird_group.draw(SCREEN)
pipe_group.draw(SCREEN)
ground_group.draw(SCREEN)
# 显示分数
pygame.display.set_caption("Flappy Bird - Score: {}".format(pygame.time.get_ticks() // 1000))
# 刷新屏幕
pygame.display.flip()
# 设置帧率
CLOCK.tick(FPS)
```
这个代码实现了一个基本的愤怒的小鸟游戏,其中包括了鸟、管道、地面等基本元素的定义和更新,以及游戏主循环、事件处理、精灵组的创建和更新等功能。
阅读全文