用python写一个飞机大战
时间: 2023-12-02 08:02:19 浏览: 79
好的,下面是一个简单的飞机大战游戏的Python实现,希望能够帮助你入门:
```
import pygame
import random
# 初始化pygame
pygame.init()
# 屏幕尺寸
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
# 创建屏幕对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置标题
pygame.display.set_caption("飞机大战")
# 加载图片
bg_img = pygame.image.load("images/bg.jpg")
hero_img = pygame.image.load("images/hero.png")
enemy_img = pygame.image.load("images/enemy.png")
bullet_img = pygame.image.load("images/bullet.png")
# 设置背景音乐
pygame.mixer.music.load("sounds/game_music.ogg")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)
# 设置游戏音效
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
bullet_sound.set_volume(0.5)
enemy_down_sound = pygame.mixer.Sound("sounds/enemy_down.wav")
enemy_down_sound.set_volume(0.5)
game_over_sound = pygame.mixer.Sound("sounds/game_over.wav")
game_over_sound.set_volume(0.5)
# 设置字体
font = pygame.font.Font(None, 36)
# 定义游戏状态常量
GAME_READY = 0
GAME_PLAYING = 1
GAME_OVER = 2
# 定义游戏状态变量
game_state = GAME_READY
# 定义英雄飞机类
class Hero(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = hero_img
self.rect = self.image.get_rect()
self.rect.centerx = SCREEN_WIDTH // 2
self.rect.bottom = SCREEN_HEIGHT - 20
self.speed = 6
self.bullets = pygame.sprite.Group()
def update(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT]:
self.rect.x -= self.speed
if self.rect.left < 0:
self.rect.left = 0
elif key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
def fire(self):
bullet = Bullet()
bullet.rect.centerx = self.rect.centerx
bullet.rect.bottom = self.rect.top
self.bullets.add(bullet)
# 定义敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_img
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, -10)
self.speed = random.randint(2, 6)
def update(self):
self.rect.y += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.kill()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
self.speed = -10
def update(self):
self.rect.y += self.speed
if self.rect.bottom < 0:
self.kill()
# 创建英雄飞机对象
hero = Hero()
# 创建敌机精灵组
enemies = pygame.sprite.Group()
# 创建子弹精灵组
bullets = pygame.sprite.Group()
# 创建敌机生成事件
ENEMY_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(ENEMY_EVENT, 1000)
# 游戏循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if game_state == GAME_PLAYING:
hero.fire()
elif event.type == ENEMY_EVENT:
if game_state == GAME_PLAYING:
enemy = Enemy()
enemies.add(enemy)
# 绘制背景
screen.blit(bg_img, (0, 0))
# 绘制英雄飞机
if game_state == GAME_PLAYING:
hero.update()
bullets.update()
enemies.update()
screen.blit(hero.image, hero.rect)
bullets.draw(screen)
enemies.draw(screen)
# 碰撞检测
for enemy in pygame.sprite.spritecollide(hero, enemies, True):
game_state = GAME_OVER
game_over_sound.play()
for bullet in pygame.sprite.groupcollide(bullets, enemies, True, True):
enemy_down_sound.play()
# 显示得分
score_text = font.render("得分:%d" % len(enemies), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 绘制游戏准备提示
elif game_state == GAME_READY:
ready_text = font.render("按任意键开始游戏", True, (255, 255, 255))
screen.blit(ready_text, (SCREEN_WIDTH // 2 - ready_text.get_width() // 2, SCREEN_HEIGHT // 2 - ready_text.get_height() // 2))
# 绘制游戏结束提示
elif game_state == GAME_OVER:
over_text = font.render("游戏结束,得分:%d" % len(enemies), True, (255, 255, 255))
screen.blit(over_text, (SCREEN_WIDTH // 2 - over_text.get_width() // 2, SCREEN_HEIGHT // 2 - over_text.get_height() // 2))
# 更新屏幕
pygame.display.update()
# 设置帧率
pygame.time.Clock().tick(60)
```
这个代码实现的是一个简单的飞机大战游戏,包括英雄飞机、敌机、子弹等元素,并且有游戏准备、游戏进行、游戏结束等状态。你可以根据自己的需要进行修改和扩展。
阅读全文