给我用python写一个飞机大战小游戏
时间: 2023-12-07 16:31:34 浏览: 106
用Python写一个飞机大战小游戏
3星 · 编辑精心推荐
好的,以下是Python的飞机大战小游戏示例代码:
```
import pygame
import random
# 初始化 pygame
pygame.init()
# 创建游戏窗口
width, height = 480, 700
screen = pygame.display.set_mode((width, height))
# 设置游戏窗口标题
pygame.display.set_caption("飞机大战")
# 载入游戏背景图像
bg_img = pygame.image.load("images/background.png")
# 载入游戏音乐
pygame.mixer.music.load("sounds/game_music.ogg")
pygame.mixer.music.play(-1) # 循环播放
# 载入游戏音效
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
enemy1_down_sound = pygame.mixer.Sound("sounds/enemy1_down.wav")
game_over_sound = pygame.mixer.Sound("sounds/game_over.wav")
# 字体设置
font = pygame.font.Font(None, 36)
gameover_font = pygame.font.Font(None, 48)
# 定义颜色变量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("images/player.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.left = (width - self.rect.width) // 2
self.rect.top = height - self.rect.height - 50
self.speed = 5
self.bullets = pygame.sprite.Group()
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and self.rect.top > 0:
self.rect.top -= self.speed
if keys[pygame.K_DOWN] and self.rect.bottom < height:
self.rect.bottom += self.speed
if keys[pygame.K_LEFT] and self.rect.left > 0:
self.rect.left -= self.speed
if keys[pygame.K_RIGHT] and self.rect.right < width:
self.rect.right += self.speed
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
self.bullets.add(bullet)
bullet_sound.play()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.image.load("images/bullet.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed = -10
def update(self):
self.rect.top += self.speed
if self.rect.bottom < 0:
self.kill()
# 定义敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("images/enemy1.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.left = random.randint(0, width - self.rect.width)
self.rect.bottom = 0
self.speed = random.randint(2, 6)
def update(self):
self.rect.top += self.speed
if self.rect.top > height:
self.kill()
# 创建精灵组
all_sprites = pygame.sprite.Group()
player = Player()
enemies = pygame.sprite.Group()
# 将玩家飞机和敌机加入精灵组
all_sprites.add(player)
# 设置游戏时钟
clock = pygame.time.Clock()
# 设置游戏结束标志位
game_over = False
# 游戏主循环
while not game_over:
# 事件循环
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
# 更新所有精灵
all_sprites.update()
# 检测子弹是否击中敌机
hits = pygame.sprite.groupcollide(player.bullets, enemies, True, True)
for hit in hits:
enemy1_down_sound.play()
enemies.add(Enemy())
# 检测玩家飞机是否撞上敌机
hits = pygame.sprite.spritecollide(player, enemies, True)
if hits:
game_over_sound.play()
game_over = True
# 生成新的敌机
if len(enemies) < 5:
enemies.add(Enemy())
# 绘制游戏画面
screen.blit(bg_img, (0, 0))
all_sprites.draw(screen)
for enemy in enemies:
screen.blit(enemy.image, enemy.rect)
score_txt = font.render("Score: {}".format(len(player.bullets)), True, WHITE)
screen.blit(score_txt, (10, 10))
# 更新窗口内容
pygame.display.update()
# 限制游戏帧数为60帧
clock.tick(60)
# 游戏结束
gameover_txt = gameover_font.render("Game Over", True, RED)
screen.blit(gameover_txt, ((width - gameover_txt.get_width()) // 2, (height - gameover_txt.get_height()) // 2))
pygame.display.update()
# 等待2秒
pygame.time.wait(2000)
# 退出游戏
pygame.quit()
```
提示:代码中需要用到一些图片和音频文件,可以按照代码中指定的路径放置文件。也可以自行调整路径。
阅读全文