用Python写一个飞机大战
时间: 2024-03-06 22:30:26 浏览: 48
好的,以下是一个简单的飞机大战游戏的 Python 代码示例:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 窗口大小和标题
WIDTH = 480
HEIGHT = 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞机大战")
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 加载背景音乐和音效
pygame.mixer.music.load("bgm.mp3")
pygame.mixer.music.play(-1)
bullet_sound = pygame.mixer.Sound("bullet.wav")
explosion_sound = pygame.mixer.Sound("explosion.wav")
# 加载图片
bg_image = pygame.image.load("background.png")
player_image = pygame.image.load("player.png")
enemy_image = pygame.image.load("enemy.png")
bullet_image = pygame.image.load("bullet.png")
explosion_images = []
for i in range(1, 6):
explosion_images.append(pygame.image.load(f"explosion{i}.png"))
# 游戏对象类
class GameObject:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.width = image.get_width()
self.height = image.get_height()
self.image = image
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
# 玩家类
class Player(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.speed = 5
def move(self, dx, dy):
self.x += dx * self.speed
self.y += dy * self.speed
# 限制玩家移动范围
if self.x < 0:
self.x = 0
elif self.x > WIDTH - self.width:
self.x = WIDTH - self.width
if self.y < 0:
self.y = 0
elif self.y > HEIGHT - self.height:
self.y = HEIGHT - self.height
def shoot(self):
bullet = Bullet(self.x + self.width // 2 - bullet_image.get_width() // 2, self.y, bullet_image)
bullets.append(bullet)
bullet_sound.play()
# 敌人类
class Enemy(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.speed = random.randint(1, 3)
def move(self):
self.y += self.speed
# 子弹类
class Bullet(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.speed = 10
def move(self):
self.y -= self.speed
# 爆炸类
class Explosion:
def __init__(self, x, y):
self.x = x
self.y = y
self.images = explosion_images
self.index = 0
self.frame_rate = 5
self.frame_count = 0
def draw(self, surface):
surface.blit(self.images[self.index], (self.x, self.y))
self.frame_count += 1
if self.frame_count % self.frame_rate == 0:
self.index += 1
if self.index >= len(self.images):
explosions.remove(self)
# 创建游戏对象
player = Player(WIDTH // 2 - player_image.get_width() // 2, HEIGHT - player_image.get_height(), player_image)
enemies = []
bullets = []
explosions = []
# 游戏循环
running = True
clock = pygame.time.Clock()
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_SPACE:
player.shoot()
# 玩家移动
keys = pygame.key.get_pressed()
dx = keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
dy = keys[pygame.K_DOWN] - keys[pygame.K_UP]
player.move(dx, dy)
# 敌人出现
if random.randint(1, 100) == 1:
enemy = Enemy(random.randint(0, WIDTH - enemy_image.get_width()), -enemy_image.get_height(), enemy_image)
enemies.append(enemy)
# 更新游戏对象状态
for enemy in enemies:
enemy.move()
if enemy.y > HEIGHT:
enemies.remove(enemy)
elif enemy.y + enemy.height > player.y and enemy.x < player.x + player.width and enemy.x + enemy.width > player.x:
explosions.append(Explosion(player.x, player.y))
explosion_sound.play()
running = False
for bullet in bullets:
bullet.move()
if bullet.y < -bullet.height:
bullets.remove(bullet)
else:
for enemy in enemies:
if bullet.x < enemy.x + enemy.width and bullet.x + bullet.width > enemy.x and bullet.y < enemy.y + enemy.height and bullet.y + bullet.height > enemy.y:
enemies.remove(enemy)
bullets.remove(bullet)
explosions.append(Explosion(enemy.x, enemy.y))
explosion_sound.play()
# 绘制游戏界面
WINDOW.blit(bg_image, (0, 0))
player.draw(WINDOW)
for enemy in enemies:
enemy.draw(WINDOW)
for bullet in bullets:
bullet.draw(WINDOW)
for explosion in explosions:
explosion.draw(WINDOW)
pygame.display.update()
# 游戏帧率控制
clock.tick(60)
# 退出 Pygame
pygame.quit()
```
这是一个简单的版本,你可以根据需要进行修改和扩展。
阅读全文