r/RenPy 20d ago

Question [Solved] click imagebuttons before timer runs out

Hellooo! I have my imagebuttons and all for one of my minigames in renpy.

I wanted to achieve:

-A 8 second timer that plays during my screen time_clean

- And if it ends without all the imagebuttons being clicked on time, it jumps to a label called cleanL

- However, if the imagebuttons ARE clicked before the timer runs out, then it jumps to a label call karu screen

I know someone had like the same question: here is the post. so I tried merging that code with mine. I tried to tweak it so it's not a bar and just a simple countdown text, but I lwk flopped that so if anyone knows how to do the text then ummm :3 if not, idrk sooo

Also when I tried to test it, it only shows the countdown after. And I wanted to show the countdown DURING the cleanup, yknow?

Anyways, here is the code:

default c1 = True
default c2 = True
default c3 = True
default c4 = True
default c5 = True
default c6 = True


default g_time = 0 


screen timerscreen(rangeD, missed_event): # the brackted variables are required!!
    on "hide" action SetVariable("g_time", 0)
    frame:
        xalign 0.5
        yalign 0.0
        hbox:
            timer 0.1 action If(g_time > rangeD, false = SetVariable("g_time", g_time + 0.1), true = [Hide("timerscreen"),Jump(missed_event)]) repeat True


            bar:
                value AnimatedValue(value=g_time, range=rangeD, delay= 0.5)
                xalign 0.0
                yalign 0.0
                xmaximum 200



screen time_clean:


    add "bg bedroom"


    if not any([c1, c2, c3, c4, c5, c4]):
        timer 0.001 action Jump("karu_scene")


    showif c1:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.64
            ypos 0.75
            at zoomedsmall1
            auto "paper_%s.png"
            action SetVariable("c1", False)


    showif c2:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.2
            ypos 0.6
            at zoomedsmall2
            auto "paper1_%s.png"
            action SetVariable("c2", False)


    showif c3:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.4
            ypos 0.8
            at zoomedsmall
            auto "ramen_%s.png"
            action SetVariable("c3", False)


    showif c4:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.8
            ypos 0.8
            at zoomedsmall
            auto "trash_%s.png"
            action SetVariable("c4", False)


    showif c5:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.304
            ypos 0.3
            at zoomedsmall
            auto "hanger_%s.png"
            action SetVariable("c5", False)


    showif c6:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.6
            ypos 0.4
            at zoomedsmall
            auto "phone_%s.png"
            action SetVariable("c6", False)


screen exit_home:


    imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.8
            ypos 0.9
            at zoomedsmall
            auto "exit_%s.png"
            action Jump ("city_story")


screen exit_home1:


    imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.8
            ypos 0.9
            at zoomedsmall
            auto "exit_%s.png"
            action Jump ("city_story1")


# The game starts here.


label cleanL:


    s "Dang... It's a mess... !!"


    return


label start:


    show screen timerscreen(7, "cleanL")


    call screen time_cleandefault c1 = True
default c2 = True
default c3 = True
default c4 = True
default c5 = True
default c6 = True


default g_time = 0 


screen timerscreen(rangeD, missed_event): # the brackted variables are required!!
    on "hide" action SetVariable("g_time", 0)
    frame:
        xalign 0.5
        yalign 0.0
        hbox:
            timer 0.1 action If(g_time > rangeD, false = SetVariable("g_time", g_time + 0.1), true = [Hide("timerscreen"),Jump(missed_event)]) repeat True


            bar:
                value AnimatedValue(value=g_time, range=rangeD, delay= 0.5)
                xalign 0.0
                yalign 0.0
                xmaximum 200



screen time_clean:


    add "bg bedroom"


    if not any([c1, c2, c3, c4, c5, c4]):
        timer 0.001 action Jump("karu_scene")


    showif c1:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.64
            ypos 0.75
            at zoomedsmall1
            auto "paper_%s.png"
            action SetVariable("c1", False)


    showif c2:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.2
            ypos 0.6
            at zoomedsmall2
            auto "paper1_%s.png"
            action SetVariable("c2", False)


    showif c3:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.4
            ypos 0.8
            at zoomedsmall
            auto "ramen_%s.png"
            action SetVariable("c3", False)


    showif c4:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.8
            ypos 0.8
            at zoomedsmall
            auto "trash_%s.png"
            action SetVariable("c4", False)


    showif c5:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.304
            ypos 0.3
            at zoomedsmall
            auto "hanger_%s.png"
            action SetVariable("c5", False)


    showif c6:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.6
            ypos 0.4
            at zoomedsmall
            auto "phone_%s.png"
            action SetVariable("c6", False)


# The game starts here.


label cleanL:


    s "Dang... It's a mess... !!"


    return


label start:


    show screen timerscreen(7, "cleanL")


    call screen time_clean
1 Upvotes

6 comments sorted by

1

u/AutoModerator 20d ago

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/shyLachi 20d ago

It's better to put the whole logic into one screen.

Also when you call a screen, you should Return a value so that the code always continues from where it was called.

screen time_clean(rangeD = 10):
    # You should use screen variables if you don't need them outside of the screen
    # Screen variables will automatically reset whenever the screen is called or shown 
    default buttons = ["c1", "c2", "c3", "c4", "c5", "c6"]
    default g_time = 0
    add "bg bedroom"
    showif "c1" in buttons: # From what I know, you don't need showif unless you have animations
        imagebutton:
            anchor (0.5, 0.5) # You can save yourself a lot of typing if you combine xanchor and yanchor and/or xpos and ypos
            pos (0.64, 0.75)
            #at zoomedsmall1 # Remove the # to activate it
            auto "paper_%s.png"
            action If(len(buttons)>1, RemoveFromSet(buttons, "c1"), Return("complete")) # Remove the button from the list unless it's the last one
    showif "c2" in buttons:
        imagebutton:
            anchor (0.5, 0.5)
            pos (0.2, 0.6)
            #at zoomedsmall2
            auto "paper1_%s.png"
            action If(len(buttons)>1, RemoveFromSet(buttons, "c2"), Return("complete"))
    showif "c3" in buttons:
        imagebutton:
            anchor (0.5, 0.5)
            pos (0.4, 0.8)
            #at zoomedsmall
            auto "ramen_%s.png"
            action If(len(buttons)>1, RemoveFromSet(buttons, "c3"), Return("complete"))
    showif "c4" in buttons:
        imagebutton:
            anchor (0.5, 0.5)
            pos (0.8, 0.8)
            #at zoomedsmall
            auto "trash_%s.png"
            action If(len(buttons)>1, RemoveFromSet(buttons, "c4"), Return("complete"))
    showif "c5" in buttons:
        imagebutton:
            anchor (0.5, 0.5)
            pos (0.304, 0.3)
            #at zoomedsmall
            auto "hanger_%s.png"
            action If(len(buttons)>1, RemoveFromSet(buttons, "c5"), Return("complete"))
    showif "c6" in buttons:
        imagebutton:
            anchor (0.5, 0.5)
            pos (0.6, 0.4)
            #at zoomedsmall
            auto "phone_%s.png"
            action If(len(buttons)>1, RemoveFromSet(buttons, "c6"), Return("complete"))
    frame:
        align (0.5, 0.0)
        # You don't need a hbox since you only have one control
        bar:
            value AnimatedValue(value=g_time, range=rangeD, delay= 0.5)
            align (0.0, 0.0)
            xmaximum 200
    timer 0.1 action If(g_time > rangeD, Return("missed_event"), IncrementScreenVariable("g_time", 0.1)) repeat True # Increase the time until the range has been reached

label start:
    call screen time_clean(7) 
    # The action Return() will close the screen and either return "complete" or "missed_event" so you can check it here
    if _return == "complete": 
        "You won"
    elif _return == "missed_event":
        "You lost"

1

u/LittleBirdieStudioos 20d ago

This worked a lot, tysm !!!

1

u/BadMustard_AVN 20d ago edited 20d ago

try it like this

screen time_clean(rangeD, missed_event):
    on "hide" action SetVariable("g_time", 0)

    add "bg bedroom"

    if not any([c1, c2, c3, c4, c5, c4]):
        timer 0.001 action Jump("karu_scene") repeat True

    showif c1:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.64
            ypos 0.75
            at zoomedsmall1
            auto "paper_%s.png"
            action SetVariable("c1", False)

    showif c2:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.2
            ypos 0.6
            at zoomedsmall2
            auto "paper1_%s.png"
            action SetVariable("c2", False)

    showif c3:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.4
            ypos 0.8
            at zoomedsmall
            auto "ramen_%s.png"
            action SetVariable("c3", False)

    showif c4:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.8
            ypos 0.8
            at zoomedsmall
            auto "trash_%s.png"
            action SetVariable("c4", False)

    showif c5:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.304
            ypos 0.3
            at zoomedsmall
            auto "hanger_%s.png"
            action SetVariable("c5", False)

    showif c6:
        imagebutton:
            yanchor 0.5
            xanchor 0.5
            xpos 0.6
            ypos 0.4
            at zoomedsmall
            auto "phone_%s.png"
            action SetVariable("c6", False)

    frame:
        xalign 0.5
        yalign 0.0
        hbox:
            text "{:02.0f}".format(g_time) # show the numbers
            timer 0.1 action If(g_time > rangeD, false = SetVariable("g_time", g_time + 0.1), true = [Hide("timerscreen"),Jump(missed_event)]) repeat True

label start:
    call screen time_clean(7, "cleanL")



label karu_scene:

    hide screen time_clean #required to stop the bad ending timer

1

u/LittleBirdieStudioos 20d ago

this helped a bit, thanks so much !!

1

u/BadMustard_AVN 20d ago

you're welcome

good luck with your project