用python帮我写一个飞机大战的游戏
时间: 2023-06-15 14:03:15 浏览: 207
好的,我可以为您提供一个简单的飞机大战游戏的代码。以下是代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口的尺寸
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 800
WINDOW_SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)
# 创建游戏窗口
screen = pygame.display.set_mode(WINDOW_SIZE)
# 设置游戏窗口标题
pygame.display.set_caption("Plane War")
# 加载游戏素材
bg_img = pygame.image.load("images/bg.jpg").convert()
plane_img = pygame.image.load("images/plane.png").convert_alpha()
bullet_img = pygame.image.load("images/bullet.png").convert_alpha()
enemy_img = pygame.image.load("images/enemy.png").convert_alpha()
boom_img = pygame.image.load("images/boom.png").convert_alpha()
# 设置游戏字体
font = pygame.font.SysFont("arial", 24)
# 设置游戏音效
pygame.mixer.music.load("sounds/bg.mp3")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
bullet_sound.set_volume(0.2)
enemy_down_sound = pygame.mixer.Sound("sounds/enemy_down.wav")
enemy_down_sound.set_volume(0.2)
# 定义游戏变量
score = 0
plane_rect = plane_img.get_rect()
plane_rect.centerx = WINDOW_WIDTH // 2
plane_rect.bottom = WINDOW_HEIGHT - 20
bullet_list = []
enemy_list = []
boom_list = []
enemy_speed = 1
enemy_add_counter = 0
enemy_down_counter = 0
game_over = False
# 定义函数:绘制分数
def draw_score():
score_text = font.render("Score: {}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 定义函数:绘制飞机
def draw_plane():
screen.blit(plane_img, plane_rect)
# 定义函数:绘制子弹
def draw_bullet():
for bullet in bullet_list:
screen.blit(bullet_img, bullet)
# 定义函数:绘制敌机
def draw_enemy():
for enemy in enemy_list:
screen.blit(enemy_img, enemy)
# 定义函数:绘制爆炸
def draw_boom():
for boom in boom_list:
screen.blit(boom_img, boom)
# 定义函数:生成敌机
def generate_enemy():
random_left = random.randint(0, WINDOW_WIDTH - enemy_img.get_width())
enemy_rect = enemy_img.get_rect()
enemy_rect.left = random_left
enemy_rect.bottom = 0
enemy_list.append(enemy_rect)
# 定义函数:移动子弹
def move_bullet():
for bullet in bullet_list:
bullet.move_ip(0, -10)
if bullet.bottom < 0:
bullet_list.remove(bullet)
# 定义函数:移动敌机
def move_enemy():
global game_over
for enemy in enemy_list:
enemy.move_ip(0, enemy_speed)
if enemy.top > WINDOW_HEIGHT:
enemy_list.remove(enemy)
game_over = True
# 定义函数:检测碰撞
def check_collision():
global score, enemy_down_counter
for bullet in bullet_list:
for enemy in enemy_list:
if bullet.colliderect(enemy):
bullet_list.remove(bullet)
enemy_list.remove(enemy)
enemy_down_counter = 5
boom_list.append(enemy)
enemy_down_sound.play()
score += 10
# 定义函数:播放爆炸
def play_boom():
global enemy_down_counter
for boom in boom_list:
screen.blit(boom_img, boom)
enemy_down_counter -= 1
if enemy_down_counter == 0:
boom_list.remove(boom)
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
bullet_list.append(pygame.Rect(plane_rect.centerx - 3, plane_rect.top - 10, 6, 10))
bullet_sound.play()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
exit()
# 绘制背景
screen.blit(bg_img, (0, 0))
# 绘制分数
draw_score()
# 绘制飞机
draw_plane()
# 绘制子弹
draw_bullet()
# 绘制敌机
draw_enemy()
# 绘制爆炸
draw_boom()
# 生成敌机
enemy_add_counter += 1
if enemy_add_counter == 50:
generate_enemy()
enemy_add_counter = 0
# 移动子弹
move_bullet()
# 移动敌机
move_enemy()
# 检测碰撞
check_collision()
# 播放爆炸
play_boom()
# 更新屏幕
pygame.display.update()
# 判断游戏是否结束
if game_over:
game_over_text = font.render("Game Over", True, (255, 0, 0))
screen.blit(game_over_text, (WINDOW_WIDTH // 2 - 60, WINDOW_HEIGHT // 2 - 12))
pygame.display.update()
pygame.time.delay(2000)
break
```
游戏素材和音效需要自行准备,放在与代码相同的目录下。运行代码后,按空格键发射子弹,避免敌机撞到自己。
阅读全文