#头条创作挑战赛#
飞机大战是一款经典的游戏,玩家控制一个小飞机,击败敌机和boss飞机来获取高分。下面是一个使用Python语言实现的简单飞机大战游戏的代码示例:import pygame
import random# 初始化 pygame
pygameit()# 设置游戏窗口大小
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))# 设置游戏标题
pygame.display.set_caption("飞机大战")# 加载背景图片
background = pygame.image.load('background.png')# 加载玩家飞机图片
player_img=pygame.image.load('player.png')
player_rect=player_img.get_rect()
player_rect.midbottom = (screen_width / 2, screen_height - 10)# 加载敌机图片
enemy_img = pygame.image.load('enemy.png')# 加载boss飞机图片
boss_img = pygame.image.load('boss.png')# 设置玩家飞机的移动速度
player_speed = 5# 创建敌机列表
enemies = []enemy_speed = 2# 创建boss飞机
boss = Noneboss_speed = 1# 创建子弹列表
bullets = []bullet_speed = 5# 设置游戏帧率
clock = pygame.time.Clock()frame_rate = 60# 设置游戏得分和生命值
score = 0life = 3# 设置字体
font = pygame.font.Font(None, 36)# 循环更新游戏状态
running = Truewhile running:
# 设置帧率 clock.tick(frame_rate) # 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # 移动玩家飞机
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_rect.x -= player_speed
if keys[pygame.K_RIGHT]:
player_rect.x += player_speed
if keys[pygame.K_UP]:
player_rect.y -= player_speed
if keys[pygame.K_DOWN]:
player_rect.y += player_speed # 碰撞检测
for enemy in enemies:
if enemylliderect(player_rect):
enemies.remove(enemy) life -= 1 if boss and player_rectlliderect(boss):
life -= 1 for bullet in bullets:
if boss and bulletlliderect(boss):
bullets.remove(bullet) score += 10 for bullet in bullets:
for enemy in enemies:
if bulletlliderect(enemy):
bullets.remove(bullet)
enemies.remove(enemy)
score += 1 # 生成敌机
if len(enemies) < 5:
enemy = enemy_img.get_rect()
enemy.x=random.randint(0,screen_width- enemy.width)
enemy.y = random.randint(-50, -10)
enemiesend(enemy) # 移动敌机
for enemy in enemies:
enemy.y += enemy_speed # 生成boss飞机
if score >= 50 and not boss:
boss = boss_img.get_rect()
boss.midtop = (screen_width / 2, -100) # 移动boss飞机
if boss:
if boss.y <= 0:
boss.y += boss_speed
else:
boss.x += random.choice([-1, 1])
if boss.left < 0 or boss.right > screen_width:
boss.x -= random.choice([-1, 1]) # 生成子弹
if keys[pygame.K_SPACE]:
bullet = pygame.Rect(player_rect.midtop[0], player_rect.midtop[1], 5, 10)
bulletsend(bullet) # 移动子弹
for bullet in bullets:
bullet.y -= bullet_speed # 绘制背景
screen.blit(background, (0, 0)) # 绘制玩家飞机
screen.blit(player_img, player_rect) # 绘制敌机
for enemy in enemies: screen.blit(enemy_img, enemy) # 绘制boss飞机
if boss: screen.blit(boss_img, boss) # 绘制子弹
for bullet in bullets: pygame.draw.rect(screen, (255, 0, 0), bullet) # 绘制得分和生命值
score_text = fontder("Score: " + str(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
life_text = fontder("Life: " + str(life), True, (255, 255, 255))
screen.blit(life_text, (10, 50)) # 刷新屏幕
pygame.display.flip()# 退出游戏
pygame.quit()
上述代码使用pygame库来实现飞机大战游戏,在游戏窗口中加载背景图片、玩家飞机、敌机和boss飞机的图片,并设置了一些游戏状态变量,如分数和生命值等。游戏主循环中,首先处理玩家飞机的移动控制和碰撞检测,然后生成敌机和boss飞机,并移动它们。同时,判断是否按下空格键发射子弹,并移动子弹。最后,绘制游戏窗口中的各种元素,如背景、玩家飞机、敌机、boss飞机、子弹、得分和生命值等。