用python写一个飞机大战的小游戏
时间: 2023-12-07 15:32:41 浏览: 76
好的,以下是一个简单的飞机大战小游戏的代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 创建游戏窗口
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption("飞机大战")
# 加载背景图片
background = pygame.image.load("images/background.png")
# 加载飞机图片
player_img = pygame.image.load("images/me1.png")
player_rect = player_img.get_rect()
player_width = player_rect.width
player_height = player_rect.height
player_x = (screen_width - player_width) // 2
player_y = screen_height - player_height - 50
# 加载子弹图片
bullet_img = pygame.image.load("images/bullet1.png")
bullet_width = bullet_img.get_width()
bullet_height = bullet_img.get_height()
# 加载敌机图片
enemy_img = pygame.image.load("images/enemy1.png")
enemy_width = enemy_img.get_width()
enemy_height = enemy_img.get_height()
# 加载音效
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
enemy_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
game_over_sound = pygame.mixer.Sound("sound/game_over.wav")
# 设置游戏字体
font = pygame.font.Font(None, 36)
# 设置游戏变量
score = 0
game_over = False
clock = pygame.time.Clock()
# 定义函数:绘制分数
def draw_score():
score_text = font.render("Score: {}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 定义函数:绘制游戏结束
def draw_game_over():
game_over_text = font.render("Game Over", True, (255, 0, 0))
screen.blit(game_over_text, ((screen_width - game_over_text.get_width()) // 2, screen_height // 2))
# 定义函数:绘制飞机
def draw_player():
screen.blit(player_img, (player_x, player_y))
# 定义函数:绘制子弹
def draw_bullets(bullets):
for bullet in bullets:
screen.blit(bullet_img, (bullet[0], bullet[1]))
# 定义函数:绘制敌机
def draw_enemies(enemies):
for enemy in enemies:
screen.blit(enemy_img, (enemy[0], enemy[1]))
# 定义函数:移动子弹
def move_bullets(bullets):
for bullet in bullets:
bullet[1] -= 5
# 定义函数:移动敌机
def move_enemies(enemies):
for enemy in enemies:
enemy[1] += 3
# 定义函数:生成敌机
def generate_enemy(enemies):
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = -enemy_height
enemies.append([enemy_x, enemy_y])
# 定义函数:判断子弹是否打中敌机
def hit_enemies(bullets, enemies):
global score
for bullet in bullets:
for enemy in enemies:
if bullet[0] > enemy[0] - bullet_width and bullet[0] < enemy[0] + enemy_width and \
bullet[1] > enemy[1] - bullet_height and bullet[1] < enemy[1] + enemy_height:
bullets.remove(bullet)
enemies.remove(enemy)
score += 100
enemy_down_sound.play()
# 定义函数:判断飞机是否与敌机相撞
def crash_player(player_rect, enemies):
for enemy in enemies:
if pygame.Rect.colliderect(player_rect, pygame.Rect(enemy[0], enemy[1], enemy_width, enemy_height)):
return True
return False
# 创建子弹列表和敌机列表
bullets = []
enemies = []
# 游戏主循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 检测键盘输入
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= 5
if keys[pygame.K_RIGHT]:
player_x += 5
if keys[pygame.K_UP]:
player_y -= 5
if keys[pygame.K_DOWN]:
player_y += 5
if keys[pygame.K_SPACE]:
bullet_sound.play()
bullets.append([player_x + player_width // 2 - bullet_width // 2, player_y - bullet_height])
# 绘制背景
screen.blit(background, (0, 0))
# 绘制分数
draw_score()
# 绘制飞机
draw_player()
# 移动子弹
move_bullets(bullets)
# 绘制子弹
draw_bullets(bullets)
# 生成敌机
if len(enemies) < 5 and not game_over:
generate_enemy(enemies)
# 移动敌机
move_enemies(enemies)
# 绘制敌机
draw_enemies(enemies)
# 判断子弹是否打中敌机
hit_enemies(bullets, enemies)
# 判断游戏结束
if crash_player(player_rect, enemies) and not game_over:
game_over = True
game_over_sound.play()
if game_over:
draw_game_over()
# 更新游戏画面
pygame.display.update()
# 设置游戏帧率
clock.tick(60)
```
请注意,上述代码中使用的图片和音效需要自己准备,可以根据自己的需求进行修改。此外,还需要安装pygame模块,可以使用以下命令进行安装:
```
pip install pygame
```
希望对你有所帮助!
阅读全文