Python实例题:pygame开发打飞机游戏

发布于:2025-05-13 ⋅ 阅读:(12) ⋅ 点赞:(0)

目录

Python实例题

题目

pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本

代码解释

初始化部分:

游戏主循环:

退出部分:

运行思路

注意事项

Python实例题

题目

pygame开发打飞机游戏

pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本

import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义屏幕尺寸
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 650

# 创建屏幕对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("打飞机游戏")

# 定义颜色
WHITE = (255, 255, 255)

# 加载玩家飞机图片
player_img = pygame.image.load("player.png")  # 请确保该图片存在
player_rect = player_img.get_rect()
player_rect.centerx = SCREEN_WIDTH // 2
player_rect.bottom = SCREEN_HEIGHT - 10

# 玩家飞机移动速度
player_speed = 5

# 加载敌机图片
enemy_img = pygame.image.load("enemy.png")  # 请确保该图片存在
enemies = []

# 敌机生成间隔和计时器
ENEMY_SPAWN_INTERVAL = 1000
enemy_spawn_timer = 0

# 加载子弹图片
bullet_img = pygame.image.load("bullet.png")  # 请确保该图片存在
bullets = []

# 子弹移动速度
bullet_speed = 8

# 游戏主循环
running = True
clock = pygame.time.Clock()

while running:
    # 控制游戏帧率
    clock.tick(60)

    # 处理事件
    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_rect = bullet_img.get_rect()
                bullet_rect.centerx = player_rect.centerx
                bullet_rect.top = player_rect.top
                bullets.append(bullet_rect)

    # 获取按键状态
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_rect.left > 0:
        player_rect.x -= player_speed
    if keys[pygame.K_RIGHT] and player_rect.right < SCREEN_WIDTH:
        player_rect.x += player_speed
    if keys[pygame.K_UP] and player_rect.top > 0:
        player_rect.y -= player_speed
    if keys[pygame.K_DOWN] and player_rect.bottom < SCREEN_HEIGHT:
        player_rect.y += player_speed

    # 生成敌机
    enemy_spawn_timer += clock.get_time()
    if enemy_spawn_timer > ENEMY_SPAWN_INTERVAL:
        enemy_rect = enemy_img.get_rect()
        enemy_rect.x = random.randint(0, SCREEN_WIDTH - enemy_rect.width)
        enemy_rect.y = -enemy_rect.height
        enemies.append(enemy_rect)
        enemy_spawn_timer = 0

    # 移动敌机
    for enemy in enemies[:]:
        enemy.y += 3
        if enemy.top > SCREEN_HEIGHT:
            enemies.remove(enemy)

    # 移动子弹
    for bullet in bullets[:]:
        bullet.y -= bullet_speed
        if bullet.bottom < 0:
            bullets.remove(bullet)

    # 检测子弹和敌机的碰撞
    for bullet in bullets[:]:
        for enemy in enemies[:]:
            if bullet.colliderect(enemy):
                bullets.remove(bullet)
                enemies.remove(enemy)

    # 绘制背景
    screen.fill(WHITE)

    # 绘制玩家飞机
    screen.blit(player_img, player_rect)

    # 绘制敌机
    for enemy in enemies:
        screen.blit(enemy_img, enemy)

    # 绘制子弹
    for bullet in bullets:
        screen.blit(bullet_img, bullet)

    # 更新显示
    pygame.display.flip()

# 退出 Pygame
pygame.quit()
    

代码解释

  • 初始化部分

    • 初始化pygame库,设置屏幕尺寸和标题。
    • 加载玩家飞机、敌机和子弹的图片,并设置玩家飞机的初始位置。
    • 定义敌机生成间隔和计时器,以及子弹的移动速度。
  • 游戏主循环

    • 控制游戏帧率为 60 帧每秒。
    • 处理事件,包括关闭窗口事件和发射子弹事件。
    • 根据按键状态移动玩家飞机。
    • 按一定间隔生成敌机,并移动敌机和子弹。
    • 检测子弹和敌机的碰撞,若碰撞则移除对应的子弹和敌机。
    • 绘制背景、玩家飞机、敌机和子弹,并更新显示。
  • 退出部分

    • 当用户关闭窗口时,退出pygame

运行思路

  • 安装依赖库:确保已经安装了pygame库,若未安装,可使用以下命令进行安装:
pip install pygame
  • 准备图片:准备好player.pngenemy.pngbullet.png三张图片,并将其放在与代码文件相同的目录下。
  • 运行脚本:将上述代码保存为aircraft_game.py文件,在终端中运行:
python aircraft_game.py
  • 开始游戏:使用上下左右键移动玩家飞机,按空格键发射子弹,尝试击落敌机。

注意事项

  • 请确保图片文件的路径和名称正确,否则会出现加载图片失败的错误。
  • 此代码只是一个简单的示例,你可以根据需求对游戏进行扩展,如添加音效、计分系统、关卡设计等。