How to add "lives" to a minigame?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
LyraCentauri
Newbie
Posts: 10
Joined: Fri Mar 26, 2021 5:50 pm
Contact:

How to add "lives" to a minigame?

#1 Post by LyraCentauri »

How do I add "lives" to my minigame?

Hello again! I'm back with more questions about creating a minigame with Renpy as I've been experimenting with ways to make a QTE more challenging. This time I want to make it so that if the player misses the target (clicks the wrong spot on the screen) they lose a life and after three misses, the minigame ends and they have to start over.

I've added a button that is a transparent image behind the moving target covering the whole screen. Whenever the player clicks this "button" (which is anything outside the target hit box) they add a value to the "miss" variable. My first issue is that after 3 misses ($ miss==3) nothing happens and the game continues. How do I make it so that when $miss==3, the script jumps to my "death" label (or just ends in general)?

I've also added three heart images to the screen as a visual representation of the lives the player has. How do I make it so that after each miss, a heart is hidden from the screen? I've tried to use a series of if statements like this;
if miss==1:
hide image "images/heart_3.png"
But this also does nothing and the hearts stay on screen.

Please help me figure this out, I would greatly appreciate it!!

Code: Select all

screen target_scr(tgt_align, t):

    # animated timer
    bar value AnimatedValue(0, t, t, t) bar_invert True xsize 300 align (0.95, 0.05)

    button:
        image "transparent.png"
        action SetVariable("miss", If(miss<3,miss+1,3))

    imagebutton:
        auto "target_%s.png"
        action [Hide("target_scr"), Return(1)] # hide the screen by its name
        align tgt_align
        sensitive not game_over # clickable if game is not over

    image "heart_1.png":
        xpos -25
        ypos -50

    image "heart_2.png":
        xpos -25
        ypos 275

    image "heart_3.png":
        xpos -25
        ypos 600

    if not game_over:
        timer t action [Jump("too_sloww")]

screen minigame_scr():
    text "score: [score]" align (0.5, 0.05)

label savegold(targets_number, game_time):

    label try_againn:
        pass

    $ targets_left = targets_number

    show qte scoreboard
    show screen minigame_scr

    while targets_left > 0: # show targets while we have them
        show screen target_scr(tgt_align=( renpy.random.randint(2, 8)*0.1, renpy.random.randint(2, 8)*0.1 ), t=game_time)
        $ ui.interact() # waits for player interaction
        $ score += 1
        $ targets_left -= 1

    if score==10:
        jump rescue

###################################################
# THIS PART OF THE CODE DOESN'T WORK BUT I LEFT IT IN TO SHOW YOU WHAT
#IM TRYING TO DO
    if miss==3:
        hide image "images/heart_1.png"
    if miss==2:
        hide image "images/heart_2.png"
    if miss==1:
        hide image "images/heart_3.png"
####################################################

label rescue:
    pyr "\"Wow you're fast!\""
    hide screen minigame_scr
    hide qte scoreboard
    jump gold

label death:
    "Oh no! You ded."
    hide screen minigame_scr
    hide qte scoreboard

    hide screen target_scr 
    $ score = 0
    $ game_over = False

    menu:
        "Retry":
            jump try_againn


label too_sloww:
    $ game_over = True
    pyr "\"Too slow...\""
    hide screen minigame_scr
    hide qte scoreboard

    hide screen target_scr 
    $ score = 0
    $ game_over = False

    menu:
        "Retry":
            jump try_againn
        # "...":
        #     pass
        
call savegold(10,5.0
)

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to add "lives" to a minigame?

#2 Post by hell_oh_world »

I would have the hearts in a hbox/vbox instead, then use loop to propagate the hearts.

Code: Select all

default lives = 3 # i would prolly try to do the reverse, i'll use lives instead of miss, depends on what you need, you can always just invert the logic.

screen target_scr(tgt_align, t):

    # animated timer
    bar value AnimatedValue(0, t, t, t) bar_invert True xsize 300 align (0.95, 0.05)

    button:
        image "transparent.png"
        action [SetVariable("lives", lives - 1), If(lives == 0, Jump("death"))] # try to use multiple actions separated by ,

    imagebutton:
        auto "target_%s.png"
        action [Hide("target_scr"), Return(1)] # hide the screen by its name
        align tgt_align
        sensitive not game_over # clickable if game is not over
    
    hbox: # positions the hearts automatically in a row
        for i in range(lives):
            image "heart.png" # if the heart images are all the same then use one image instead if not then you can do `image "heart_{}.png".format(i)` where i is a number starting from 0 up to the current lives - 1.

    if not game_over:
        timer t action [Jump("too_sloww")]

LyraCentauri
Newbie
Posts: 10
Joined: Fri Mar 26, 2021 5:50 pm
Contact:

Re: How to add "lives" to a minigame?

#3 Post by LyraCentauri »

Thank you so much for the help! It works swimmingly now:D
hell_oh_world wrote: Tue Jul 27, 2021 5:41 am I would have the hearts in a hbox/vbox instead, then use loop to propagate the hearts.

Code: Select all

default lives = 3 # i would prolly try to do the reverse, i'll use lives instead of miss, depends on what you need, you can always just invert the logic.

screen target_scr(tgt_align, t):

    button:
        image "transparent.png"
        action [SetVariable("lives", lives - 1), If(lives == 0, Jump("death"))] # try to use multiple actions separated by ,
    
    hbox: # positions the hearts automatically in a row
        for i in range(lives):
            image "heart.png" # if the heart images are all the same then use one image instead if not then you can do `image "heart_{}.png".format(i)` where i is a number starting from 0 up to the current lives - 1.

Post Reply

Who is online

Users browsing this forum: VESTED