python飞机大战小游戏
时间: 2023-11-22 14:49:59 浏览: 81
以下是Python语言实现的飞机大战小游戏的基本功能:
```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")
# 加载敌方飞机图片
enemy_imgs = [
pygame.image.load("images/enemy1.png"),
pygame.image.load("images/enemy2.png"),
pygame.image.load("images/enemy3.png")
]
# 加载子弹图片
bullet_img = pygame.image.load("images/bullet1.png")
# 加载游戏音效
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
enemy_down_sound = pygame.mixer.Sound("sounds/enemy_down.wav")
game_over_sound = pygame.mixer.Sound("sounds/game_over.wav")
# 设置字体
font = pygame.font.SysFont(None, 48)
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = player_img
self.rect = self.image.get_rect()
self.rect.centerx = screen_width // 2
self.rect.bottom = screen_height - 10
self.speed = 5
self.bullets = pygame.sprite.Group()
def update(self):
# 控制玩家飞机移动
keys = pygame.key.get_pressed()
if keys[pygame.K_w] or keys[pygame.K_UP]:
self.rect.y -= self.speed
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
self.rect.y += self.speed
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
self.rect.x -= self.speed
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
self.rect.x += self.speed
# 控制玩家飞机不超出屏幕范围
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > screen_width:
self.rect.right = screen_width
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
# 控制玩家飞机发射子弹
if keys[pygame.K_k]:
bullet = Bullet(self.rect.centerx, self.rect.top)
self.bullets.add(bullet)
bullet_sound.play()
# 定义敌方飞机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = random.choice(enemy_imgs)
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, screen_width - self.rect.width)
self.rect.y = random.randint(-self.rect.height * 2, -self.rect.height)
self.speed = random.randint(1, 3)
self.hp = 1
def update(self):
# 控制敌方飞机移动
self.rect.y += self.speed
# 控制敌方飞机不超出屏幕范围
if self.rect.top > screen_height:
self.kill()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed = -10
def update(self):
# 控制子弹移动
self.rect.y += self.speed
# 控制子弹不超出屏幕范围
if self.rect.bottom < 0:
self.kill()
# 定义游戏结束类
class GameOver(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = font.render("Game Over", True, (255, 0, 0))
self.rect = self.image.get_rect()
self.rect.centerx = screen_width // 2
self.rect.centery = screen_height // 2
# 创建玩家飞机对象
player = Player()
# 创建敌方飞机组
enemies = pygame.sprite.Group()
# 创建子弹组
bullets = pygame.sprite.Group()
# 创建游戏结束对象
game_over = GameOver()
# 设置游戏帧率
clock = pygame.time.Clock()
FPS = 60
# 设置游戏状态
game_over_flag = False
score = 0
# 游戏循环
while True:
# 控制游戏帧率
clock.tick(FPS)
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 控制游戏状态
if not game_over_flag:
# 控制敌方飞机生成
if random.randint(1, 60) == 1:
enemy = Enemy()
enemies.add(enemy)
# 控制敌方飞机与子弹碰撞
hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
enemy_down_sound.play()
score += 10
# 控制玩家飞机与敌方飞机碰撞
hits = pygame.sprite.spritecollide(player, enemies, True)
if hits:
game_over_flag = True
game_over_sound.play()
# 更新玩家飞机、敌方飞机、子弹
player.update()
enemies.update()
bullets.update()
# 绘制游戏界面
screen.blit(background, (0, 0))
screen.blit(player.image, player.rect)
enemies.draw(screen)
bullets.draw(screen)
# 绘制游戏得分
score_text = font.render("Score: {}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
else:
# 绘制游戏结束界面
screen.blit(game_over.image, game_over.rect)
# 更新游戏界面
pygame.display.update()
# 以下是三个相关问题:
--相关问题--:
阅读全文