找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 568|回复: 6

[教程] 萌新也可以学习的高级密码锁制作!(renpy进阶学习经验四)

[复制链接]
发表于 2024-6-18 19:57:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 烈林凤 于 2024-8-15 15:34 编辑

这次居然半个月后就更新了进阶学习经验的第四篇!难不成以后的更新频率越来越快?100年后岂不是可以做到一分钟出一篇!(开玩笑的)
本篇的灵感来源于我许久之前做过的一篇教程,真的非常感谢各位的支持!在此贴上链接萌新也可以学习的renpy简单密码锁制作! - 经验教程 - RenPy中文空间
不过这篇教程时代久远,是我萌新时期经验总结下来的一篇,放在现在显然是不够看的,于是,我制作了一个更高级的密码锁

可以直接下载附件中的.rpy文件,放入工程的game文件夹中(任何位置都可以),然后查看“使用”环节的使用方法

这次给各位带来的是“萌新也可以学习的高级密码锁制作!”

这次教程主要运用了Function()行为,因此会运用诸多与Python直接相关的内容,文章中也会有对这些python语句的详细解释,非常适合萌新初步认识python语句
关于教程里的很多名词的命名都无所谓,你想怎么命名都可以,我这么命名只是我听着顺耳。
这次将很多讲解的部分放在了代码注释里,请自行查看。
如果你觉得看文章看不懂想直接抄代码,可以直接复制“抄代码环节”里的代码。

感谢@Aaron栩生阿龙 提供的思路!

注意!本篇教程使用的是8.2.1版本的renpy,虽然说用低版本不一定会报错,但还是建议换成最新版本,谢谢配合!

接下来进入正题——

No.1
我们需要先对变量进行定义——
[RenPy] 纯文本查看 复制代码
# 定义全局变量
default mylist = [] # 用于存放输入的元素
default true_num = '' # 正确答案
default max_num = 0 # 最大输入数量

在此涉及到了“全局变量”与“局部变量”的概念:
用default或init python定义的变量为全局变量,即可以用于任何地方的变量
而像是def函数括号里的变量、screen括号里的变量就是局部变量,这些变量只能在各自的区域内使用,别的地方不能直接使用这些变量,这样能避免过多的占用和防止冲突
全局变量和局部变量在后续都有使用

No.2
接下来需要使用init python定义函数——
[RenPy] 纯文本查看 复制代码
init -1 python:
    # 向列表中添加元素(用于显示列表元素)
    def show_list(list, myinput):
        # 如果列表中存在'错误'或'正确',则清空列表
        if ('错误' or '正确') in list:
            list.clear()
        
        list.append(myinput)
        
        # 限制列表元素数量
        if len(list) > max_num:
            list.pop()
        else:
            pass

    # 撤回列表中的最后一个元素
    def remove_last_element(list):
        if list:
            list.pop()
        else:
            pass

    # 清空列表中的所有元素
    def list_clear(list):
        list.clear()

    # 判断列表中的元素(答案)是否正确
    def list_decide(list):
        # 使用join函数将列表转换为字符串方便比较
        mystr = ''.join(list)

        # 判断答案是否正确
        if mystr == true_num:
            list.clear()
            list.append('正确')
        else:
            list.clear()
            list.append('错误')

在此定义了4个函数,分别用于——显示数字并限制数量、撤回最后一个输入的数字、清空屏幕中的所有数字、判断密码是否正确
在这些函数括号内的就是局部变量,而调用函数时向函数传入参数被称之为“入参”,而传入的这些参数就会与括号内的变量位置一一对应,改变这些变量的值,从而执行函数内指令。

append()用于向列表添加元素
pop()用于删除列表最后一个元素
clear()用于清除列表中的所有元素
join()用于将列表中的元素全部拼合成一串字符
len()用于获取列表中元素的数量
这些python函数的具体用法可以上网自行搜索,在此就不过多赘述了

NO.3
然后是screen的部分——
[RenPy] 纯文本查看 复制代码
screen lock_ceshi_15():

    # 使用meun保证能替换掉屏幕中的其他菜单
    tag menu

    # 使用join函数将列表转换为字符串
    $ mystr = ''.join(mylist)

    # 显示列表中的元素
    if mylist:
        button:
            background "#ffffff"
            xycenter (0.5, 0.2)
            hbox:
                for i in mylist:
                    text '{color=#000000}'

    # 各类按键
    vbox:
        xycenter (0.5, 0.5)
        spacing 10
        if mylist != []:
            hbox:
                spacing 11
                button:
                    xysize (117, 75)
                    background "#9dc874"
                    hover_background "#c7eda4"
                    text '确定' xycenter (0.5,0.5)
                    # 调用函数list_decide,传入列表mylist作为参数
                    action Function(list_decide, list=mylist)
                    # 如果想要在按下按钮后跳转回原先的label中,那可以使用以下action行为
                    # action If(mystr == true_num,Return(),Function(list_decide, list=mylist))
                button:
                    xysize (117, 75)
                    background "#c87474"
                    hover_background "#eda4a4"
                    text '清除' xycenter (0.5,0.5)
                    # 调用函数list_clear,传入列表mylist作为参数
                    action Function(list_clear, list=mylist)
        else:
            hbox:
                spacing 9
                button:
                    xysize (118, 75)
                    background "#00000000"
                button:
                    xysize (118, 75)
                    background "#00000000"
        
        # 多出10个像素高度的空白,用于美观
        null height 10

        hbox:
            spacing 10
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '1' xycenter (0.5,0.5)
                # 调用函数show_list,传入列表mylist和输入值作为参数
                action Function(show_list, list=mylist, myinput='1')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '2' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='2')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '3' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='3')
        
        hbox:
            spacing 10
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '4' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='4')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '5' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='5')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '6' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='6')
        
        hbox:
            spacing 10
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '7' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='7')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '8' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='8')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '9' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='9')

        hbox:
            spacing 10
            button:
                xysize (160, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '回退' xycenter (0.5,0.5)
                # 调用函数remove_last_element,传入列表mylist作为参数
                action Function(remove_last_element, list=mylist)
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '0' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='0')

这里的各类按钮的action均使用了Function()行为,该方法可以使按钮行为被触发时调用一个函数并传入参数,请查看文档界面行为(action)、值(value)和函数 — Ren'Py 中文文档 (renpy.cn)
这里还有一个关于参数的概念——固定位置入参和关键词入参,这里简单说明一下,具体可查看文档编程语言基础 — Ren'Py 中文文档 (renpy.cn)

例如函数def xxx(xa,xb,xc),需要将其变为“ xa=1,xb=None,xc='test' ”——
固定位置入参:需要根据函数变量的位置填写参数,填写的位置不同会使入参的位置不同—— xxx(1, ,'test')
关键词入参:无需根据函数变量的位置填写参数,填写的位置无论在哪里都不会影响入参的位置—— xxx(xb=,xa=1,xc='test')
在本文的Function()中所使用的均为关键词入参,具体请自行查看文档


此外,在ui搭建中,使用了一些特殊的功能,在此也一并讲了——
tag:使用该语句会使screen被视为一个变量,并贴上一个图像标签(tag),在同一个屏幕中,不会显示两个相同tag的图像(保证该screen可以将其他拥有该tag的screen替换掉,只保留我们想要的这个),具体可查看文档界面和界面语言 — Ren'Py 中文文档 (renpy.cn)
null:使界面中插入一段空白,有两种形式,可以参考文档界面和界面语言 — Ren'Py 中文文档 (renpy.cn)

抄代码环节——
[RenPy] 纯文本查看 复制代码
# 定义全局变量
default mylist = [] # 用于存放输入的元素
default true_num = '' # 正确答案
default max_num = 0 # 最大输入数量

init -1 python:
    # 向列表中添加元素(用于显示列表元素)
    def show_list(list, myinput):
        # 如果列表中存在'错误'或'正确',则清空列表
        if ('错误' or '正确') in list:
            list.clear()
        
        list.append(myinput)
        
        # 限制列表元素数量
        if len(list) > max_num:
            list.pop()
        else:
            pass

    # 撤回列表中的最后一个元素
    def remove_last_element(list):
        if list:
            list.pop()
        else:
            pass

    # 清空列表中的所有元素
    def list_clear(list):
        list.clear()

    # 判断列表中的元素(答案)是否正确
    def list_decide(list):
        # 使用join函数将列表转换为字符串方便比较
        mystr = ''.join(list)

        # 判断答案是否正确
        if mystr == true_num:
            list.clear()
            list.append('正确')
        else:
            list.clear()
            list.append('错误')

screen lock_ceshi_15():

    # 使用meun保证能替换掉屏幕中的其他菜单
    tag menu

    # 使用join函数将列表转换为字符串
    $ mystr = ''.join(mylist)

    # 显示列表中的元素
    if mylist:
        button:
            background "#ffffff"
            xycenter (0.5, 0.2)
            hbox:
                for i in mylist:
                    text '{color=#000000}'

    # 各类按键
    vbox:
        xycenter (0.5, 0.5)
        spacing 10
        if mylist != []:
            hbox:
                spacing 11
                button:
                    xysize (117, 75)
                    background "#9dc874"
                    hover_background "#c7eda4"
                    text '确定' xycenter (0.5,0.5)
                    # 调用函数list_decide,传入列表mylist作为参数
                    action Function(list_decide, list=mylist)
                    # 如果想要在按下按钮后跳转回原先的label中,那可以使用以下action行为
                    # action If(mystr == true_num,Return(),Function(list_decide, list=mylist))
                button:
                    xysize (117, 75)
                    background "#c87474"
                    hover_background "#eda4a4"
                    text '清除' xycenter (0.5,0.5)
                    # 调用函数list_clear,传入列表mylist作为参数
                    action Function(list_clear, list=mylist)
        else:
            hbox:
                spacing 9
                button:
                    xysize (118, 75)
                    background "#00000000"
                button:
                    xysize (118, 75)
                    background "#00000000"
        
        # 多出10个像素高度的空白,用于美观
        null height 10

        hbox:
            spacing 10
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '1' xycenter (0.5,0.5)
                # 调用函数show_list,传入列表mylist和输入值作为参数
                action Function(show_list, list=mylist, myinput='1')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '2' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='2')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '3' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='3')
        
        hbox:
            spacing 10
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '4' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='4')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '5' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='5')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '6' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='6')
        
        hbox:
            spacing 10
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '7' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='7')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '8' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='8')
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '9' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='9')

        hbox:
            spacing 10
            button:
                xysize (160, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '回退' xycenter (0.5,0.5)
                # 调用函数remove_last_element,传入列表mylist作为参数
                action Function(remove_last_element, list=mylist)
            button:
                xysize (75, 75)
                background "#7491c8"
                hover_background "#abc1e9"
                text '0' xycenter (0.5,0.5)
                action Function(show_list, list=mylist, myinput='0')


使用环节——
[RenPy] 纯文本查看 复制代码
# 启动界面
label start:
    $ true_num = '012345'
    $ max_num = 6
    call screen lock_ceshi_15

使用python语句将true_num和max_num的数值改变,一个是正确的密码,另一个是可输入密码的最大长度,最后使用call screen显示界面(也可以使用show screen加pause)
试着运行一下,会正确显示类似于图片中的画面——
QQ截图20240618193251.jpg

代码各位可以随意使用并修改,希望各位用得高兴!
祝各位还在上学的小伙伴们暑假愉快!
我们下次再见!



ceshi_15.rpy

6.15 KB, 下载次数: 1, 下载积分: 活力 100

评分

参与人数 2活力 +300 干货 +4 收起 理由
被诅咒的章鱼 + 300 + 3 期待百年之后楼主1分钟1更新
ZYKsslm + 1 鼓励原创!

查看全部评分

本帖被以下淘专辑推荐:

发表于 2024-6-18 20:02:50 来自手机 | 显示全部楼层
我亲爱的烈烈🤤🤤🤤实在是太厉害了!

点评

不是,哥们)  发表于 2024-6-18 20:07
回复 支持 1 抱歉 0

使用道具 举报

发表于 2024-8-8 23:30:51 | 显示全部楼层
大佬您好,非常感谢您的分享,但我想问问# 可以使用return返回原先的label,这一步是如何实现的?我在if判断下照您的提示添加了return,但在实际效果中并没有返回上一个label,请问还需要写什么才能返回?感谢您的答复!
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2024-8-9 14:39:51 | 显示全部楼层
萌新5566 发表于 2024-8-8 23:30
大佬您好,非常感谢您的分享,但我想问问# 可以使用return返回原先的label,这一步是如何实现的?我在if判 ...

哦,我在写教程时没能意识到这个return是在def中的,并不能像在screen中一样使用,之后我会修改教程的。
如果想要返回原先的label中,需要修改“确定”按钮下的action行为,例如:
[RenPy] 纯文本查看 复制代码
action If(mystr == true_num,Return(),Function(list_decide, list=mylist))
使用这段代码就能实现你想要的效果
回复 支持 抱歉

使用道具 举报

发表于 2024-8-9 17:52:45 | 显示全部楼层
非常实用,感谢您!
回复 支持 抱歉

使用道具 举报

发表于 2024-8-16 23:05:55 | 显示全部楼层
谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|RenPy中文空间 ( 苏ICP备17067825号|苏公网安备 32092302000068号 )

GMT+8, 2024-9-8 11:26 , Processed in 0.157843 second(s), 36 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表