Randomizing Display w/ Corresponding Options (& Disabling U.I.)

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
Foray
Newbie
Posts: 7
Joined: Sun Jan 21, 2018 8:06 am
Contact:

Randomizing Display w/ Corresponding Options (& Disabling U.I.)

#1 Post by Foray »

So I'm trying to make a rock/paper/scissors-like mini-game whereby the player is only allowed to continue upon multiple victories. However, I wish there to be a prompt telegraphing the computer's choice with an image before the player chooses. To work this, I'm creating a loop of sorts which pauses at the base label, then randomly selects to display one of three images corresponding to each of the computer's choices. Upon seeing the randomized image, the player is allowed to press one of three keys ("W" "A" & "D"), with the correct key jumping to a "success" label, the incorrect key to a "failure" label, and a tie to a "draw" label. Each of these labels displays a visual representing the results of the player's (and the computer's) choice and alters a predefined variable (player's wins or computer's wins) before a brief pause, after which it jumps the player back to the original label (the loop is broken when either the player or enemy reaches a number of victories).

My difficulties here are two-fold:

1) I'm not sure what code I would use to randomize the computer's choice (and corresponding image) at the start of each round, nor the most efficient way to display the respective image indicating the randomized output (show image, show scene, jump label, or using if statements).

2) I wish for the dialogue box and standard Ren'Py navigation options to disappear during the mini-game (ie: can't press "Enter/Return" to advance or roll the mousewheel to move back), but am not sure how to code this. I imagine it's done by calling a screen that overwrites the respective keys' functions and is then dismissed at the end of the sequence, but I do not know how to format it.

I put a code sample below to give an idea of how I currently have the system structured, with comments indicating trouble spots:

Code: Select all

label base:
    while (player_win < 3) and (computer_win < 3):
    	"Ready for another round?"
    	## disable text window & hotkey/navigation commands here
    	pause 3.0
    	## randomizer code would go here, with each result leading to a different visual output
    	## and enabling keyboard buttons to allow player to respond. Successes would lead to
    	## "label base.playerwin" losses to "label base.computerwin" ties to "label base.draw"

	label base.playerwin:
	    hide screen RPSchoicekeys
	    show win
	    $ player_win += 1
	    pause 2.0
	    jump base
	label base.computerwin:
	    hide screen RPSchoicekeys
 	    show loss
	    $ computer_win += 1
	    pause 2.0
	    jump base
	label base.draw:
	    hide screen RPSchoicekeys
	    show tie
	    pause 2.0
	    jump base
Any help would be appreciated, even if it involves

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Randomizing Display w/ Corresponding Options (& Disabling U.I.)

#2 Post by Imperf3kt »

Nice coincidence, I'm currently making a very similar thing xD

I currently use the choice menu for the player selections, but I don't plan to keep it that way.
The code I use also won't stay this way forever, but for all intents and purposes, it does work. I believe it's pretty much close to what you're after.
Anyway, if it's of any use to you, here's how I do things: (I've added comments to explain why I have what I have)

Code: Select all

## Self explanatory
define d = Character("Developer")

## Sets up the music to use in game
define audio.balloon = "music/Balloon Game.mp3"

## set the computer selection to nothing
default result = "none"
## set the player selection to nothing
default selection = "none"
## set player victories to zero
default score = 0
## set computer victories to zero
default computer = 0

## This is used as a developer test and will probably make its way into the final game somehow because I miss the late 90's / early 2000s and "cheats" >.>
default persistent.cheating = 0

label start:
    
    ## My game does not use saves. This line sets the "pause menu" aka the game_menu, to the about screen to avoid crashing Ren'Py.
    $ _game_menu_screen = "about"
    ## play the music "Balloon game.mp3"
    play music balloon
    ## an image named rps.jpg is found in the images folder. This sets it as the background.
    scene rps
    ## This is a screen with score statistics. It can be found in the next block of code.
    show screen stats

label rps_select:
    
    ## What to do if a game plays as it should
    if not persistent.cheating:
        menu:
            
            ## Player chooses their "selection" and computer makes a random selection
            "Rock":
                $selection = "rock"
                $result = renpy.random.choice(['rock', 'paper', 'scissors'])
            "Paper":
                $selection = "paper"
                $result = renpy.random.choice(['rock', 'paper', 'scissors'])
            "Scissors":
                $selection = "scissors"
                $result = renpy.random.choice(['rock', 'paper', 'scissors'])
            ## Return the player to the main menu
            "End":
                return
                
    ## What to do if a game allows the player to never lose
    else:
        menu:
            
            ## ingenuity at work >.> sets the computer to a losing move no matter what the player chooses
            "Rock":
                $selection = "rock"
                $result = "scissors"
            "Paper":
                $selection = "paper"
                $result = "rock"
            "Scissors":
                $selection = "scissors"
                $result = "paper"
            "End":
                return
## This label tests the outcomes and decides a win, loss or tie outcome.
label results:

####rock####

    if result == "rock":
        if selection == "rock":
            jump tie
        elif selection == "paper":
            jump win
        elif selection == "scissors":
            jump lose

####paper####

    elif result == "paper":
        if selection == "rock":
            jump lose
        elif selection == "paper":
            jump tie
        elif selection == "scissors":
            jump win

####scissors####

    elif result == "scissors":
        if selection == "rock":
            jump win
        elif selection == "paper":
            jump lose
        elif selection == "scissors":
            jump tie

## Declare a tie and return to the selection menu
label tie:
    
    d "We tied with [result]!"
    jump rps_select

## Declare player the winner and add +1 to player score    
label win:
    
    d "[selection] beats [result], you win!"
    $ score += 1
    jump rps_select

## Declare Computer the winner and add +1 to computer score    
label lose:
    
    d "[result] beats [selection], you lost!"
    $ computer += 1
    jump rps_select
rps.rpy

Code: Select all

screen stats():

## Allow player to click through screen    
    modal False
    ## Keep screen at front of layers
    zorder 100
    
    ## Position a box with some text at the middle, top of the screen.
    vbox:
        xalign 0.5
        ypos 0.01
        text "{color=#000}Computer: [computer] You: [score]{/color}"

## This is called from the main menu [c]textbutton _("Cheats") action ShowMenu("cheats")[/c]
screen cheats():

    tag menu

    use game_menu(_("Cheats"), scroll="viewport"):

        style_prefix "cheats"
        

        hbox:
            vbox:
                ## An original name...
                
               label "Never lose"
               
                ## Set cheat on or off. Player must return to this screen to adjust.
                textbutton _("On") action SetField(persistent, "cheating", 1)
                textbutton _("Off") action SetField(persistent, "cheating", 0)
        
style cheats_label is gui_label
style cheats_label_text is gui_label_text
style cheats_text is gui_text

style cheats_label_text:
    size gui.label_text_size
Now, to stop a player from rolling back and selecting a different option isn't necessary, as the player cannot roll back through the menu after they're returned to the menu, but they CAN roll back as soon as they know the computer's move.
If you feel it necessary to block this entirely, perhaps consult this for further ideas.
https://www.renpy.org/doc/html/save_loa ... k_rollback
And with an example of it in use: https://www.renpy.org/wiki/renpy/doc/re ... k_rollback
An additional link I don't quite understand, but may be of use: https://www.renpy.org/doc/html/config.h ... out_choice

Now, I haven't got that far yet, but adding an image is as simple as adding a "show" and maybe use a transition, during the results label.

Hope this helps, good luck.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Foray
Newbie
Posts: 7
Joined: Sun Jan 21, 2018 8:06 am
Contact:

Re: Randomizing Display w/ Corresponding Options (& Disabling U.I.)

#3 Post by Foray »

What a coincidence! Your code sample helped wonders, and after a few hours of adjusting it to my scenario, I think I'm all sorted out. Thank-you so much!

I have to say, in my limited time here, I have witnessed some of the most prompt, helpful and friendly people on any forum. You are all amazing.

Post Reply

Who is online

Users browsing this forum: Google [Bot]