少帅下飞机(Python版本)基于pygame

最近在短视频平台上的少帅下飞机很火看到了很多java、C语言等好多版本。

今天给大家带来一个python版本。基于pygame库的 程序运行效果如下:

环境安装:python3.x版本 pygame库

安装pygame库:

pip install pygame

我的代码实现部分如下:

1. 初始化

首先,我们需要初始化Pygame,并创建一个窗口:

import pygame
import sys

# 初始化
pygame.init()
screen = pygame.display.set_mode((800, 800))
clock = pygame.time.Clock()
pygame.display.set_caption("张学良下飞机")  # 更改窗口标题

2. 加载图片 

下面是我用自己用的三张图片大家根据自己的图片来更改 :

飞机                       少帅                 卫兵1             卫兵2

# 加载并调整图片尺寸
plane_img = pygame.transform.scale(pygame.image.load('img/plane.jpg'), (128, 128))
shaoshaui_img = pygame.transform.scale(pygame.image.load('img/shaoshuai.png'), (64, 64))
weibing1_img = pygame.transform.scale(pygame.image.load('img/weibing1.png'), (64, 64))
weibing2_img = pygame.transform.scale(pygame.image.load('img/weibing2.png'), (64, 64))

3. 定义变量

# 定义位置
plane_x = 0
plane_y = 100
guard_y = 300
shao_visible = False
shao_y = 200  # 少帅的初始高度
falling = False
fall_speed = 25  # 每秒下降25个像素
falling_timer = 0  # 下落计时

4. 循环代码

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # 更新飞机位置
    plane_x += 1
    if plane_x > 800:
        plane_x = -1000000000  # 重新从屏幕左端出现

5. 下落逻辑

if not shao_visible and plane_x >= 500:
    shao_visible = True  # 显示少帅
    pygame.time.set_timer(pygame.USEREVENT, 1000)  # 设置定时器,1秒后触发

6. 使用blit绘制所有图片

screen.fill((255, 255, 255))  # 清屏
screen.blit(plane_img, (plane_x, plane_y))
screen.blit(weibing1_img, (400, guard_y))  # 卫兵1
screen.blit(weibing2_img, (600, guard_y))  # 卫兵2

if shao_visible:
    screen.blit(shaoshaui_img, (500, shao_y))  # 掉落的位置

pygame.display.flip()
clock.tick(60)  # 控制帧率

完整代码如下: 

import pygame
import sys

# 初始化
pygame.init()
screen = pygame.display.set_mode((800, 800))
clock = pygame.time.Clock()
pygame.display.set_caption("张学良下飞机")  # 更改窗口标题

# 加载并调整图片尺寸
plane_img = pygame.transform.scale(pygame.image.load('img/plane.jpg'), (128, 128))
shaoshaui_img = pygame.transform.scale(pygame.image.load('img/shaoshuai.png'), (64, 64))
weibing1_img = pygame.transform.scale(pygame.image.load('img/weibing1.png'), (64, 64))
weibing2_img = pygame.transform.scale(pygame.image.load('img/weibing2.png'), (64, 64))

# 定义位置
plane_x = 0
plane_y = 100
guard_y = 300
shao_visible = False
shao_y = 200  # 少帅的初始高度
falling = False
fall_speed = 25  # 每秒下降25个像素
falling_timer = 0  # 下落计时

# 游戏循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # 更新飞机位置
    plane_x += 1
    if plane_x > 800:
        plane_x = -1000000000  # 飞机飞出屏幕后,设置一个很小的值,使其重新从屏幕左端出现

    # 检查飞机是否到达 x = 500
    if not shao_visible and plane_x >= 500:
        shao_visible = True  # 显示少帅
        shao_y = 200  # 设置少帅初始位置
        pygame.time.set_timer(pygame.USEREVENT, 1000)  # 设置定时器,1秒后触发
        falling_timer = 0  # 重置计时器

    # 更新少帅的Y位置
    if shao_visible and shao_y < 301 :
        shao_y += 2  # 少帅向下移动

    # 处理定时器事件
    if event.type == pygame.USEREVENT:
        falling = True  # 开始同时下落
        pygame.time.set_timer(pygame.USEREVENT, 0)  # 停止定时器
    # 同时下落
    if falling:
        if guard_y < 800:  # 800 是最终位置
            guard_y += fall_speed / 60  # 按帧率计算下落速度
        if shao_y < 800:
            shao_y += fall_speed / 60

    # 绘制
    screen.fill((255, 255, 255))  # 清屏
    screen.blit(plane_img, (plane_x, plane_y))
    screen.blit(weibing1_img, (400, guard_y))  # 卫兵1
    screen.blit(weibing2_img, (600, guard_y))  # 卫兵2

    if shao_visible:
        screen.blit(shaoshaui_img, (500, shao_y))  # 掉落的位置

    pygame.display.flip()
    clock.tick(60)

欢迎大家在评论区分享你们的想法和建议,期待你的参与!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值