学习Python开发小游戏(三)-----勇闯地下一百层

该博客介绍了一个Python游戏开发项目。需安装pygame、pgzero、numpy。游戏功能包括初始化界面显示砖块和角色图片,可通过键盘控制角色移动,角色不在砖块上会下降,在砖块上移动有行走效果,能统计闯过层数并计分,游戏失败会显示信息并停止。代码素材来自相关书籍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提条件:

        需要安装pygame,pgzero,numpy(windows:1.19.3)

功能:

       1.初始化界面显示5个砖块和角色图片

       2.键盘控制角色的左右移动,当角色不在砖块上时显示角色下降

       3.角色在砖块上移动时显示角色行走的效果

       4.统计角色闯过的层数,每闯过一层分数加1,同时在界面显示分数

       5.游戏失败,界面显示对应信息,同时停止游戏

附:

       代码中涉及到的素材来自异步社区:《Python游戏趣味编程》一书中提供的素材:https://www.epubit.com/bookDetails?id=UB72096d97d6149

以下是代码:

import pgzrun, random

TITLE = '勇闯地下一百层'
WIDTH = 600
HEIGHT = 800

# 导入角色
roles = []
for i in range(10):
    roles.append(Actor(str(i + 1)))
# 初始化显示的角色的图片
role_index = 4
# 初始化角色显示的位置
role_x = WIDTH / 2
role_y1 = HEIGHT / 4
for r in roles:
    r.x = role_x
    r.y = role_y1
# 导入砖块,初始化5个砖块
bricks = []
for i in range(5):
    brick = Actor('brick')
    brick.pos = random.randint(180, 420), 150 * (i + 1)
    bricks.append(brick)

# 移动速度
speed_role = 5  # 角色下降速度
speed_brick = 1  # 砖块上升速度
speed_go = 0  # 角色自身移动的速度
# 游戏是否结束
is_loose = False
# 分数
score = 0
# 角色的上一帧的y坐标
role_y = roles[role_index].y


def draw():
    # 清除游戏画面
    screen.clear()
    screen.fill('white')
    # 绘制角色
    roles[role_index].draw()
    # 绘制砖块
    for bk in bricks:
        bk.draw()
    # 当前得分
    screen.draw.text('当前得分:' + str(score), (200, 750), fontsize=30, fontname='s', color='green')

    if is_loose:
        screen.draw.text('Game Over !', (150, 300), fontsize=50, fontname='s', color='red')


def update():
    global is_loose, speed_role, speed_brick, score, role_y, role_index, role_x, role_y1, speed_go
    # 角色是否站在砖块上
    is_player_on_brick = False
    # 游戏失败直接返回
    if is_loose:
        return

    # 角色和砖块的位置
    for bk in bricks:
        # 砖块重复出现
        if bk.y < 0:
            bk.pos = random.randint(180, 420), HEIGHT
        # 角色是否站在砖块上
        if abs(
                roles[role_index].bottom - bk.top) < 5 and bk.left - roles[role_index].left < roles[
            role_index].width * 2 / 3 and roles[role_index].right - bk.right < roles[role_index].width * 2 / 3:
            is_player_on_brick = True
            # 角色站在砖块上时,角色和砖块一块上移
            for i in range(len(roles)):
                roles[i].bottom = bk.top
            # 得分情况
            if role_y < roles[role_index].y:
                score += 1
            # 角色的移动
            if keyboard.left or keyboard.a:
                # 角色左移,x坐标减4
                role_x -= 4
                # 如果当前显示的角色图片是面朝右显示,则将其置为往左显示
                if role_index < 5:
                    role_index = 4
                for i in range(len(roles)):
                    roles[i].x = role_x
                speed_go += 1
                if speed_go % 4 == 0:
                    role_index += 1
                    if role_index > 9:
                        role_index = 5
            if keyboard.right or keyboard.d:
                # 角色右移,x坐标加4
                role_x += 4
                # 如果当前显示的角色图片是面朝左显示,则将其置为往右显示
                if role_index > 4:
                    role_index = 5
                for i in range(len(roles)):
                    roles[i].x = role_x
                speed_go += 1
                if speed_go % 4 == 0:
                    role_index += 1
                    if role_index > 4:
                        role_index = 0
    # 更新角色上一帧的y坐标
    role_y = roles[role_index].y
    # 如果角色不在任意一块砖上,角色下降
    if not is_player_on_brick:
        roles[role_index].y += speed_role
    # 砖块上移
    for bk in bricks:
        bk.y -= speed_brick
    # 判定游戏失败的条件
    if roles[role_index].top < 0 or roles[role_index].bottom > HEIGHT:
        speed_role = 0
        speed_brick = 0
        is_loose = True


pgzrun.go()

游戏效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值