Level 1Unit B · Session 12
12

Unit B · Telling a computer what to do

Winning, losing, and starting again

What we are making: the shape of a game. A start screen, a way to win, a way to lose, and an ending that says which happened.

Every game has the same four parts

You can already move a sprite, keep a score and notice a collision. What you cannot yet do is end. A game that never ends is a toy.

  1. The start. Everything goes back to where it belongs. Score zero, lives full, sprites in place.
  2. The play. The forever loop. Move, check, score.
  3. The end. Something becomes true — the score hits ten, the lives hit zero, the timer runs out.
  4. The verdict. The game says which happened. Won, or lost.
💡 Tip
Part four is the one everybody leaves out. A game that simply stops leaves the player wondering whether it broke. One say block is the difference between a finished game and a confusing one.
New words
Win conditionThe thing that has to become true for the player to win.
Lose conditionThe thing that ends it badly. Lives at zero, or time up.
stop allEnds every script at once. Use it after you have said what happened.
TimerA variable counting down. Ends the game at zero.
broadcastOne sprite telling all the others something has happened.

Do it — a way to win

Put this inside the forever loop you already have, underneath the catching part.

One line for the win, one for saying so
if score = 10 ? then
say You win! for 3 seconds
stop all
📷
images/l1-s12-script-win.png
Screenshot: the win check sitting inside the forever loop, below the touching check
  1. Add it. Play until you win. Read what it says.
  2. Take out the say block and play again. It just freezes. That is what the player sees when you forget part four.
  3. Put it back, and change 10 to 3 so you can test it quickly.

Do it — a way to lose

Losing needs something to lose. Make a variable called lives and set it to 3 under the green flag, next to set score to 0.

Lose a life, and lose the game
when green flag clicked
set score to 0
set lives to 3
forever
if touching Rock ? then
change lives by -1
go to x: 0 y: -120
if lives = 0 ? then
say Game over for 3 seconds
stop all
📷
images/l1-s12-script-lives.png
Screenshot: the whole lose branch, with lives showing on the stage
⚠ Careful
Notice the sprite jumps back to the start after losing a life. Without that it is still touching the rock on the next loop, and all three lives vanish in a tenth of a second. Same problem as the apple in session 11, same fix.

Do it — a clock instead

Some games are not won or lost, they are timed. Score as much as you can in thirty seconds. This is a separate script, on any sprite.

A countdown, running alongside the game
when green flag clicked
set time to 30
repeat until time = 0
wait 1 seconds
change time by -1
say Time up! for 3 seconds
stop all
📷
images/l1-s12-script-timer.png
Screenshot: the timer script beside the play script, both on the stage at once
💡 Tip
Two scripts really do run at the same time. The timer counts while the player is playing, because both have their own green-flag hat. This is the first time your program has done two things at once, and it is worth noticing.

Do it — a start screen

A game that begins the instant the flag is clicked catches the player out. Make it wait.

Say what to do, then wait to be told to go
when green flag clicked
say Arrow keys to move. Press space to start.
wait until key space pressed ?
say
forever
📷
images/l1-s12-start-screen.png
Screenshot: the stage showing the instruction before the game has started
💡 Tip
This one block will save you in session 15. The commonest thing a tester does is sit and stare, because nothing told them what to press. A game that says so does not need you standing next to it.

Do it — put all four together

Twenty minutes. Take the catching game from session 11 and give it the full shape.

  1. A start screen that says what to press.
  2. Score, and a win at 10.
  3. Lives, and a loss at 0. Or a timer, if you prefer.
  4. A say block for each ending, so the player knows which happened.
  5. Play it twice in a row and check the second game is identical to the first.
📷
images/l1-s12-finished-game.png
Screenshot: a finished small game with score and lives visible on the stage

Swap and play

  1. Swap computers with another team. Say nothing at all.
  2. Play theirs. Can you tell how to start? Can you tell when you have won?
  3. Write down the first two things that confused you.
  4. Swap back, read your list, and fix those two things only.
What you seeWhyWhat to do
The game freezes instead of endingstop all with nothing said first.Put a say block before it. Always.
All three lives go at onceThe sprite is still touching on the next loop.Move it away after losing a life, as in the code above.
The win never triggersThe score jumped past the number.Use > rather than =, or make sure the score only ever goes up by one.
The timer does not countIt is inside the play loop.It needs its own green-flag script, separate from the game.
The second game is wrongSomething was not set at the start.Score, lives, time and every sprite's position. All of them, every time.

Save it properly

  1. File → Save to your computer, into Documents → Robotics.
  2. Name it session12-game.
  3. Open it again from the file manager to prove it saved.

End of Unit B

  • A game has four parts: the start, the play, the end, and the verdict.
  • A win condition and a lose condition are both just an if.
  • Say what happened before you stop. Never freeze silently.
  • A timer is a variable counting down in a script of its own.
  • A start screen is one wait until, and it fixes most testing problems.
  • Everything resets at the start. Score, lives, time, every sprite.

You now have the whole shape of a game and all the pieces that go in it. Project 1 starts next session, and it is yours from here.

Challenge optional — only if you finish early
  • Add a second level: when the score reaches 10, the game speeds up instead of ending.
  • Make the game say the final score in the same sentence as the verdict.
11. When things touch, and starting again