写一个雷霆战机的python小游戏
时间: 2024-05-29 17:04:45 浏览: 187
这是一个简单的雷霆战机小游戏,使用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("雷霆战机")
# 加载游戏背景
background = pygame.image.load("background.png").convert()
# 加载玩家飞机
player_img = pygame.image.load("player.png").convert()
player_img.set_colorkey((255, 255, 255)) # 设置透明色
player_rect = player_img.get_rect()
player_rect.centerx = screen_width // 2
player_rect.bottom = screen_height - 10
# 设置玩家飞机的移动速度
player_speed = 5
# 加载敌机
enemy_imgs = [
pygame.image.load("enemy1.png").convert(),
pygame.image.load("enemy2.png").convert(),
pygame.image.load("enemy3.png").convert(),
]
for enemy_img in enemy_imgs:
enemy_img.set_colorkey((255, 255, 255))
enemy_rects = []
for i in range(10):
enemy_img = random.choice(enemy_imgs)
enemy_rect = enemy_img.get_rect()
enemy_rect.centerx = random.randint(0, screen_width)
enemy_rect.bottom = random.randint(-1000, -100)
enemy_rects.append(enemy_rect)
# 设置敌机的移动速度
enemy_speed = 3
# 加载子弹
bullet_img = pygame.image.load("bullet.png").convert()
bullet_img.set_colorkey((255, 255, 255))
bullet_rect = bullet_img.get_rect()
# 设置子弹的移动速度
bullet_speed = 10
# 记录玩家得分
score = 0
# 加载游戏音效
shoot_sound = pygame.mixer.Sound("shoot.wav")
explode_sound = pygame.mixer.Sound("explode.wav")
# 设置游戏循环
running = True
while running:
# 处理游戏事件
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
# 处理玩家控制
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_rect.left > 0:
player_rect.move_ip(-player_speed, 0)
if keys[pygame.K_RIGHT] and player_rect.right < screen_width:
player_rect.move_ip(player_speed, 0)
if keys[pygame.K_UP] and player_rect.top > 0:
player_rect.move_ip(0, -player_speed)
if keys[pygame.K_DOWN] and player_rect.bottom < screen_height:
player_rect.move_ip(0, player_speed)
if keys[pygame.K_SPACE]:
# 发射子弹
bullet_rect.centerx = player_rect.centerx
bullet_rect.bottom = player_rect.top
shoot_sound.play()
# 移动敌机
for enemy_rect in enemy_rects:
enemy_rect.move_ip(0, enemy_speed)
if enemy_rect.top > screen_height:
enemy_rect.centerx = random.randint(0, screen_width)
enemy_rect.bottom = random.randint(-1000, -100)
# 移动子弹
bullet_rect.move_ip(0, -bullet_speed)
# 检测子弹是否击中敌机
for enemy_rect in enemy_rects:
if bullet_rect.colliderect(enemy_rect):
enemy_rect.centerx = random.randint(0, screen_width)
enemy_rect.bottom = random.randint(-1000, -100)
bullet_rect.bottom = player_rect.top
score += 1
explode_sound.play()
# 绘制游戏界面
screen.blit(background, (0, 0))
screen.blit(player_img, player_rect)
for enemy_rect, enemy_img in zip(enemy_rects, enemy_imgs):
screen.blit(enemy_img, enemy_rect)
screen.blit(bullet_img, bullet_rect)
font = pygame.font.Font(None, 36)
score_text = font.render("Score: {}".format(score), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 更新游戏界面
pygame.display.update()
# 结束游戏
pygame.quit()
```
游戏玩法:
玩家通过键盘控制飞机移动,空格键发射子弹。敌机会从屏幕上方随机出现并向下移动,玩家需要控制飞机躲避敌机并尽可能地击落它们。当子弹击中敌机时,敌机会随机出现在屏幕上方并加分。游戏结束条件为玩家飞机被敌机撞击。
阅读全文