Page 1 of 1

Variable not working as expected

Posted: Sun Jan 14, 2018 1:28 am
by Imperf3kt
For various reasons I am making a rock paper scissors style game.
I did some googling before attempting and found one game folder on github that was made with the deprecated ui. functions.
Though it does work flawlessly, I don't wish to use the code. Its also not as customisable as I require.

I found another post by PyTom, that doesn't work with the version of Ren'Py I am using (6.99.14.3134)

So I resigned to making my own.
The base "game" was simple to make, only took a few minutes of fiddling around and double-checking in the documentation.
My issue comes in when I try to customise how it looks.

What I am trying to do is have a bar on the left with a number inside it reflecting your total number of wins. I then plan to duplicate the bar for losses and ties.
After a certain number of wins (currently 1 for testing purposes) the bar is supposed to switch out with a flaming bar for visual effect.
My issue though is that the flaming bar is appearing instead of the non-flaming bar, despite me not even "winning" any games and I cannot tell why. I checked and "wins" is definitely registering 0.

Here is the code I've been pulling my hair out over for the last ~4 hours

Code: Select all

define d = Character("Developer")

## 10fps
image bar_animated:
    "gui/interface/00_bar_hot.png"
    pause 0.1
    "gui/interface/01_bar_hot.png"
    pause 0.1
    "gui/interface/02_bar_hot.png"
    pause 0.1
    "gui/interface/03_bar_hot.png"
    pause 0.1
    "gui/interface/04_bar_hot.png"
    pause 0.1
    "gui/interface/05_bar_hot.png"
    pause 0.1
    "gui/interface/06_bar_hot.png"
    pause 0.1
    "gui/interface/07_bar_hot.png"
    pause 0.1
    "gui/interface/08_bar_hot.png"
    pause 0.1
    "gui/interface/09_bar_hot.png"
    pause 0.1
    "gui/interface/10_bar_hot.png"
    pause 0.1
    "gui/interface/11_bar_hot.png"
    repeat

default result = "none"
default rps_opponent = "1"

default selection = "none"
default wins = "0"

label start:
    
    scene rps
    show screen stats

label rps:

    menu:
        "Rock, Paper, Scissors!"
        
        "Rock":
            $selection = "rock"
            $rps_opponent = renpy.random.randint(1, 3)
        "Paper":
            $selection = "paper"
            $rps_opponent = renpy.random.randint(1, 3)
        "Scissors":
            $selection = "scissors"
            $rps_opponent = renpy.random.randint(1, 3)
        "End":
            jump fin
    
label results:

####rock####
    
    if rps_opponent == 1 and selection == "rock":
        $ result = "rock"
        jump tie
        
    elif rps_opponent == 1 and selection == "paper":
        $ result = "rock"
        jump win
        
    elif rps_opponent == 1 and selection == "scissors":
        $ result = "rock"
        jump lose

####paper####

    if rps_opponent == 2 and selection == "rock":
        $ result = "paper"
        jump lose
        
    elif rps_opponent == 2 and selection == "paper":
        $ result = "paper"
        jump tie
        
    elif rps_opponent == 2 and selection == "scissors":
        $ result = "paper"
        jump win

####scissors####

    if rps_opponent == 3 and selection == "rock":
        $ result = "scissors"
        jump win
        
    elif rps_opponent == 3 and selection == "paper":
        $ result = "scissors"
        jump lose
        
    elif rps_opponent == 3 and selection == "scissors":
        $ result = "scissors"
        jump tie

label tie:
    
    d "We tied with [result]!"
    jump rps
    
label win:
    
    d "[selection] beats [result], you win!"
    $ wins += 1
    jump rps
    
label lose:
    
    d "[result] beats [selection], you lost!"
    jump rps
    
label fin:
    return

Code: Select all

screen stats():
    
    modal False
    zorder 100
    if wins >= 1:
        vbox:

            yalign 0
            xalign 0

            add "bar_animated"
    else:
        vbox:

            yalign 0
            xalign 0
            add "gui/interface/bar.png"

I've tried deleting persistent, adding [wins] to the script to check it is indeed "0", recompiling all files, etc.
I cannot work out why the wrong image is showing.
If I reverse the if to if wins <= 1: then I get the opposite effect, the flaming bar never shows, despite winning numerous times. Only the regular bar shows instead.

Re: Variable not working as expected

Posted: Sun Jan 14, 2018 2:45 am
by Ocelot
default wins = "0" Here wins is a string. In your screen it compare it to integer. And string is always larger than any integer. So there is your problem. I am curious, why don't you have any errors: wins += 1 should result in TypeError

Re: Variable not working as expected

Posted: Sun Jan 14, 2018 4:09 am
by Imperf3kt
Not sure myself but I knew it had something to do with that line.

Thanks for pointing it out, I'll go make the required adjustments.

E: Perfect! Solved the problem instantly. Slipped my mind somehow.
I also fixed up the default rps_opponent = 1 line

to anyone out there pretty new to Ren'Py or Python, what was done was simply remove the quotes "


Oh and of course, if anyone happens upon this in the future, feel free to use it for anything you like. Its only made up of the most basic Ren'Py language.
I'm going to see if I can't optimise it somewhat :)

Re: Variable not working as expected

Posted: Sun Jan 14, 2018 4:28 am
by Ocelot
I'm going to see if I can't optimise it somewhat :)
You can ditch rps_opponent, and use only result by doing:
$ result = ['rock', 'paper', 'scissors'][renpy.random.randint(1, 3)]

Re: Variable not working as expected

Posted: Sun Jan 14, 2018 3:13 pm
by IrinaLazareva
)classified(

Re: Variable not working as expected

Posted: Sun Jan 14, 2018 3:18 pm
by IrinaLazareva
and classic...

Code: Select all

#Rock Paper Scissors

default situation = ['Scissors cuts Paper', 'Paper covers Rock', 'Rock crushes Scissors']
default mwinner = [('Scissors', 'Paper'), ('Paper', 'Rock'), ('Rock', 'Scissors')]

image implayer1 = ConditionSwitch(
    'player1==None', Animation('rock.png', .5, 'paper.png', .5, 'scissors.png', .5),
    'player1=="Rock" ', 'rock.png',
    'player1=="Paper" ', 'paper.png',
    'player1=="Scissors" ', 'scissors.png',
    )
image implayer2 = ConditionSwitch(
    'player2==None', Animation('rock2.png', .5, 'paper2.png', .5, 'scissors2.png', .5),
    'player2=="Rock" ', 'rock2.png',
    'player2=="Paper" ', 'paper2.png',
    'player2=="Scissors" ', 'scissors2.png',  
    )

screen statgame():
    frame:
        align (.5,.1)
        grid 2 2:
            add 'implayer1' size(150,150) # null   # <- replace for test without images
            add 'implayer2' size(150,150) # null   # <- replace for test without images
            text 'You: [player1]'
            text 'Computer: [player2]'
    frame:
        align (.5, .35)
        for i in range(len(situation)):
            if (player1, player2)==mwinner[i]:
                vbox:
                    text situation[i] color "#3f0"
                    text "You win!"
            elif (player2, player1)==mwinner[i]:
                vbox:
                    text situation[i] color "#fe0"
                    text "You lost!"
                    
label letsbegin:
    $ player1 = None
    $ player2 = None
    "Ready!"
    show screen statgame
    menu:
        "Rock! Paper! Scissors!"
        'Rock':
            $ player1 = 'Rock'
        'Paper':
            $ player1 = 'Paper'
        'Scissors':
            $ player1 = 'Scissors'
    $ player2 = renpy.random.choice(['Rock', 'Paper', 'Scissors'])        
    pause
    hide screen statgame
    menu:
        "Replay?"
        "Yep":
            jump letsbegin
        "Nope":
            pass        
    return

Re: Variable not working as expected

Posted: Sun Jan 14, 2018 4:36 pm
by Imperf3kt
Ocelot wrote: Sun Jan 14, 2018 4:28 am
I'm going to see if I can't optimise it somewhat :)
You can ditch rps_opponent, and use only result by doing:
$ result = ['rock', 'paper', 'scissors'][renpy.random.randint(1, 3)]
Thanks a bunch! I need to learn more Python, I barely understand the basics. I mostly rely on logic I learnt through HTML, CSS and the tiny bit of JavaScript I touched upon before I discovered Ren'Py :P


IrinaLazareva, both of those look quite complex but thank you for posting them. I might be able to understand how better to do certain things by studying them.

Certainly, I intend to convert from a choicemenu, to imagebuttons eventually, which may not turn out quite as optimal as it currently is.