当前位置: 首页 > news >正文

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

目录

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 = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:# 发射子弹bullet_rect = bullet_img.get_rect()bullet_rect.centerx = player_rect.centerxbullet_rect.top = player_rect.topbullets.append(bullet_rect)# 获取按键状态keys = pygame.key.get_pressed()if keys[pygame.K_LEFT] and player_rect.left > 0:player_rect.x -= player_speedif keys[pygame.K_RIGHT] and player_rect.right < SCREEN_WIDTH:player_rect.x += player_speedif keys[pygame.K_UP] and player_rect.top > 0:player_rect.y -= player_speedif 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.heightenemies.append(enemy_rect)enemy_spawn_timer = 0# 移动敌机for enemy in enemies[:]:enemy.y += 3if enemy.top > SCREEN_HEIGHT:enemies.remove(enemy)# 移动子弹for bullet in bullets[:]:bullet.y -= bullet_speedif 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
  • 开始游戏:使用上下左右键移动玩家飞机,按空格键发射子弹,尝试击落敌机。

注意事项

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

相关文章:

  • 【LeetCode】49.字母异位词分组
  • leetcode 18. 四数之和
  • 【Linux】进程状态、优先级、切换和调度
  • 三、transformers基础组件之Model
  • 判断一个数组有没有重复值
  • PID与模糊PID系统设计——基于模糊PID的水下航行器运动控制研究Simulink仿真(包含设计报告)
  • 基于STM32、HAL库的BMP388 气压传感器 驱动程序设计
  • Blender 入门教程(一):模型创建
  • vue-pdf-embed预览PDF
  • 基于SpringBoot的校园周边美食探索及分享平台【附源码+数据库+文档下载】
  • 什么是内存刷新
  • 视频翻译软件有哪些?推荐5款视频翻译工具[特殊字符][特殊字符]
  • 各类型和字节数组互相转换
  • #将一个 .c 文件转变为可直接运行的文件过程及原理
  • 处理均值的配对比较
  • 【记录nginx请求头参数丢失问题】
  • TongWeb7.0常用-D参数说明
  • 企业级商城系统容器化部署技术方案
  • 产品文档撰写--金字塔原理--实战3
  • Linux系统管理与编程18:自动化部署dhcp服务
  • 马上评丨75万采购300元设备,仅仅终止采购还不够
  • 全球医药股普跌,A股创新药板块下挫
  • ​中国超大规模市场是信心所在——海南自贸港建设一线观察
  • 印巴战火LIVE丨“快速接近战争状态”?印度袭击巴军事基地,巴启动反制军事行动
  • 警方通报男子地铁上拍视频致乘客恐慌受伤:列车运行一度延误,已行拘
  • 罕见沙尘再度入川,官方:沙尘传输高度达到平流层,远超以往