pygame飞机大战代码
时间: 2023-07-03 20:05:44 浏览: 138
飞机大战的代码
以下是一个简单的 Pygame 飞机大战代码示例:
```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").convert()
# 加载玩家飞机
player_img = pygame.image.load("images/me1.png").convert_alpha()
player_rect = player_img.get_rect()
player_rect.left, player_rect.top = (screen_width - player_rect.width) // 2, \
screen_height - player_rect.height - 60
# 加载敌机
enemy1_img = pygame.image.load("images/enemy1.png").convert_alpha() # 小型敌机
enemy2_img = pygame.image.load("images/enemy2.png").convert_alpha() # 中型敌机
enemy3_img = pygame.image.load("images/enemy3.png").convert_alpha() # 大型敌机
enemies = []
enemy_speed = [1, 2, 3] # 不同类型敌机的速度
enemy_freq = 0 # 敌机出现的频率
enemy1_down_img = pygame.image.load("images/enemy1_down.png").convert_alpha()
enemy2_down_img = pygame.image.load("images/enemy2_down.png").convert_alpha()
enemy3_down_img = pygame.image.load("images/enemy3_down.png").convert_alpha()
# 加载子弹
bullet_img = pygame.image.load("images/bullet1.png").convert_alpha()
bullet_rect = bullet_img.get_rect()
bullet_speed = 10
bullet_list = []
# 加载音效
pygame.mixer.init()
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
game_over_sound = pygame.mixer.Sound("sound/game_over.wav")
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
# 设置游戏状态
game_over = False
score = 0
font = pygame.font.SysFont("consolas", 24, True, False)
# 游戏主循环
while not game_over:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 鼠标左键发射子弹
bullet_rect.left, bullet_rect.top = player_rect.left + player_rect.width // 2, \
player_rect.top - bullet_rect.height
bullet_list.append(bullet_rect.copy())
bullet_sound.play()
# 绘制游戏元素
screen.blit(background, (0, 0)) # 绘制背景
# 绘制玩家飞机
screen.blit(player_img, player_rect)
# 绘制敌机
if enemy_freq % 20 == 0:
enemy_type = random.randint(1, 3)
if enemy_type == 1:
enemy_img = enemy1_img
enemy_rect = enemy1_img.get_rect()
elif enemy_type == 2:
enemy_img = enemy2_img
enemy_rect = enemy2_img.get_rect()
else:
enemy_img = enemy3_img
enemy_rect = enemy3_img.get_rect()
enemy_rect.left, enemy_rect.top = random.randint(0, screen_width - enemy_rect.width), \
-enemy_rect.height
enemies.append([enemy_img, enemy_rect, enemy_type])
for enemy in enemies:
if enemy[1].top > screen_height: # 敌机飞出屏幕后删除
enemies.remove(enemy)
else:
enemy[1].top += enemy_speed[enemy[2] - 1]
screen.blit(enemy[0], enemy[1])
# 绘制子弹
for bullet_rect in bullet_list:
bullet_rect.top -= bullet_speed
if bullet_rect.top < -bullet_rect.height: # 子弹飞出屏幕后删除
bullet_list.remove(bullet_rect)
else:
screen.blit(bullet_img, bullet_rect)
# 检测子弹和敌机的碰撞
for bullet_rect in bullet_list:
for enemy in enemies:
if bullet_rect.colliderect(enemy[1]):
if enemy[2] == 1:
score += 100
enemy_down_img = enemy1_down_img
enemy_down_sound = enemy1_down_sound
elif enemy[2] == 2:
score += 200
enemy_down_img = enemy2_down_img
enemy_down_sound = enemy2_down_sound
else:
score += 300
enemy_down_img = enemy3_down_img
enemy_down_sound = enemy3_down_sound
enemy_down_rect = enemy_down_img.get_rect()
enemy_down_rect.left, enemy_down_rect.top = enemy[1].left, enemy[1].top
enemies.remove(enemy)
bullet_list.remove(bullet_rect)
screen.blit(enemy_down_img, enemy_down_rect)
enemy_down_sound.play()
# 绘制得分
score_text = font.render("得分:{}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 更新游戏状态
enemy_freq += 1
if enemy_freq >= 100:
enemy_freq = 0
pygame.display.update()
# 检测玩家飞机和敌机的碰撞
for enemy in enemies:
if player_rect.colliderect(enemy[1]):
game_over = True
game_over_sound.play()
break
# 游戏结束
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
game_over_text = font.render("游戏结束! 得分:{}".format(score), True, (255, 0, 0))
screen.blit(game_over_text, (screen_width // 2 - 100, screen_height // 2))
pygame.display.update()
```
在代码中,我们使用 Pygame 库创建了一个窗口,并加载了游戏元素,如游戏背景、玩家飞机、敌机、子弹、音效等。在游戏主循环中,我们通过处理游戏事件、绘制游戏元素和更新游戏状态来实现游戏逻辑。具体来说,我们使用鼠标左键发射子弹,通过更新敌机和子弹的位置来实现游戏动画效果,并检测子弹和敌机的碰撞以及玩家飞机和敌机的碰撞来更新游戏得分和状态。最后,我们在游戏结束后显示游戏得分和结束信息,并等待玩家退出游戏。
需要注意的是,这只是一个简单的示例代码,如果你想要更加完整和复杂的游戏,还需要更多的代码和调试。
阅读全文