帮我用Python写一段飞机大战的游戏代码
时间: 2024-04-25 19:14:56 浏览: 113
用Python写一个飞机大战小游戏
3星 · 编辑精心推荐
好的,以下是一份简单的飞机大战游戏代码,使用Python Pygame库实现:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 游戏窗口大小
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
# 颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("飞机大战")
# 载入图片
bg_img = pygame.image.load("images/bg.jpg").convert()
player_img = pygame.image.load("images/hero.png").convert_alpha()
enemy1_img = pygame.image.load("images/enemy1.png").convert_alpha()
enemy2_img = pygame.image.load("images/enemy2.png").convert_alpha()
bullet_img = pygame.image.load("images/bullet.png").convert_alpha()
bomb_img = pygame.image.load("images/bomb.png").convert_alpha()
# 设置字体
font = pygame.font.Font(None, 36)
# 音效
pygame.mixer.music.load("sounds/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sounds/use_bomb.wav")
bomb_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sounds/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sounds/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sounds/enemy1_down.wav")
enemy1_down_sound.set_volume(0.1)
enemy2_down_sound = pygame.mixer.Sound("sounds/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
game_over_sound = pygame.mixer.Sound("sounds/game_over.wav")
game_over_sound.set_volume(0.2)
# 玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self, plane_img, plane_rect, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = plane_img
self.rect = plane_rect
self.rect.topleft = init_pos
self.speed = 8
self.bullets = pygame.sprite.Group()
self.is_hit = False
self.bomb_num = 3
self.bullet_num = 0
self.invincible_ticks = 0
def move_up(self):
if self.rect.top > 0:
self.rect.top -= self.speed
def move_down(self):
if self.rect.bottom < SCREEN_HEIGHT - 60:
self.rect.top += self.speed
def move_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
def move_right(self):
if self.rect.right < SCREEN_WIDTH:
self.rect.right += self.speed
def shoot(self):
bullet = Bullet(bullet_img, self.rect.midtop)
self.bullets.add(bullet)
bullet_sound.play()
def use_bomb(self):
if self.bomb_num > 0:
self.bomb_num -= 1
bomb_sound.play()
for enemy in enemies:
if enemy.rect.bottom > 0:
enemy.hit()
def get_bomb(self):
self.bomb_num += 1
get_bomb_sound.play()
def get_bullet(self):
self.bullet_num += 30
get_bullet_sound.play()
def update(self, invincible_ticks):
if invincible_ticks > 0:
self.invincible_ticks = invincible_ticks
else:
self.invincible_ticks = 0
if self.invincible_ticks % 2 == 0:
self.is_hit = False
screen.blit(self.image, self.rect)
else:
self.is_hit = True
# 敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self, enemy_img, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.topleft = init_pos
self.speed = random.randint(3, 5)
self.is_hit = False
self.down_ticks = 0
def move(self):
self.rect.top += self.speed
def hit(self):
self.is_hit = True
self.down_ticks = 10
def update(self):
if not self.is_hit:
self.move()
screen.blit(self.image, self.rect)
else:
if self.down_ticks == 10:
if self.image == enemy1_img:
enemy1_down_sound.play()
elif self.image == enemy2_img:
enemy2_down_sound.play()
self.down_ticks -= 1
if self.down_ticks == 0:
self.kill()
# 子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, bullet_img, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.midbottom = init_pos
self.speed = 10
def move(self):
self.rect.top -= self.speed
def update(self):
self.move()
if self.rect.bottom <= 0:
self.kill()
# 游戏结束函数
def game_over(score):
game_over_font = pygame.font.Font(None, 48)
game_over_text = game_over_font.render("游戏结束", True, WHITE)
game_over_rect = game_over_text.get_rect()
game_over_rect.centerx = screen.get_rect().centerx
game_over_rect.centery = screen.get_rect().centery - 60
screen.blit(game_over_text, game_over_rect)
score_font = pygame.font.Font(None, 36)
score_text = score_font.render("得分:" + str(score), True, WHITE)
score_rect = score_text.get_rect()
score_rect.centerx = screen.get_rect().centerx
score_rect.centery = screen.get_rect().centery
screen.blit(score_text, score_rect)
pygame.display.update()
pygame.mixer.music.stop()
game_over_sound.play()
pygame.time.delay(2000)
pygame.quit()
exit()
# 创建玩家飞机
player_rect = player_img.get_rect()
player_rect.topleft = [SCREEN_WIDTH // 2 - player_rect.width // 2, SCREEN_HEIGHT - player_rect.height - 60]
player = Player(player_img, player_rect, [SCREEN_WIDTH // 2 - player_rect.width // 2, SCREEN_HEIGHT - player_rect.height - 60])
# 创建敌机
enemies = pygame.sprite.Group()
enemies_down = pygame.sprite.Group()
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 500)
enemy1_down_imgs = []
for i in range(1, 6):
enemy1_down_imgs.append(pygame.image.load("images/enemy1_down" + str(i) + ".png").convert_alpha())
enemy2_down_imgs = []
for i in range(1, 6):
enemy2_down_imgs.append(pygame.image.load("images/enemy2_down" + str(i) + ".png").convert_alpha())
# 分数
score = 0
# 游戏主循环
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_UP:
player.move_up()
elif event.key == pygame.K_DOWN:
player.move_down()
elif event.key == pygame.K_LEFT:
player.move_left()
elif event.key == pygame.K_RIGHT:
player.move_right()
elif event.key == pygame.K_SPACE:
player.shoot()
elif event.key == pygame.K_b:
player.use_bomb()
elif event.type == ADDENEMY:
enemy_pos = [random.randint(0, SCREEN_WIDTH - enemy1_img.get_width()), 0]
enemy = Enemy(enemy1_img, enemy_pos)
enemies.add(enemy)
# 绘制背景
screen.blit(bg_img, (0, 0))
# 绘制玩家飞机
if not player.is_hit:
screen.blit(player.image, player.rect)
else:
player.invincible_ticks -= 1
if player.invincible_ticks == 0:
player.is_hit = False
# 绘制子弹
player.bullets.update()
player.bullets.draw(screen)
# 绘制敌机
enemies.update()
enemies.draw(screen)
# 碰撞检测
if pygame.sprite.spritecollide(player, enemies, False) and not player.is_hit:
player.is_hit = True
player.invincible_ticks = 60
for enemy in enemies:
enemy.hit()
if player.bomb_num > 0:
player.use_bomb()
else:
game_over(score)
for bullet in player.bullets:
if pygame.sprite.spritecollide(bullet, enemies, True):
if bullet.rect.bottom > 0:
bullet.kill()
score += 10
# 更新分数
score_text = font.render("得分:" + str(score), True, WHITE)
screen.blit(score_text, (10, 10))
# 获取道具
prop_type = random.randint(0, 1000)
if prop_type == 0:
prop_pos = [random.randint(0, SCREEN_WIDTH - bomb_img.get_width()), 0]
prop = Prop(bomb_img, prop_pos, 0)
props.add(prop)
elif prop_type == 1:
prop_pos = [random.randint(0, SCREEN_WIDTH - bullet_img.get_width()), 0]
prop = Prop(bullet_img, prop_pos, 1)
props.add(prop)
# 更新道具
props.update()
props.draw(screen)
# 更新屏幕
pygame.display.update()
# 游戏结束
if player.is_hit:
player.invincible_ticks -= 1
if player.invincible_ticks == 0:
player.is_hit = False
if player.bomb_num <= 0 and not pygame.sprite.spritecollide(player, props, True, pygame.sprite.collide_mask):
game_over(score)
```
请注意,此代码仅提供了一个基本的游戏框架,您可以在此基础上添加更多功能和改进。
阅读全文