For a teacher with a class? The classroom edition has the full plan, slides and standards.

Homeschooling PDF

Family edition

Game Builder: Change the Rules with Variables

Ages 9-14 ยท about 45 minutes, in one sitting

Your child plays a short platform game, changes one number, the jump power, and finds the player stuck in a valley it climbed out of a minute ago. They learn that gravity, jump and speed are variables the game reads, find the same numbers in the game's code, and tune the game to feel the way they want. At the end you play it and guess what they changed.

What you need

Before you start: try it once yourself

Break and fix Meadow Run yourself once before you start, about ten minutes. The arrow keys run, Space jumps, R starts the level over and Esc goes back to the editor.

  1. Open the lesson. Go to mytekdev.com/try/gamemaker?lesson=change-the-rules-with-variables&ref=fam-gamebuilder-1. Meadow Run opens in the editor with a short guide along the bottom; press Play, run to the star, then press Esc.
  2. Read the variables. Click Player on the right. The Movement tab shows Gravity 0.5, Jump Power 10 and Move Speed 4.
  3. Break it. Set Jump Power to 8, press Done, then Play, and run right. The player drops into the low valley with the mushroom and cannot jump back out of it.
  4. See it in the code. Set Jump Power back to 10 and press Done. Click the Code Preview bar under the level, then click Game Settings on the right: the code shows player.velocityY = -10 next to jump power and += 0.5 next to gravity; press Done.
  5. Gravity. Set Gravity to 0.8 and play: the player is trapped in the valley again. Then try 0.3, where the player floats and jumps much higher, and set it back to 0.5.
Meadow Run in the test window with Jump Power 8: the player jumps against the wall of the low valley with the mushroom and cannot get over it

Jump Power 8: the same level, and the player cannot get out of the valley.

If you see something else: Code Preview shows the gravity and jump code only after Game Settings is clicked, not Player. Esc switches between the editor and the game, so a second press starts the game again. The line under Player on the right shows the current G, Jump and Spd: check it after every test, because a number not set back spoils the next one.

The ideas, in plain words

A variable is a name a program uses for a value it keeps, so the program can read the value wherever it needs it. In Game Builder, Gravity, Jump Power and Move Speed are variables. Each rule of the game is written once and reads those names, so changing one number changes that rule everywhere in the game, on every level, at once. The level itself, the tiles and the objects, is separate: it does not change when a number does.

The rules run 60 times a second, once a frame. Every frame, gravity is added to the player's falling speed. When Space is pressed and the player is on the ground, its upward speed is set to the Jump Power, and while an arrow key is held, its sideways speed is set to the Move Speed. The rules and the level have to fit each other: the valley in Meadow Run has walls 2 tiles tall, a Jump Power of 10 climbs them, and 8 climbs less than 2 tiles. A bigger gravity does the same thing from the other side, by taking the speed away sooner.

Your child might askA good answer
Why is the jump power negative in the code?On a screen, up means a smaller y. A negative speed moves the player up the screen.
Did I break the game?No, the code still works. The numbers no longer fit the level, so you can fix the number or fix the level.
Why does bigger gravity make the jump lower?Gravity pulls the player down every frame. Pulling harder stops the rise sooner, so the jump is lower.
Why not just put the number in each place it is used?Then you would have to find and change every copy. With a variable you change it once, and everything that reads it changes too.

Do it together

  1. Open mytekdev.com/try/gamemaker?lesson=change-the-rules-with-variables&ref=fam-gamebuilder-1 together. Meadow Run opens in the editor with a short guide along the bottom; follow it together. The arrow keys run, Space jumps, R starts the level over and Esc goes back to the editor. The guide has your child play to the star, open Player, set Jump Power to 8 and play again.

    If you get stuck: Esc switches between the editor and the game, so a second press starts the game again.

    You'll know it worked when: Running right, the player drops into the low valley with the mushroom and cannot jump back out.

  2. Talk about what happened: the level did not change, only one number did. Gravity, Jump Power and Move Speed are variables, named numbers the game reads every time the player moves. The valley walls are 2 tiles tall; a Jump Power of 10 climbs them, and 8 climbs less than 2. Press Esc, click Player, set Jump Power back to 10 and press Done.

    You'll know it worked when: The line under Player on the right shows Jump 10 again, and your child can say why 8 got the player stuck.

  3. Find the numbers in the code. Click the Code Preview bar under the level, then click Game Settings on the right. Press Done when you have looked.

    If you get stuck: Code Preview shows the gravity and jump code only after Game Settings is clicked, not Player.

    You'll know it worked when: The code shows player.velocityY = -10 next to jump power and += 0.5 next to gravity: the same numbers as in the Player box.

  4. Predict, then test. Before touching the app, your child says or writes what Gravity 0.8 will do. Then click Player, set Gravity to 0.8, press Done, press Play and run to the valley. Do the same with 0.3, then set Gravity back to 0.5.

    If you get stuck: Check the line under Player after every test, because a number that was not set back spoils the next test.

    You'll know it worked when: Gravity 0.8 traps the player too, because it pulls harder and the jump is lower. At 0.3 the player floats and jumps much higher.

  5. Now your child makes the game their own, with three rules: change at least two numbers, one at a time; press Play after every change; and a player must still be able to reach the star.

    You'll know it worked when: The game feels different, and your child can still run it from the start to the star.

  6. The real question: when a change makes some part impossible to get past, ask your child to name two different ways to fix it. One is to change the number back a little. The other is to change the level: pick a tile under Tiles on the left and click on the level to draw it, for example a step in the valley wall. Right-click erases.

    You'll know it worked when: Your child names both, a number fix and a level fix. That means they see that the rules and the level work together, and that a variable is one place to change a rule for the whole game.

  7. Trade seats. Your child presses Play first, because the Player box shows the numbers. You play their game for about 3 minutes without pressing Esc and guess which numbers they changed, and whether each went up or down. Then they tell you what really changed. Finish with the big question: how can changing one number change a whole game?

    You'll know it worked when: Your child explains each change they made and what it did to the way the game feels.

Talk about it

Ask these while you work together. A child who has understood the lesson can answer them without help:

The last one is the real test. A child who can fix it by changing the number or by changing the level understands that the rules and the level work together, and that a variable is one place to change a rule for the whole game.

For younger and older kids

Younger: For ages 7 and 8, do the gravity test side by side, and when they make the game their own, change only Jump Power.

Older: For teens, find all three variables in Code Preview, explain in writing why Gravity 0.8 and Jump Power 8 both trap the player, and write a short design note: the feel they wanted, and the numbers that made it.

Keep going

Bring it into other subjects

For your records

Ready to copy into an hour log, a portfolio or a transcript.

LessonGame Builder: Change the Rules with Variables
SubjectsComputer science, Game design
Time45 minutes
What we didChanged the variables that set a platform game's gravity, jump power and speed, predicting each result before testing it, and found the same numbers in the game's code. Tuned the game to a feel of their own and fixed the parts that broke, by changing a number or changing the level.
Skills practicedvariables in a program; predicting, then testing; fixing a problem two ways; tuning how a game feels

Game Builder: Change the Rules with Variables

Activity sheet (optional)

Name: ______________________________ Date: ______________

Open the app: scan the QR code, or go to mytekdev.com/try/gamemaker?lesson=change-the-rules-with-variables

Essential question: How can changing one number change a whole game?

  1. Play it. Press Play. Run with the arrow keys, jump with Space, and reach the star. Esc goes back.
  2. Name the variables. Click Player. Write the three numbers on the Movement tab: Gravity ____ Jump Power ____ Move Speed ____
  3. Predict, then test. Fill in each row of the table: write your prediction first, then change the number, press Done and Play.
  4. Make it yours. Change at least two numbers. A player must still reach the star. Write your final numbers below.
ChangeI predict...What happened
Gravity 0.8
Gravity 0.3
My change 1:
My change 2:

My final numbers: Gravity ____ Jump Power ____ Move Speed ____

1. Which prediction surprised you most, or was wrong? What did you learn from it?

2. My playtester guessed I changed: ________________ (higher or lower?)

Quick check

Name: ______________________________

1. With Jump Power 8 the player is stuck in the valley. What changed? (A) the level, (B) the value of a variable the game reads, (C) the player's picture.

2. Gravity goes from 0.5 to 0.8. The jump gets: (A) higher, (B) lower, (C) the same.

3. In someone's game, the player cannot climb a wall. Name two different ways to fix it.

Answers

QuestionAnswerWhy
1BThe level is the same. Only the number the game reads for the jump changed, and the new jump is too low for the valley wall.
2BGravity pulls the player down. A bigger gravity pulls harder, so the player stops rising sooner.
3Any two of: raise Jump Power; lower Gravity; make the wall shorter; add a step or a platform in front of itAny two fixes that would each work.

If your state or umbrella school asks, this lesson covers CSTA 2017 1B-DA-07, CSTA 2017 1B-AP-15, CSTA 2017 2-AP-17.

Game Builder is a mytekDev app, open source under the GNU General Public License 3.0. The goal star in Meadow Run is from Kenney's UI Pack, released under CC0.