Variables, what are they?

Step 5
Step completed?

We have learned to write strings. But, to make a game, there will be an important notion to know: variables.

Delete your code (you can save it into another file if you want to reuse it later), and put this code instead. Remember to save to run it on your Gamebuino.

from gamebuino_meta import begin, waitForUpdate, display, buttons, color

while True:
    waitForUpdate()
    display.clear()
    
    name = "Bob"
    
    display.print("My name is")
    display.print(name)
    
    display.print("\n")
    display.print("\n")
    
    name = "Jack"
    
    display.print("My name is")
    display.print(name)

Two sentences are displayed on the screen. As you look at the code, you may notice that the instructions for displaying the text are the same! The only difference is that we use what is called a variable.

But what's the point?

A variable, as its name suggests, is subject to change. It has a name, here name, as well as a value. Its value is first of all equal to "Bob". display.print(name) therefore displays "Bob" on the screen. Then we change its value to "Jack". display.print(name) now displays "Jack" on the screen!

There is a difference: when you use display.print to display a variable, you don't put quotation marks (otherwise, the program would understand that it is a string of characters and would literally display name instead of the value of the variable)

Challenge #3

Try to modify this code to display other first names instead of "Bob" and "Jack". It's very simple! But let's go a little further. Try to create a second variable that we will call "score". Finally, try to display the following result on the screen, remembering everything we have learned so far:

My name is Bob

My score is 15

My name is Jack

My score is 8

Need help?

  • The principle is the same as in the example above, except that we now have 2 variables. If you follow the rule of order of instructions, you should be able to do so.
  • You can use the line break seen in the previous step: display.print("\n")
  • You can also change the text color with: display.setColor(color.BLUE)

Expected result

Solution

See
from gamebuino_meta import begin, waitForUpdate, display, buttons, color

while True:
    waitForUpdate()
    display.clear()
    
    name = "Bob"
    score = 15
    
    display.print("My name is")
    display.print(name)
    
    display.print("\n")
    
    display.print("My score is")
    display.print(score)
    
    display.print("\n")
    display.print("\n")
    
    name = "Jack"
    score = 8
    
    display.print("My name is")
    display.print(name)
    
    display.print("\n")
    
    display.print("My score is")
    display.print(score)

Explanations

We first defined our two variables: name and score. Small difference: when it is a number, and not a string, do not put quotation marks! Then we wrote our two sentences, implementing our variables, and a line break. Then we modified the values of the two variables, and again called the instructions to write our sentences. The two sentences are displayed again, but with different values!

Go further

Have fun! In our example, everything is in blue, but you can try using different colors. We will use the variables a lot later on, so try to create others in order to understand how they work, for example to display the number of lives of your player (we want to make a game after all!), or other information...

In need of inspiration? Look what we've done:

See
from gamebuino_meta import begin, waitForUpdate, display, buttons, color

while True:
    waitForUpdate()
    display.clear()
    
    name = "Brian"
    score = 15
    lives = 3
    
    display.setColor(color.BLUE)
    display.print(name)
    
    display.setColor(color.GREEN)
    display.print(" (lives:")
    display.setColor(color.RED)
    display.print(lives)
    display.setColor(color.GREEN)
    display.print(")")
    
    display.print("\n")
    display.print("\n")
    
    display.setColor(color.WHITE)
    display.print("Score:")
    display.print(score)
    
    display.print("\n")
    display.print("\n")
    
    lives = 2
    
    display.print("I lost a life!")
    
    display.print("\n")
    display.print("\n")
    
    display.setColor(color.GREEN)
    display.print("Remaining lives:")
    display.setColor(color.RED)
    display.print(lives)

Note that a variable, as its name suggests, is not fixed! Here, we had 3 lives at the beginning, then we lost one during the program. At the end we display the new value! The most important thing is to follow the order of the instructions.

It's all very well to write text, but what if we programmed a real game now?

Steps