pycharm小鸟管道游戏代码
时间: 2023-07-05 19:17:06 浏览: 62
以下是一个基于Python和Pygame库的简单小鸟管道游戏的代码实现,供参考:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 游戏窗口大小
screen_width = 288
screen_height = 512
# 创建游戏窗口
game_screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("PyCharm小鸟管道游戏")
# 加载游戏资源
bg_img = pygame.image.load("assets/background.png").convert()
bird_imgs = [
pygame.image.load("assets/bird0.png").convert_alpha(),
pygame.image.load("assets/bird1.png").convert_alpha(),
pygame.image.load("assets/bird2.png").convert_alpha(),
]
pipe_img = pygame.image.load("assets/pipe.png").convert_alpha()
base_img = pygame.image.load("assets/base.png").convert_alpha()
# 游戏参数设置
gravity = 0.25
bird_speed = -6
pipe_speed = -3
pipe_gap = 100
pipe_interval = 120
base_speed = -1
# 游戏状态
score = 0
game_over = False
# 声音效果
wing_sound = pygame.mixer.Sound("assets/sounds/wing.wav")
point_sound = pygame.mixer.Sound("assets/sounds/point.wav")
hit_sound = pygame.mixer.Sound("assets/sounds/hit.wav")
die_sound = pygame.mixer.Sound("assets/sounds/die.wav")
# 定义函数:绘制小鸟
def draw_bird(bird_index, bird_rect):
bird_surface = bird_imgs[bird_index]
game_screen.blit(bird_surface, bird_rect)
# 定义函数:绘制管道
def draw_pipe(pipe_list):
for pipe in pipe_list:
top_pipe_rect = pipe_img.get_rect(midbottom=(pipe[0], pipe[1] - pipe_gap))
bottom_pipe_rect = pipe_img.get_rect(midtop=(pipe[0], pipe[1] + pipe_gap))
game_screen.blit(pipe_img, top_pipe_rect)
game_screen.blit(pipe_img, bottom_pipe_rect)
# 定义函数:检测碰撞
def check_collision(bird_rect, pipe_list):
if bird_rect.top <= 0 or bird_rect.bottom >= 450:
return True
for pipe in pipe_list:
top_pipe_rect = pipe_img.get_rect(midbottom=(pipe[0], pipe[1] - pipe_gap))
bottom_pipe_rect = pipe_img.get_rect(midtop=(pipe[0], pipe[1] + pipe_gap))
if bird_rect.colliderect(top_pipe_rect) or bird_rect.colliderect(bottom_pipe_rect):
return True
return False
# 定义函数:更新分数
def update_score(pipe_list):
global score
for pipe in pipe_list:
if pipe[0] == 50:
score += 1
pygame.mixer.Sound.play(point_sound)
# 初始化小鸟位置和速度
bird_rect = bird_imgs[0].get_rect(center=(50, 256))
bird_speed_y = 0
bird_index = 0
bird_animation = pygame.USEREVENT + 1
pygame.time.set_timer(bird_animation, 200)
# 初始化管道位置和速度
pipe_list = []
spawn_pipe = pygame.USEREVENT
pygame.time.set_timer(spawn_pipe, pipe_interval)
# 初始化地面位置和速度
base_x = 0
# 游戏主循环
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 and not game_over:
bird_speed_y = bird_speed
pygame.mixer.Sound.play(wing_sound)
if event.key == pygame.K_RETURN and game_over:
game_over = False
score = 0
pipe_list.clear()
bird_rect.center = (50, 256)
bird_speed_y = 0
base_x = 0
if event.type == bird_animation:
if bird_index < 2:
bird_index += 1
else:
bird_index = 0
bird_rect, bird_surface = bird_rect, bird_imgs[bird_index]
if event.type == spawn_pipe and not game_over:
pipe_list.append([screen_width, random.randint(150, 350)])
# 绘制游戏元素
game_screen.blit(bg_img, (0, 0))
if not game_over:
# 更新小鸟速度和位置
bird_speed_y += gravity
bird_rect.centery += bird_speed_y
draw_bird(bird_index, bird_rect)
# 更新管道位置和速度
for pipe in pipe_list:
pipe[0] += pipe_speed
draw_pipe(pipe_list)
# 检测碰撞
game_over = check_collision(bird_rect, pipe_list)
if game_over:
pygame.mixer.Sound.play(hit_sound)
pygame.mixer.Sound.play(die_sound)
# 更新分数
update_score(pipe_list)
# 更新地面位置
base_x += base_speed
if base_x <= -336:
base_x = 0
game_screen.blit(base_img, (base_x, 450))
# 更新分数显示
score_surface = pygame.font.Font(None, 36).render("Score: {}".format(score), True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(screen_width / 2, 50))
game_screen.blit(score_surface, score_rect)
# 更新屏幕显示
pygame.display.update()
pygame.time.Clock().tick(60)
```
注意:以上代码仅供参考,实际运行可能需要根据自己的环境和需求进行相应的修改。