当前位置: 代码网 > it编程>前端脚本>Python > Python实例题之pygame开发打飞机游戏实例代码

Python实例题之pygame开发打飞机游戏实例代码

2025年06月13日 Python 我要评论
题目pygame开发打飞机游戏pygame-aircraft-game使用 pygame 开发的打飞机游戏脚本import pygameimport random# 初始化 pygamepygame.

题目

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
  • 开始游戏:使用上下左右键移动玩家飞机,按空格键发射子弹,尝试击落敌机。

注意事项

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

到此这篇关于python实例题之pygame开发打飞机游戏的文章就介绍到这了,更多相关python pygame打飞机游戏内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com