|
发表于 2023-3-20 14:52:08
|
显示全部楼层
以下是 Python 飞机大战的基本源码,仅供参考:
```
import pygame
import time
import random
# 初始化 Pygame
pygame.init()
# 创建屏幕
screen = pygame.display.set_mode((480, 800))
# 标题和图标
pygame.display.set_caption(\"Plane Game\")
ico = pygame.image.load(\"resources/images/logo.ico\").convert_alpha()
pygame.display.set_icon(ico)
# 载入背景图片
bg = pygame.image.load(\"resources/images/background.png\").convert()
# 加载游戏音乐和音效
pygame.mixer.music.load(\"resources/sound/game_music.ogg\")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound(\"resources/sound/bullet.wav\")
bullet_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound(\"resources/sound/enemy1_down.wav\")
enemy1_down_sound.set_volume(0.2)
# 游戏结束图片和音效
game_over = pygame.image.load(\"resources/images/gameover.png\").convert_alpha()
game_over_rect = game_over.get_rect()
font = pygame.font.Font(None, 48)
# 设置玩家飞机相关参数
player_img = pygame.image.load(\"resources/images/player.png\").convert_alpha()
player_rect = player_img.get_rect()
player_rect.midbottom = (240, 700)
player_speed = 10
player_bullet_speed = 14
player_bullets = []
player_bullet_interval = 0
# 设置敌人飞机相关参数
enemy1_imgs = [
pygame.image.load(\"resources/images/enemy1.png\").convert_alpha(),
pygame.image.load(\"resources/images/enemy1_down1.png\").convert_alpha(),
pygame.image.load(\"resources/images/enemy1_down2.png\").convert_alpha(),
pygame.image.load(\"resources/images/enemy1_down3.png\").convert_alpha(),
]
enemy1_speed = 2
enemies1 = []
enemy1_downs = []
enemy1_interval = 0
# 得分
score = 0
# 主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 绘制背景
screen.blit(bg, (0, 0))
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play()
# 绘制玩家飞机
player_rect.x += player_speed
if player_rect.left <= 0:
player_rect.left = 0
if player_rect.right >= 480:
player_rect.right = 480
screen.blit(player_img, player_rect)
# 发射玩家子弹
player_bullet_interval -= 1
if player_bullet_interval < 0:
player_bullet_interval = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] and player_bullet_interval == 0:
player_bullet = pygame.Rect(player_rect.centerx - 3, player_rect.top - 20, 6, 20)
player_bullets.append(player_bullet)
bullet_sound.play()
player_bullet_interval = 15
# 绘制玩家子弹
for b in player_bullets:
b.top -= player_bullet_speed
if b.bottom <= 0:
player_bullets.remove(b)
pygame.draw.rect(screen, pygame.Color(0, 255, 0), b)
# 发射敌人飞机
enemy1_interval -= 1
if enemy1_interval < 0:
enemy1_x = random.randint(0, 480 - enemy1_imgs[0].get_width())
enemy1_y = -enemy1_imgs[0].get_height()
enemy1_rect = pygame.Rect(enemy1_x, enemy1_y, enemy1_imgs[0].get_width(), enemy1_imgs[0].get_height())
enemies1.append(enemy1_rect)
enemy1_interval = 100 - score // 10
# 绘制敌人飞机
for e in enemies1:
if e.bottom > 0:
screen.blit(enemy1_imgs[0], e)
e.top += enemy1_speed
else:
enemies1.remove(e)
# 检测玩家子弹是否击中敌人飞机
for b in player_bullets:
for e in enemies1:
if b.colliderect(e):
enemies1.remove(e)
player_bullets.remove(b)
enemy1_downs.append(e)
score += 1
enemy1_down_sound.play()
if len(enemy1_downs) > 0:
for e in enemy1_downs:
screen.blit(enemy1_imgs[len(enemy1_imgs) - e.bottom // 30], e)
if e.bottom >= 0:
enemy1_downs.remove(e)
# 绘制得分
score_text = font.render(f\"Score: {score}\", True, pygame.Color(255, 0, 0))
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.update()
# 检测玩家是否与敌人飞机碰撞
for e in enemies1:
if e.colliderect(player_rect):
screen.blit(game_over, game_over_rect)
pygame.display.update()
time.sleep(2)
pygame.quit()
exit()
```
请注意,这只是一个基本的框架,您可能需要根据自己的需求进行修改和调整。 |
|