飞机大战是一个经典的小游戏,通常由玩家控制一个飞机,目标是摧毁尽可能多的敌机,并躲避敌机的攻击。下面是一个简单的Python飞机大战游戏的示例,使用了pygame
库来创建游戏界面和处理游戏逻辑。
效果图
首先,确保你已经安装了pygame
库。如果没有安装,可以通过以下命令安装:
pip install pygame
接下来是飞机大战游戏的代码:
import pygame
import random
# 初始化pygame
pygame.init()
# 设置屏幕尺寸
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640
# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 创建屏幕对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置游戏标题
pygame.display.set_caption('飞机大战')
# 加载飞机图片并缩放
player_img = pygame.image.load('player.png').convert_alpha()
player_img = pygame.transform.scale(player_img, (int(player_img.get_width() * 0.5), int(player_img.get_height() * 0.5)))
player_x = SCREEN_WIDTH / 2 - player_img.get_width() / 2
player_y = SCREEN_HEIGHT - player_img.get_height() - 10
player_speed = 5
# 加载敌机图片并缩放
enemy_img = pygame.image.load('enemy.png').convert_alpha()
enemy_img = pygame.transform.scale(enemy_img, (int(enemy_img.get_width() * 0.5), int(enemy_img.get_height() * 0.5)))
enemy_x = random.randint(0, SCREEN_WIDTH - enemy_img.get_width())
enemy_y = random.randint(50, 150)
enemy_speed = 2 # 减慢敌机的移动速度
# 初始化子弹列表
bullets = []
# 子弹属性
bullet_speed = 10
bullet_width = 4
bullet_height = 10
# 游戏循环标志
running = True
# 敌机得分
score = 0
# 函数:绘制子弹
def draw_bullets(bullet_list):
for bullet in bullet_list:
pygame.draw.rect(screen, WHITE, (bullet[0], bullet[1], bullet_width, bullet_height))
# 函数:更新子弹位置
def update_bullets(bullet_list):
for bullet in bullet_list[:]:
bullet[1] -= bullet_speed
if bullet[1] < 0:
bullet_list.remove(bullet)
# 函数:检测子弹和敌机的碰撞
def check_collision(bullet, enemy):
bullet_rect = pygame.Rect(bullet[0], bullet[1], bullet_width, bullet_height)
enemy_rect = pygame.Rect(enemy[0], enemy[1], enemy_img.get_width(), enemy_img.get_height())
return bullet_rect.colliderect(enemy_rect)
# 游戏主循环
while running:
# 填充屏幕背景色
screen.fill(BLACK)
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# 发射子弹
bullet = [player_x + player_img.get_width() / 2 - bullet_width / 2, 0]
bullets.append(bullet)
# 检测按键
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < SCREEN_WIDTH - player_img.get_width():
player_x += player_speed
# 飞机移动边界控制
player_x = max(0, min(player_x, SCREEN_WIDTH - player_img.get_width()))
# 敌机移动
enemy_y += enemy_speed
if enemy_y > SCREEN_HEIGHT:
score -= 10 # 敌机逃脱减少得分
enemy_x = random.randint(0, SCREEN_WIDTH - enemy_img.get_width())
enemy_y = random.randint(50, 150)
# 绘制飞机和敌机
screen.blit(player_img, (player_x, player_y))
screen.blit(enemy_img, (enemy_x, enemy_y))
# 更新子弹位置并绘制
update_bullets(bullets)
draw_bullets(bullets)
# 检测子弹和敌机的碰撞
for bullet in bullets[:]:
if check_collision([bullet[0], bullet[1]], [enemy_x, enemy_y]):
score += 10 # 增加得分
bullets.remove(bullet)
break # 每次只处理一次碰撞
# 更新屏幕显示
pygame.display.update()
# 显示得分
font = pygame.font.SysFont(None, 35)
score_text = font.render('Score: ' + str(score), True, WHITE)
screen.blit(score_text, (10, 10))
# 退出游戏
pygame.quit()
在这个示例中,我们首先初始化了pygame
库并设置了屏幕的尺寸和标题。然后,我们加载了飞机和敌机的图片,并设置了它们的初始位置和速度。
在游戏的主循环中,我们处理了事件(例如退出游戏),检测了按键输入以控制飞机的移动,并更新了敌机的位置。如果敌机飞出了屏幕,我们会重新生成它的位置。
最后,我们在屏幕上绘制飞机和敌机,并更新屏幕显示。当游戏退出时,我们调用pygame.quit()
来清理资源。
请注意,这个代码示例假设你有一个名为player.png
的飞机图片和一个名为enemy.png
的敌机图片,并将它们放在了与代码相同的目录下。你需要将这些图片替换为你自己的图片,或者更改代码中的图片路径以匹配你的文件位置。
这个简单的飞机大战游戏可以作为一个很好的起点,你可以在此基础上添加更多的功能,比如子弹射击、碰撞检测、得分系统、音效和更多种类的敌机等。