Our Renpy Game Part 5 - Variables, Conditionals and Screens

by IoFAdmin at

renpy | python | programming

Previously On Our Pirate Adventure...

In post #3 we installed Renpy and started our game code and then we set up our git repository for the project in post #4.

Continuing Our Game

In this post we're continuing our game by exploring three new aspects of Renpy:

  1. Variables
  2. Conditionals
  3. Screens

Some Programming Info

These Renpy tutorials are for a wide range of people. Some of you already know how to program and are interested in learning Renpy while others of you don't know much about programming and will need some more guidance and explanation.

If you're familiar with programming, you can skip the Variables and Conditionals sections of this post and go straight to the Screens section. If you're new to writing code, you should read them. Alright enough chit chat. Let's get to work.

So What Are Variables?

Variables in Renpy (and for programming in general) are parts of the code to save values so you can do calculations or display data. For our pirate game, we'll use variables to keep track of the player's name, items he/she has found, and whatever else we need along the way. Once we get to the code below, we'll show how to save the current player's name.

Conditionals Overview

Putting it as simply as possible, conditionals are ifs and elses. If I get up on time, I'll start my work day well. Else, I'll be late and my boss will be angry with me. Conditionals in programming work the same way: if the player has a key, open the door, else have him knock. We'll explore conditionals in our code below.

Renpy Screens

In Renpy, screens are used to group user interface elements like buttons, text, and images. Since the elements are grouped into a unit, they can be hidden or shown together instead of individually. Users interact with a screen's elements instead of the screen itself. The Renpy documentation explains screens in detail. In our code, we'll define a screen that's displayed on top of a background and allow the user to click an image. While Renpy allows you to show many screens at once, we'll start simple with one screen.

Yo Ho Ho! Where's the Code?

Like we did in post #3, I'll show you all of the code before breaking it down into sections that I'll explain. We'll start with script.rpy from post #3. Open that file and then copy/paste the code below into it (replacing everything there).

default player_name = ""
define p = Character("[player_name]")
define s = Character("Squid")
define f = Character("Miss Fluffybottom")
define hasHouseKey = False

label start:
    scene bg insidehouse
    show pirate happy

    $ player_name = renpy.input("What is your name?", length=10)
    $ player_name = player_name.strip()
    if player_name == "":
        $ player_name = "Sheldon"

    p "No Miss Fluffybottom! Wait!"
    "Your cat, the adventurous (and naughty) Miss Fluffybottom runs out the front door of your house. That bad kitty's chasing a bird again! You should probably go after her right meow."

    menu:
        "Go after her.":
            jump house_outside
        "She'll be fine.":
            jump house_inside

    return

label house_inside:
    scene bg insidehouse
    show pirate happy
    p "Just your normal run-of-the-mill house. It's not much but it's home. Mom doesn't seem to be home right now."
    p "Maybe I should go check on Miss Fluffybottom."

    menu:
        "Check on Ms Fluffybottom.":
            jump house_outside

label flower_pot:
    scene bg outsidehouse
    show pirate happy

    if hasHouseKey:
        p "Mom's favorite plant could use some watering but I'll LEAVE it alone right now."
    else:
        $ hasHouseKey = True
        p "Hey what's that? Looks like a house key! Now I have the key to my front door."

    jump front_porch

label house_outside:
    scene bg outsidehouse
    show screen frontPorchScreen
    show pirate happy

    p "There she is down on the beach chasing the seagulls again. Crazy cat! I should probably bring her back in the house before she gets lost or drowns."
    "The door slams shut and you're locked out of the house!"

    menu:
        "Use my key." if hasHouseKey:
            hide screen frontPorchScreen
            jump house_inside
        "Knock on the door." if not hasHouseKey:
            jump door_knock
        "Go after Ms Fluffybottom.":
            hide screen frontPorchScreen
            jump beach_squid

label front_porch:
    scene bg outsidehouse
    show screen frontPorchScreen
    show pirate happy

    p "My front porch."

    menu:
        "Use my key." if hasHouseKey:
            hide screen frontPorchScreen
            jump house_inside
        "Knock on the door." if not hasHouseKey:
            hide screen frontPorchScreen
            jump door_knock
        "Go after Ms Fluffybottom.":
            hide screen frontPorchScreen
            jump beach_squid

label door_knock:
    scene bg outsidehouse
    show pirate happy

    "Knock, knock, knock!"
    p "I guess Mom's not home."

    jump front_porch

label beach_squid:
    scene bg beach
    show squid

    "As you reach for your cat, a sea creature rises out of the sea."
    s "Boo! Sorry... I mean Roar!"
    "The squid, who definitely doesn't speak English, roars and grabs poor Miss Fluffybottom! Today would've been a good day for a catnap but too late for that."

    show pirate happy at right

    p "Let my cat go you under-cooked calamari!"
    f "Meow!!!"
    "The squid, holding your terrified feline, wipes tears from her eyes. (That calamari insult hurt.) She dives under the waves."

    hide squid

    p "Wow! Mom's gonna be mad."

    show squid

    "The giant squid pops her head above the water and wiggles her tentacles in disgust."
    s "Roar! Gurgle. (Yuck! I guess cousin Johnny is right... cats really are nasty.)"
    "The squid spits out the water-logged Miss Fluffybottom who flies through the air and lands on..."
    "a docked pirate ship!"

    p "Ok... correction. Mom's gonna be SUPER mad. Somehow I have to get on that pirate ship and rescue the cat."
    p "I can start at the pier or those palm trees further down the beach."

Even More Code

Next, we'll create a new file. In the same directory as script.rpy, create a new file named myscreens.rpy and copy/paste the following code into it.

screen frontPorchScreen():
    imagebutton:
        xalign 0.75
        yalign 1.0
        idle "flower"
        action Jump("flower_pot")

Last One I Promise

Save the image flower.png in the images directory that we used in post #3. That's it with the changes... time for some explanations.

What Did We Just Do?

default player_name = ""
define p = Character("[player_name]")
define s = Character("Squid")
define f = Character("Miss Fluffybottom")
define hasHouseKey = False

We changed the p character to be player_name and we defined hasHouseKey as False. We define variables and constants outside of any label which is ran before any other code (as our game starts). To understand what's going on with p, let's move on to label start.

label start:
    scene bg insidehouse
    show pirate happy

    $ player_name = renpy.input("What is your name?", length=10)
    $ player_name = player_name.strip()
    if player_name == "":
        $ player_name = "Sheldon"

    p "No Miss Fluffybottom! Wait!"
    "Your cat, the adventurous (and naughty) Miss Fluffybottom runs out the front door of your house. That bad kitty's chasing a bird again! You should probably go after her right meow."

    menu:
        "Go after her.":
            jump house_outside
        "She'll be fine.":
            jump house_inside

    return

The $ player_name lines are running Python code. The renpy.input line asks the player to enter a name that is 10 characters or less in length and the following line is removing blank spaces at the beginning and end of the name. Finally, if the player name is blank ("") set it to Sheldon. In case you were wondering, player_name is a Renpy variable.

label flower_pot:
    scene bg outsidehouse
    show pirate happy

    if hasHouseKey:
        p "Mom's favorite plant could use some watering but I'll LEAVE it alone right now."
    else:
        $ hasHouseKey = True
        p "Hey what's that? Looks like a house key! Now I have the key to my front door."

    jump front_porch

Here we see a Renpy variable again: hasHouseKey. If you remember, we defined hasHouseKey and set it to False at the very top of our script. Here we're using a conditional to see if hasHouseKey is equal to True. (In Renpy and Python, if <variable> is a shortcut for saying "if <variable> is True".) If hasHouseKey is True, we're displaying some text. Else (hasHouseKey is equal to False), we're setting our hasHouseKey variable to True and displaying some other text.

Why are we doing this? Well, inquisitive reader, we are making our code check to see if we have a house key (hasHouseKey is equal to True) and if not, now we do (set hasHouseKey to True). Another way to think about this is to only find the house key once and to display the text about watering the plant if we go to this label more than once (we already have the key).

label house_outside:
    scene bg outsidehouse
    show screen frontPorchScreen
    show pirate happy

    p "There she is down on the beach chasing the seagulls again. Crazy cat! I should probably bring her back in the house before she gets lost or drowns."
    "The door slams shut and you're locked out of the house!"

    menu:
        "Use my key." if hasHouseKey:
            hide screen frontPorchScreen
            jump house_inside
        "Knock on the door." if not hasHouseKey:
            jump door_knock
        "Go after Ms Fluffybottom.":
            hide screen frontPorchScreen
            jump beach_squid

Here's something that we haven't seen before: show screen frontPorchScreen. show <screen_name> tells Renpy to display a screen on top of the current display. We talked about screens in an earlier section but we need some more explanation here. Renpy loads up the screen named frontPorchScreen located in myscreens.rpy

screen frontPorchScreen():
    imagebutton:
        xalign 0.75
        yalign 1.0
        idle "flower"
        action Jump("flower_pot")

Ok so here's our first screen! The imagebutton part is defining a part of the screen. (See Imagebuttons documentation for more info.) An imagebutton is just like what it sounds: an image that we display to the user that acts as a button. xalign 0.75 tells Renpy to display the image at 75% of the way to the right and yalign 1.0 means display the image 100% of the way to the bottom. The idle statement means to display the flower image from our images directory. When the user clicks on the imagebutton, jump to the "flower_pot" label in our script.rpy file. frontPorchScreen is like a variable, since we can use it to refer to the screen.

There's a lot going on here but nothing terribly difficult.

Now we come to the "menu" part of the code.

"Use my key." if hasHouseKey:
    hide screen frontPorchScreen
    jump house_inside

This may look a little weird but it's very simple. If the hasHouseKey is equal to True, display Use my key.

"Knock on the door." if not hasHouseKey:
    jump door_knock

This is the exact opposite of the previous statement: if hasHouseKey is equal to False, display Knock on the door. and jump to door_knock.

"Go after Ms Fluffybottom.":
    hide screen frontPorchScreen
    jump beach_squid

For the last part of this menu, hide the frontPorchScreen screen (the imagebutton of the flower pot will not be shown) and jump to the beach_squid label.

Everything else in the code is something that you've seen before.

Screenshots

Wrapping Up

If you followed along and everything worked correctly, your Renpy game should look very similar to the images above.

Git Repo Link

If you'd like to download the code and images in this post, get them here.

Continue my Renpy journey.

Comments