使用python制作一个双人飞机游戏
时间: 2023-12-21 14:05:52 浏览: 64
好的,下面是一个简单的双人飞机游戏的代码示例,你可以根据自己的需求进行修改和扩展:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
screen_width = 600
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption("双人飞机游戏")
# 加载游戏图片
player1_img = pygame.image.load("player1.png").convert_alpha()
player2_img = pygame.image.load("player2.png").convert_alpha()
enemy_img = pygame.image.load("enemy.png").convert_alpha()
bullet_img = pygame.image.load("bullet.png").convert_alpha()
background_img = pygame.image.load("background.png").convert_alpha()
# 设置游戏音效
pygame.mixer.music.load("background_music.mp3")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
shoot_sound = pygame.mixer.Sound("shoot.wav")
shoot_sound.set_volume(0.2)
explosion_sound = pygame.mixer.Sound("explosion.wav")
explosion_sound.set_volume(0.2)
# 定义游戏角色类
class Sprite(pygame.sprite.Sprite):
def __init__(self, img, x, y, speed):
super().__init__()
self.image = img
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = speed
def update(self):
self.rect.y += self.speed
class Player(Sprite):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
self.bullets = pygame.sprite.Group()
def move_left(self):
self.rect.x -= self.speed
if self.rect.x < 0:
self.rect.x = 0
def move_right(self):
self.rect.x += self.speed
if self.rect.x > screen_width - self.rect.width:
self.rect.x = screen_width - self.rect.width
def shoot(self):
bullet = Bullet(bullet_img, self.rect.centerx, self.rect.top, -10)
self.bullets.add(bullet)
shoot_sound.play()
class Enemy(Sprite):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
def update(self):
super().update()
if self.rect.y > screen_height:
self.kill()
class Bullet(Sprite):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
# 创建游戏角色
player1 = Player(player1_img, screen_width//4, screen_height - 100, 5)
player2 = Player(player2_img, screen_width*3//4, screen_height - 100, 5)
players = pygame.sprite.Group(player1, player2)
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group(players, enemies)
# 设置游戏帧率
clock = pygame.time.Clock()
FPS = 60
# 游戏循环
running = True
while running:
clock.tick(FPS)
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_a:
player1.move_left()
elif event.key == pygame.K_d:
player1.move_right()
elif event.key == pygame.K_w:
player1.shoot()
elif event.key == pygame.K_LEFT:
player2.move_left()
elif event.key == pygame.K_RIGHT:
player2.move_right()
elif event.key == pygame.K_UP:
player2.shoot()
# 生成敌机
if random.randint(1, 60) == 1:
enemy = Enemy(enemy_img, random.randint(0, screen_width - enemy_img.get_width()), -enemy_img.get_height(), random.randint(2, 6))
enemies.add(enemy)
all_sprites.add(enemy)
# 更新游戏角色和敌机位置
all_sprites.update()
# 处理碰撞
for player in players:
enemies_hit = pygame.sprite.spritecollide(player, enemies, True)
if enemies_hit:
explosion_sound.play()
for bullet in player1.bullets:
enemies_hit = pygame.sprite.spritecollide(bullet, enemies, True)
if enemies_hit:
explosion_sound.play()
bullet.kill()
for bullet in player2.bullets:
enemies_hit = pygame.sprite.spritecollide(bullet, enemies, True)
if enemies_hit:
explosion_sound.play()
bullet.kill()
# 绘制游戏界面
screen.blit(background_img, (0, 0))
all_sprites.draw(screen)
# 刷新游戏界面
pygame.display.flip()
# 退出游戏
pygame.quit()
```
在这个游戏中,每个玩家可以通过键盘控制自己的飞机进行左右移动和发射子弹。敌机会从上面不断飞来,玩家需要尽可能地躲避敌机和敌机发射的子弹,同时也要尽可能地击落敌机。在游戏中,如果玩家的飞机和敌机发生碰撞,会播放爆炸音效并结束游戏。
阅读全文