找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10145|回复: 5

[已解决] 通过function规避了预加载问题,但是function的入参格式是?

[复制链接]
发表于 2021-7-15 14:15:03 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 Zealot001 于 2021-7-15 22:47 编辑

[RenPy] 纯文本查看 复制代码
default flag001 = 0

init -1 python:
    import requests
    import random

    class plusvar(object):
        def __init__(self, plusx):
            self.plusx = plusx

        @classmethod
        def plus_var_fun01(self):
            global flag001
            flag001 = flag001 + random.randint(5,8)
            if flag001 >= 50:
                flag001 = 50

    def plus_var_fun():
        global flag001
        flag001 = flag001 + random.randint(5,8)
        if flag001 >= 50:
            flag001 = 50

    def red_var_fun():
        global flag001
        flag001 = flag001 - random.randint(5,8)
        if flag001 <= 0:
            flag001 = 0



label start:
    
    $ flag001 = 20
    scene black
    call screen attack_fun

    return


screen attack_fun():

    frame:
        xcenter 0.25
        ycenter 0.25
        xsize 180
        ysize 320
        vbox:
            bar:
                value AnimatedValue(value=flag001, range=50, delay=0.3, old_value=None)
                xsize 170
                xcenter 0.5

            if flag001 < 50:
                textbutton "治疗":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action Function(plusvar.plus_var_fun01)
            elif flag001 >= 50:
                textbutton "满了,归零":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action SetVariable("flag001", 0)

            if flag001 > 0:
                textbutton "伤害":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action Function(red_var_fun)
            elif flag001 <= 0:
                textbutton "空了,加满":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action SetVariable("flag001", 50)


            text "血量是:[flag001]"


直接用function调用函数和类方法都可以让代码正常运行,也没有预加载导致的数据混乱。
但是在看文档的时候,function的介绍是
Function(callable, *args, **kwargs)
这个行为会使用 args 和 kwargs 调用 callable 。

callable
可调用的对象。
args
传给 callable` 的固定位置入参。
kwargs
传给 callable 的关键词入参。
这个行为使用一个可选的 _update_screens 关键词参数,而且这个参数默认为True。参数为True时,函数返回后,互动行动会重新开始,各界面会更新。

如果函数返回一个非空值,互动行为会停止并返回那个值。(使用call screen语句得到的返回值放置在 _return 变量中。)

没有入参,直接用全局变量的话,想复用有点麻烦,假如说我现在的函数追加了一个入参

[RenPy] 纯文本查看 复制代码
def plus_var_funX(x):        x = x + random.randint(5,8)
        if x >= 50:
            x = 50
        
action Function(plus_var_funX, x=flag001)#入参这部分的格式,按照文档里的解释,没看懂


相应的action Function(red_var_fun)里面的参数要怎么才能正确传入呢?然后对应到数值变化呢?
我试过了action Function(red_var_fun, flag001)和action Function(red_var_fun, x=flag001)
都没有办法正常运行,想请教一下具体的写法是什么?



 楼主| 发表于 2021-7-15 14:59:00 | 显示全部楼层
虽然知道*args, **kwargs分别对应的是数组和字典,但是我看其他人的程序,似乎并不局限于这两种数据类型。
现在问题在于,函数的值并没有传回到flag001
向大佬请教解决这个问题的思路
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2021-7-15 15:42:41 | 显示全部楼层
本帖最后由 Zealot001 于 2021-7-15 15:47 编辑

[RenPy] 纯文本查看 复制代码
default flag001 = 0
default flag002 = 0

init -1 python:
    import requests
    import random

    def plus_var_fun():
        global flag001
        flag001 = flag001 + random.randint(5,8)
        if flag001 >= 50:
            flag001 = 50

    def red_var_fun():
        global flag001
        flag001 = flag001 - random.randint(5,8)
        if flag001 <= 0:
            flag001 = 0



label start:

    $ flag001 = 21
    $ flag002 = 5
    scene black
    call screen stats

    return


screen stats():
    use attack_fun(flag001,0.25)
    use attack_fun(flag002,0.75)


screen attack_fun(hp, xalign):

    frame:
        xalign xalign
        # xcenter 0.25
        ycenter 0.25
        xsize 180
        ysize 320
        vbox:
            bar:
                value AnimatedValue(value=hp, range=50, delay=0.3, old_value=None)
                xsize 170
                xcenter 0.5

            if hp < 50:
                textbutton "治疗":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action Function(plus_var_fun)
            elif hp >= 50:
                textbutton "满了,归零":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action SetVariable("hp", 0)

            if hp > 0:
                textbutton "伤害":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action Function(red_var_fun)
            elif hp <= 0:
                textbutton "空了,加满":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action SetVariable("hp", 50)

            text "血量是:[hp]"


如果单纯定义全局变量就没办法复用代码,比如我现在让左右两边同时出现按钮框,但右边的以flag002入参的screen的按钮,还是会对应传入以flag001入参的screen界面。
screen还出现了另一个问题,就是当flag001大于50或者小于0就会报错
提示The variable hp does not exist.(hp这个变量不存在)


回复 支持 抱歉

使用道具 举报

发表于 2021-7-15 22:02:57 | 显示全部楼层
楼主的大部分问题,都出在设计层面上。建议学习一些设计模式方面的知识(比如常见的MVC模式),理解设计模式之后,就能自行解决这些问题。
语法什么的都是细枝末节的东西……

全局变量一般都用作系统配置项,角色属性不适合设为全局。自定义类把各种属性塞进去。
全局函数通过引用自定义类的对象,并调用对象成员方法实现数据变更。

下面是一段样例,仅作语法参考,省略了很多东西:

[RenPy] 纯文本查看 复制代码
default somebody = CharactorStatus("somebody", 0, 0)
init -1 python:
    import requests
    import random

    class CharactorStatus(NoRollback):
        name = ""
        hp = 0
        mp = 0
        def __init__(self, name, hp, mp):
            self.name = name
            self.hp = hp
            self.mp = mp

        def GetHeal(self, heal):
            self.hp = min(self.hp + heal, 10)

    def HealCharactor(charactor, heal):
        charactor.GetHeal(heal)

screen attack_fun():

    frame:
        xcenter 0.25
        ycenter 0.25
        xsize 180
        ysize 320
        vbox:
            bar:
                value AnimatedValue(value=somebody.hp, range=10, delay=1.0, old_value=None)
                xsize 200
                xcenter 0.5
            if somebody.hp < 10:
                textbutton "治疗":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action Function(HealCharactor, charactor = somebody, heal = 1)
            else:
                textbutton "满了":
                    text_size 40
                    text_outlines[(absolute(2), "#effdff", absolute(0), absolute(0))]
                    text_color "#ed54de"
                    text_selected_color "#32e0fc"
                    action SetField(somebody, "hp", 0), Jump("start")
            text "[somebody.name]"

define gray = "#e0e0e0"

label main_menu:
    return

label start:

    scene gray

    call screen attack_fun

    return

回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2021-7-15 22:12:57 | 显示全部楼层
被诅咒的章鱼 发表于 2021-7-15 22:02
楼主的大部分问题,都出在设计层面上。建议学习一些设计模式方面的知识(比如常见的MVC模式),理解设计模式 ...

是的,我也在学习这些东西,现在还在新手阶段,刚才自己琢磨了一下,其实就是就是局部变量和全局变量的问题,
如果是py3,可以用nonloacal解决,但是py2似乎不支持,先学习您的代码,然后考虑更好的优化代码。
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2021-7-15 22:37:32 | 显示全部楼层
被诅咒的章鱼 发表于 2021-7-15 22:02
楼主的大部分问题,都出在设计层面上。建议学习一些设计模式方面的知识(比如常见的MVC模式),理解设计模式 ...

我之前钻牛角尖了,果然还是类好用,类属性直接越过局部变量,您的代码真是让我茅塞顿开,太感谢了!
回复 支持 抱歉

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-27 13:21 , Processed in 0.129007 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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