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

目录

Python实例题

题目

[pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本](#pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本)

代码解释

初始化部分:

游戏主循环:

退出部分:

运行思路

注意事项

Python实例题

题目

pygame开发打飞机游戏

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

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

注意事项

  • 请确保图片文件的路径和名称正确,否则会出现加载图片失败的错误。
  • 此代码只是一个简单的示例,你可以根据需求对游戏进行扩展,如添加音效、计分系统、关卡设计等。
相关推荐
Hello world.Joey13 分钟前
数据挖掘入门-二手车交易价格预测
人工智能·python·数据挖掘·数据分析·conda·pandas
刘延林.17 分钟前
树莓5安装 PyCharm 进行python脚本开发
ide·python·pycharm
小洛~·~27 分钟前
多模态RAG与LlamaIndex——1.deepresearch调研
人工智能·python·深度学习·神经网络·chatgpt
monstercl38 分钟前
游戏资源传输服务器
运维·服务器·游戏
q_q王1 小时前
‌FunASR‌阿里开源的语音识别工具
python·大模型·llm·语音识别
不学无术の码农2 小时前
《Effective Python》第1章 Pythonic 思维总结——编写优雅、高效的 Python 代码
开发语言·python
zhou1852 小时前
MySQL保姆级安装教程(附资源包+5分钟极速配置+环境变量调试技巧)
java·python·mysql·php
lczdyx2 小时前
PNG转ico图标(支持圆角矩形/方形+透明背景)Python脚本 - 随笔
图像处理·python
lgily-12252 小时前
常用的设计模式详解
java·后端·python·设计模式
陈苏同学3 小时前
MPC控制器从入门到进阶(小车动态避障变道仿真 - Python)
人工智能·python·机器学习·数学建模·机器人·自动驾驶