python飞机大战代码
时间: 2023-07-03 19:24:59 浏览: 113
以下是基于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("飞机大战")
# 加载背景图片
bg_img = pygame.image.load("images/background.png").convert()
# 加载飞机图片
player_img = pygame.image.load("images/me1.png").convert_alpha()
# 加载子弹图片
bullet_img = pygame.image.load("images/bullet1.png").convert_alpha()
# 加载敌机图片
enemy_img = pygame.image.load("images/enemy1.png").convert_alpha()
# 加载爆炸图片
explosion_imgs = [
pygame.image.load("images/enemy0_down1.png").convert_alpha(),
pygame.image.load("images/enemy0_down2.png").convert_alpha(),
pygame.image.load("images/enemy0_down3.png").convert_alpha(),
pygame.image.load("images/enemy0_down4.png").convert_alpha()
]
# 加载游戏音效
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
bullet_sound.set_volume(0.2)
explosion_sound = pygame.mixer.Sound("sounds/explosion.wav")
explosion_sound.set_volume(0.2)
bg_music = pygame.mixer.music.load("sounds/game_music.mp3")
pygame.mixer.music.play(-1)
# 定义游戏对象类
class GameObject:
def __init__(self, img, x, y, speed):
self.img = img
self.rect = self.img.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = speed
def move(self):
self.rect.y += self.speed
# 定义玩家飞机类
class Player(GameObject):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
self.bullets = []
self.is_hit = False
def move_up(self):
self.rect.y -= self.speed
def move_down(self):
self.rect.y += self.speed
def move_left(self):
self.rect.x -= self.speed
def move_right(self):
self.rect.x += self.speed
def fire(self):
bullet = Bullet(bullet_img, self.rect.centerx, self.rect.y, -10)
self.bullets.append(bullet)
bullet_sound.play()
# 定义子弹类
class Bullet(GameObject):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
def move(self):
self.rect.y += self.speed
# 定义敌机类
class Enemy(GameObject):
def __init__(self, img, x, y, speed, hp):
super().__init__(img, x, y, speed)
self.hp = hp
def move(self):
self.rect.y += self.speed
def hit(self):
self.hp -= 1
if self.hp == 0:
explosion = Explosion(explosion_imgs, self.rect.centerx, self.rect.centery)
explosions.append(explosion)
explosion_sound.play()
return True
else:
return False
# 定义爆炸类
class Explosion:
def __init__(self, imgs, x, y):
self.imgs = imgs
self.img_index = 0
self.image = self.imgs[self.img_index]
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.centery = y
self.counter = 0
def update(self):
self.counter += 1
if self.counter % 3 == 0:
self.img_index += 1
if self.img_index == len(self.imgs):
return True
else:
self.image = self.imgs[self.img_index]
return False
# 创建玩家飞机
player = Player(player_img, screen_width / 2 - player_img.get_width() / 2, screen_height - player_img.get_height() - 50, 5)
# 创建敌机列表和爆炸列表
enemies = []
explosions = []
# 游戏主循环
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_UP]:
player.move_up()
if keys[pygame.K_DOWN]:
player.move_down()
if keys[pygame.K_LEFT]:
player.move_left()
if keys[pygame.K_RIGHT]:
player.move_right()
if keys[pygame.K_SPACE]:
player.fire()
# 创建敌机
if random.randint(1, 100) == 1:
enemy = Enemy(enemy_img, random.randint(0, screen_width - enemy_img.get_width()), -enemy_img.get_height(), random.randint(1, 3), 1)
enemies.append(enemy)
# 移动玩家飞机和子弹
player.move()
for bullet in player.bullets:
bullet.move()
# 移动敌机和判断是否被击中
for enemy in enemies:
enemy.move()
for bullet in player.bullets:
if enemy.rect.colliderect(bullet.rect):
if enemy.hit():
enemies.remove(enemy)
player.bullets.remove(bullet)
break
if enemy.rect.colliderect(player.rect):
player.is_hit = True
enemies.remove(enemy)
break
# 更新爆炸动画
for explosion in explosions:
if explosion.update():
explosions.remove(explosion)
# 绘制游戏界面
screen.blit(bg_img, (0, 0))
screen.blit(player.img, player.rect)
for bullet in player.bullets:
screen.blit(bullet.img, bullet.rect)
for enemy in enemies:
screen.blit(enemy.img, enemy.rect)
for explosion in explosions:
screen.blit(explosion.image, explosion.rect)
# 更新游戏画面
pygame.display.update()
# 判断玩家是否死亡
if player.is_hit:
pygame.quit()
exit()
```
注意:上述代码中的图片和音效资源需要自行下载并放置在正确的路径下。
阅读全文