python 双人飞机游戏
时间: 2023-09-21 09:06:16 浏览: 103
python飞机游戏
好的,双人飞机游戏需要用到 Pygame 模块,你需要先确保已经安装了 Pygame。
下面是一个简单的双人飞机游戏的代码示例:
```python
import pygame
# 初始化 Pygame
pygame.init()
# 定义屏幕大小和颜色
screen_width = 480
screen_height = 700
bg_color = (230, 230, 230)
# 创建屏幕对象
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置窗口标题
pygame.display.set_caption("双人飞机大战")
# 加载背景音乐
pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.play(-1)
# 加载游戏音效
bullet_sound = pygame.mixer.Sound("bullet.wav")
enemy_down_sound = pygame.mixer.Sound("enemy_down.wav")
game_over_sound = pygame.mixer.Sound("game_over.wav")
# 加载游戏图片
background = pygame.image.load("background.png").convert()
player1_img = pygame.image.load("player1.png").convert_alpha()
player2_img = pygame.image.load("player2.png").convert_alpha()
bullet_img = pygame.image.load("bullet.png").convert_alpha()
enemy1_img = pygame.image.load("enemy1.png").convert_alpha()
enemy2_img = pygame.image.load("enemy2.png").convert_alpha()
enemy3_img = pygame.image.load("enemy3.png").convert_alpha()
enemy4_img = pygame.image.load("enemy4.png").convert_alpha()
enemy_imgs = [enemy1_img, enemy2_img, enemy3_img, enemy4_img]
# 定义玩家类
class Player(pygame.sprite.Sprite):
def __init__(self, img, x, y):
super().__init__()
self.image = img
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 5
self.bullets = pygame.sprite.Group()
def move_left(self):
if self.rect.x > 0:
self.rect.x -= self.speed
def move_right(self):
if self.rect.x < screen_width - self.rect.width:
self.rect.x += self.speed
def move_up(self):
if self.rect.y > 0:
self.rect.y -= self.speed
def move_down(self):
if self.rect.y < screen_height - self.rect.height:
self.rect.y += self.speed
def fire(self):
bullet = Bullet(bullet_img, self.rect.x + self.rect.width / 2 - 10, self.rect.y)
self.bullets.add(bullet)
bullet_sound.play()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, img, x, y):
super().__init__()
self.image = img
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = 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, img, x, y):
super().__init__()
self.image = img
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 3
self.health = 10
def update(self):
self.rect.y += self.speed
if self.rect.top > screen_height:
self.kill()
# 创建玩家对象
player1 = Player(player1_img, 100, 500)
player2 = Player(player2_img, 300, 500)
# 创建敌机组
enemies = pygame.sprite.Group()
# 创建时钟对象
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_a:
player1.move_left()
elif event.key == pygame.K_d:
player1.move_right()
elif event.key == pygame.K_w:
player1.move_up()
elif event.key == pygame.K_s:
player1.move_down()
elif event.key == pygame.K_SPACE:
player1.fire()
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.move_up()
elif event.key == pygame.K_DOWN:
player2.move_down()
elif event.key == pygame.K_RETURN:
player2.fire()
# 更新玩家和子弹状态
player1.bullets.update()
player2.bullets.update()
enemies.update()
# 检测子弹与敌机的碰撞
for bullet in player1.bullets:
hits = pygame.sprite.spritecollide(bullet, enemies, True)
for hit in hits:
player1.bullets.remove(bullet)
enemy_down_sound.play()
for bullet in player2.bullets:
hits = pygame.sprite.spritecollide(bullet, enemies, True)
for hit in hits:
player2.bullets.remove(bullet)
enemy_down_sound.play()
# 检测玩家与敌机的碰撞
hits = pygame.sprite.spritecollide(player1, enemies, True)
if hits:
game_over_sound.play()
game_over = True
hits = pygame.sprite.spritecollide(player2, enemies, True)
if hits:
game_over_sound.play()
game_over = True
# 生成新的敌机
if len(enemies) < 10:
enemy = Enemy(enemy_imgs[pygame.randint(0, 3)], pygame.randint(0, screen_width - 50), -50)
enemies.add(enemy)
# 绘制游戏元素
screen.blit(background, (0, 0))
screen.blit(player1.image, player1.rect)
screen.blit(player2.image, player2.rect)
player1.bullets.draw(screen)
player2.bullets.draw(screen)
enemies.draw(screen)
# 刷新屏幕
pygame.display.update()
# 设置帧率
clock.tick(60)
# 退出 Pygame
pygame.quit()
```
你可以根据自己的需要,对游戏进行调整和扩展。
阅读全文