pygame写飞机大战
时间: 2023-08-05 08:03:09 浏览: 91
好的,这里是一个简单的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("飞机大战")
# 加载背景图片
background = pygame.image.load("images/background.png").convert()
# 加载游戏音效
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
enemy_down_sound = pygame.mixer.Sound("sound/enemy_down.wav")
enemy_down_sound.set_volume(0.2)
game_over_sound = pygame.mixer.Sound("sound/game_over.wav")
game_over_sound.set_volume(0.2)
# 加载玩家飞机图片
player_image = pygame.image.load("images/player.png").convert_alpha()
# 加载子弹图片
bullet_image = pygame.image.load("images/bullet.png").convert_alpha()
# 加载敌机图片
enemy_images = [
pygame.image.load("images/enemy1.png").convert_alpha(),
pygame.image.load("images/enemy2.png").convert_alpha(),
pygame.image.load("images/enemy3.png").convert_alpha(),
]
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = player_image
self.rect = self.image.get_rect()
self.rect.midbottom = (screen_width // 2, screen_height - 30)
self.speed = 8
self.bullets = pygame.sprite.Group()
def update(self, keys):
if keys[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT] and self.rect.right < screen_width:
self.rect.x += 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 = bullet_image
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 Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = random.choice(enemy_images)
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, screen_width - self.rect.width)
self.rect.y = random.randint(-200, -50)
self.speed = random.randint(1, 4)
self.health = 1
def update(self):
self.rect.y += self.speed
if self.rect.top > screen_height:
self.kill()
# 创建游戏对象
player = Player()
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
# 设置游戏主循环
clock = pygame.time.Clock()
score = 0
game_over = False
while not game_over:
clock.tick(60)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# 检测玩家按键
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
player.shoot()
# 更新玩家和子弹
player.update(keys)
player.bullets.update()
# 检测子弹和敌机碰撞
for bullet in player.bullets:
enemies_hit = pygame.sprite.spritecollide(bullet, enemies, True)
for enemy in enemies_hit:
enemy_down_sound.play()
score += 100
bullet.kill()
# 更新敌机
if random.randint(1, 30) == 1:
enemy = Enemy()
enemies.add(enemy)
all_sprites.add(enemy)
enemies.update()
# 检测玩家与敌机碰撞
if pygame.sprite.spritecollide(player, enemies, True):
game_over_sound.play()
game_over = True
# 绘制游戏界面
screen.blit(background, (0, 0))
all_sprites.draw(screen)
# 显示得分
font = pygame.font.SysFont(None, 36)
score_text = font.render("得分: {}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.update()
# 游戏结束
pygame.quit()
```
你可以在代码中看到,程序使用了 Pygame 绘图模块来创建游戏窗口、加载图片和播放音效。游戏中有三个主要的类:Player、Bullet和Enemy。其中,Player类表示玩家飞机,Bullet类表示子弹,Enemy类表示敌机。游戏主循环中,程序不断更新玩家和子弹位置,检测子弹和敌机碰撞,更新敌机位置,检测玩家与敌机碰撞,绘制游戏界面,并在游戏结束时退出 Pygame。
阅读全文