找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1323|回复: 2

[已解决] 抽奖,每次random会出现两个到背包里

[复制链接]
发表于 2022-1-8 16:50:07 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 xccc 于 2022-1-9 15:05 编辑

抽奖功能,每次可以花100金币抽一次
功能实现方法是用 random 从奖池里随机抽取一个,然后放到背包里

问题:
点击一次抽奖会有两个物品跑到背包里,希望只有一个出现,不知道问题出在哪里,请大佬们帮忙


[RenPy] 纯文本查看 复制代码
init 0 python:
    # 游戏内获得的金币
    money = 1000
    # 可以被抽奖抽到的东西
    things = ["images/q1.png","images/q2.png","images/q3.png","images/q4.png"]
    # 背包里获得的东西
    bag_things = ["images/tem.png"]
###################################
#             背包
###################################

screen mybag():
    fixed:
        add "images/background.png"

        textbutton "Voltar\n返回":
            xalign 0.05 yalign 0.9
            text_size 20
            text_color "#000000"
            text_hover_color "#4EEE94"
            action Hide("mybag")

        viewport:
            xalign 0.5 yalign 0.5 xysize (500, 600)
            scrollbars "vertical"
            spacing 5

            draggable True
            mousewheel True
            arrowkeys True
            grid 3 10:
                spacing 50
                for i in range(0,30):
                    if i<len(bag_things):
                        imagebutton:
                            idle bag_things[i]
                            hover bag_things[i]
                    else:
                        null



###################################
#            商店
###################################
screen shop():
    fixed:
        add "images/background.png"

        textbutton "Voltar\n返回":
            xalign 0.05 yalign 0.9
            text_size 20
            text_color "#000000"
            text_hover_color "#4EEE94"
            action Hide("shop")
    fixed:
        xalign 0.5 yalign 0.5
        xysize(700,500)
        vbox:
            add "images/tem.png"
            textbutton "抽奖":
                text_color "#000000"
                text_hover_color "#4EEE94"
                action Show('tip')

screen tip():
    modal True
    frame:
        xsize 300 ysize 300
        xalign 0.5 yalign 0.5
        vbox:
            if money >= 100 and len(things)>=1:
                text"你将花100块抽奖"
                hbox:
                    textbutton "确定":
                        text_color "#000000"
                        text_hover_color "#4EEE94"
                        action [SetVariable('money',money-100),Show('jiang'),Hide('tip')]
            elif money >= 100 and len(things)==0:
                text "没有东西可以抽奖啦"
                textbutton "确定":
                    text_color "#000000"
                    text_hover_color "#4EEE94"
                    action Hide("tip")
            else:
                text "钱不够"
                textbutton "确定":
                    text_color "#000000"
                    text_hover_color "#4EEE94"
                    action Hide("tip")
screen jiang():
    modal True
    $ choose = renpy.random.choice(things)
    $ things.remove(choose)
    $ bag_things.append(choose)
    frame:
        xsize 300 ysize 300
        xalign 0.5 yalign 0.5
        vbox:
            text "恭喜你获得了:"
            add choose
            textbutton "确定":
                text_color "#000000"
                text_hover_color "#4EEE94"
                action Hide('jiang')


 楼主| 发表于 2022-1-9 14:51:49 | 显示全部楼层
本帖最后由 xccc 于 2022-1-9 14:53 编辑

谢谢大佬的指导,在重新写一个function之后就可以正常运行啦!
下面是修改之后的代码,有需要的小伙伴可以参考一下


[RenPy] 纯文本查看 复制代码
###################################
#             背包
###################################

screen mybag():
    fixed:
        add "images/background.png"

        textbutton "Voltar\n返回":
            xalign 0.05 yalign 0.9
            text_size 20
            text_color "#000000"
            text_hover_color "#4EEE94"
            action Hide("mybag")

        viewport:
            xalign 0.5 yalign 0.5 xysize (500, 600)
            scrollbars "vertical"
            spacing 5

            draggable True
            mousewheel True
            arrowkeys True
            grid 3 10:
                spacing 50
                for i in range(0,30):
                    if i<len(bag_things):
                        imagebutton:
                            idle bag_things[i]
                            hover bag_things[i]
                    else:
                        null



###################################
#            商店
###################################


init python:
    class Shop():
        def __init__(self):
            self.choose = 'images/tem.png'

        def shop_func(self):
            # 随机选
            self.choose = renpy.random.choice(things)
            # 删除添加
            things.remove(self.choose)
            bag_things.append(self.choose)
    ashop = Shop()

screen shop():
    fixed:
        add "images/background.png"

        textbutton "Voltar\n返回":
            xalign 0.05 yalign 0.9
            text_size 20
            text_color "#000000"
            text_hover_color "#4EEE94"
            action Hide("shop")
    fixed:
        xalign 0.5 yalign 0.5
        xysize(700,500)
        vbox:
            add "images/tem.png"
            textbutton "抽奖":
                text_color "#000000"
                text_hover_color "#4EEE94"
                action Show('tip')

screen tip():
    modal True
    frame:
        xsize 300 ysize 300
        xalign 0.5 yalign 0.5
        vbox:
            if money >= 100 and len(things)>=1:
                text"你将花100块抽奖"
                hbox:
                    textbutton "确定":
                        text_color "#000000"
                        text_hover_color "#4EEE94"
                        action [SetVariable('money',money-100),Show('jiang'),Hide('tip'),Function(ashop.shop_func)]
                    textbutton "取消":
                        text_color "#000000"
                        text_hover_color "#4EEE94"
                        action Hide("tip")
            elif money >= 100 and len(things)==0:
                text "没有东西可以抽奖啦"
                textbutton "确定":
                    text_color "#000000"
                    text_hover_color "#4EEE94"
                    action Hide("tip")
            else:
                text "钱不够"
                textbutton "确定":
                    text_color "#000000"
                    text_hover_color "#4EEE94"
                    action Hide("tip")
screen jiang():
    modal True
    frame:
        xsize 300 ysize 300
        xalign 0.5 yalign 0.5
        vbox:
            text "恭喜你获得了"
            add ashop.choose
            textbutton "确定":
                text_color "#000000"
                text_hover_color "#4EEE94"
                action Hide('jiang')[/i][/i]
[i][i]


回复 支持 1 抱歉 0

使用道具 举报

发表于 2022-1-9 03:56:15 | 显示全部楼层
你不应该再screen里面直接写下面这段python code
[RenPy] 纯文本查看 复制代码
  modal True
    $ choose = renpy.random.choice(things)
    $ things.remove(choose)
    $ bag_things.append(choose)


你应该以玩家按按钮来触发这个逻辑,否则玩家改变窗口大小这个窗口会重新加载,这个逻辑也会再运行一次。
按钮触发逻辑,可以写个函数,然后再用function去触发。
https://doc.renpy.cn/zh-CN/screen_actions.html

回复 支持 1 抱歉 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 02:51 , Processed in 0.071155 second(s), 14 queries , File On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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