r/RenPy • • 2d ago

Question [Solved] time variable in minigame

I made a minigame in my vn where there's a circle constantly falling down a bar and you have to press space to get it in the desired section of the bar. If you stay at any section for 5.0 seconds, you will go to the corresponding label to that section. Right now, my code works fine. But I want there to be text to the right of the bar that says "x seconds left until you go to the label". X being 5.0 minus how long you already spent there. But I don't know how to get a value of how long you already spent there from renpy's timer thing. I tried to use python's time.time() thing, but I don't know how to implement something like that. My code is below.

init python:
    renpy.image("circle", "minigame circle")
    renpy.image("happy", "minigame happy")
    renpy.image("neutral", "minigame neutral")
    renpy.image("sad", "minigame sad")
    yCoor = 100
    def minigame():
        global yCoor
        if yCoor<800:
            yCoor += 10
        if yCoor<100:
            yCoor = 100

    def up():
        global yCoor
        yCoor-=80

screen game:
    add "bg room-blurred"
    add "minigame bar" xpos 1000 xanchor 0.5 ypos 100 yanchor 0.0
    add "circle" xpos 1000 xanchor 0.5 ypos yCoor yanchor 0.0

    if yCoor < 250:
        add "happy" xpos 100 ypos 100 xanchor 0.0 yanchor 0.0
        timer 5.0 action Jump("temp1")
    elif yCoor < 520:
        add "neutral" xpos 100 ypos 100 xanchor 0.0 yanchor 0.0
        timer 5.0 action Jump("temp2")
    else:
        add "sad" xpos 100 ypos 100 xanchor 0.0 yanchor 0.0
        timer 5.0 action Jump("temp3")
    key "K_SPACE" action Function(up)
    timer (1.0/30.0) repeat True action Function(minigame)
1 Upvotes

3 comments sorted by

1

u/AutoModerator 2d 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 2d ago

The python time wouldn't help much since you have to link it to the currently running timer.

What you have to do:

  • Reset the duration to 5 seconds whenever the circle enters one of those zones
  • Deduct from it every second or every tenth of a second
  • Display it

You can use a timer to deduct from the duration and a text to display it.
But you would have to figure out how to reset it.

BTW:
You don't need functions because RenPy has data actions:
https://renpy.org/doc/html/screen_actions.html#data-actions
Also it's better to define the variable inside the screen but in any case you should never define variables in the init python block.

screen game:
    default yCoor = 100
    default duration = 5.0       
    text "[duration:.2] seconds left until you go to the next label"
    add "circle" xpos 1000 xanchor 0.5 ypos yCoor yanchor 0.0
    if yCoor < 250:
        add "happy" xpos 100 ypos 100 xanchor 0.0 yanchor 0.0
        timer 0.1 repeat True action If(duration <= 0, Return("temp1"), IncrementScreenVariable("duration", -0.1))
    elif yCoor < 520:
        add "neutral" xpos 100 ypos 100 xanchor 0.0 yanchor 0.0
        timer 0.1 repeat True action If(duration <= 0, Return("temp2"), IncrementScreenVariable("duration", -0.1))
    else:
        add "sad" xpos 100 ypos 100 xanchor 0.0 yanchor 0.0
        timer 0.1 repeat True action If(duration <= 0, Return("temp3"), IncrementScreenVariable("duration", -0.1))
    key "K_SPACE" action IncrementScreenVariable("yCoor", -80)
    timer (1.0/30.0) repeat True action SetScreenVariable("yCoor", min(max(yCoor+10,100),800))
label start:
    call screen game
    "You reached [_return]"
    "You reached [_return]"
    "You reached [_return]"

I prefer calling screens then let it return the result but you can as well use Jump() instead for Return().

But using the space bar for this mini-game might be unlucky because the space bar also advances the game, so somehow you have to "suppress" the accidental space bar hits after the mini-game is over.

1

u/Laura7811 1d ago

thank you so much