0% found this document useful (0 votes)
27 views41 pages

Countdown Clock for Rocket Launch

This document contains lessons on Python programming concepts like while loops, numbers and calculations, loops in action, loops and selection, functions, and using sound in a game. The lessons are broken into stages and include tasks to complete and possible solutions. Lesson 1 covers while loops and includes stages to set up a rocket launch countdown scene, create variables to store time, import the sleep command, add a while loop to countdown from 9 to 0 seconds, make the rocket take off at 0 seconds, and debug the countdown.

Uploaded by

Gracia Omari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views41 pages

Countdown Clock for Rocket Launch

This document contains lessons on Python programming concepts like while loops, numbers and calculations, loops in action, loops and selection, functions, and using sound in a game. The lessons are broken into stages and include tasks to complete and possible solutions. Lesson 1 covers while loops and includes stages to set up a rocket launch countdown scene, create variables to store time, import the sleep command, add a while loop to countdown from 9 to 0 seconds, make the rocket take off at 0 seconds, and debug the countdown.

Uploaded by

Gracia Omari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LESSON SOLUTIONS

Level 2

[Link]
1
Contents

Lesson 1- while Loops.....................................3 Lesson 4- Loops and Selection.......................20


Stage 1- Set the Scene........................................ 3 Stage 1- ‘while’ loops and ‘break’ statements
Stage 2- Create variables.................................... 4 ...................................................................20
Stage 3- Importing the sleep command......... 5 Stage 2- Importing libraries............................... 20
Stage 4- Add a while loop.................................. 5 Stage 3- Beginning a magic-8-ball game...... 21
Stage 5- PMake the rocket take off.................. 6 Stage 4- Choose a random answer................. 21
Stage 6- Debug...................................................... 6 Stage 5- Add elif/else statements.................... 22
Challenge.............................................................7 Stage 6- Debug...................................................... 23
Challenge.............................................................24
Lesson 2- Numbers and Calculations............8
Stage 1- Set the Scene........................................ 8 Lesson 5- Functions..........................................25
Stage 2- Instructions............................................ 8 Stage 1- Customize the scene........................... 25
Stage 3- Variables to store the lengths.......... 9 Stage 2- Create variables and import .
Stage 4- Convert the variables to integers.... 9 modules.................................................. 26
Stage 5- Is it an equilateral triangle?............... 10 Stage 3- Ask the user to guess......................... 26
Stage 6- Is it an isosceles triangle?................. 10 Stage 4- Check if they got it right.................... 27
Stage 7- It must be a scalene triangle............. 11 Stage 5- Expand the ‘if’ statement.................. 27
Stage 8- Refine the code to anticipate user . Stage 6- Finish the ‘if’ statement...................... 28
errors....................................................... 12 Tasks.....................................................................28
Stage 9- Debug...................................................... 13 Solution................................................................28
Stage 10- Monty munches................................. 12 Stage 4- Check if they got it right.................... 29
Challenge.............................................................13 Stage 5- Expand the ‘if’ statement.................. 30
Challenge.............................................................31
Lesson 3- Loops in Action...............................15
Stage 1- A basic loop........................................... 15 Lesson6- Using sound in a game...................31
Stage 2- Start and stop....................................... 15 Stage 1- Set the scene......................................... 32
Stage 3- Step size................................................. 16 Stage 2- Add a character.................................... 33
Stage 4- A range inside a range....................... 16 Stage 3- Initialize the game................................ 33
Stage 5- Square numbers - debug.................. 17 Stage 4- Add a loop.............................................. 34
Stage 6- Times tables.......................................... 17 Stage 5- Handle the user choice 1................... 35
Stage 7- Interacting with the user................... 18 Stage 6- Handle the user choice 2................... 36
Stage 8- Formatting the times table................ 18 Stage 7- Add visual effects................................ 37
Challenge.............................................................19 Stage 8- Debug...................................................... 39
Challenge.............................................................40

2
Lesson 1 – While Loops

Stage 1- Set the Scene

Set up a space-themed scene with a clock for the launch countdown.

Tasks

• Choose a background image.


• Add a rocket sprite and name it ‘rocket’.
• Add a digital clock sprite and name it ‘clock’.
• Add two text objects, called ‘minutes_text’ and ‘seconds_text’.
• Set their text to show ‘0’ and place these in the appropriate sections of the clock –
‘minutes_text’ on the left and ‘seconds_text’ on the right.

Solution

3
Stage 2- Create variables

Create variables to store the time.

Tasks

• Create a variable called ‘minutes’ and initialise it to 0.


• Create a variable called ‘seconds’ and initialise it to 9.
• Set the text property of the element minutes_text to the value of minutes.
• Set the text property of the element seconds_text to the value of seconds.
• Run your program and check that the clock displays ‘0:9.’

Solution

4
Lesson 1: While Loops
Stage 3- Importing the sleep command
To use timings in Python, we need to import the ‘sleep’ procedure from the ‘time’ module.

Tasks
• Import the ‘sleep’ procedure from the ‘time’ module. This should go at the top of your
program.

Solution

Stage 4- Add a while loop


A while loop runs over and over until a condition is met. We want the loop to run to do a
countdown until ‘seconds’ is zero.
We’ll also use a ‘sleep’ command to make the loop pause so that you can see the countdown
happening

Tasks

• Add a while loop that runs while ‘seconds’ is greater than 0.


• Inside the loop, first use a ‘sleep’ command to make the code pause for a second each
time.
• Inside the loop, reduce the value of the variable ‘seconds’ by one each time.
• Inside the loop, display the new value of ‘seconds’ in ‘seconds_text’.
• Run your code and check that the clock values count down from 0:9 to 0:0.

Solution

5
Lesson 1: While Loops
Stage 5- Make the rocket take off
Set the rocket to take off when the countdown reaches 0:0

Tasks

• When the loop has finished make the rocket move up using the up() method.
• Test your program - does the counter count down to zero and then the rocket move?

Solution

Stage 6- Debug
In this program, the countdown is not running correctly. The rocket moves up immediately.

Tasks

• Debug the code to make the countdown run properly. It should count down in seconds
from 9 to 0.

Solution

6
Lesson 1: While Loops
Challenge

Try out the following challenges.

Suggestions
• Add an explosion image and make it invisible at the start.
• Make the rocket explode one second after take-off.
• And a sound to play along with the explosion.
• Make the clock start counting down when you click the ‘Launch’ button.

Possible Solution

7
Lesson 2 – and/or

Stage 1- Set the Scene

First let’s set up the design view.

Tasks
• Choose a background
• Add a sprite of your choice to be the main talking character. Name the sprite ‘character’
and position it near the bottom of the screen.
• Add three triangle sprites of different types (equilateral, isosceles and scalene).
• Name your triangles ‘tri_equ’, ‘tri_iso’ and ‘tri_sca’.

Stage 2- Instructions

Let’s make the character say the instructions

Tasks
• Make your character show a speech bubble for 5 seconds, which says ‘Tell me the three
side lengths of your triangle’. Use the ‘say’ function to do this

Solution

8
Lesson 2 – and/or
Stage 3- Variables to store the lengths

We need to ask the user to enter the lengths of the sides of their triangle.
We will store these in variables to be used later.

Tasks

• Use three ‘input()’ commands to ask the user to input the lengths of each side (x, y and
z). The first prompt should say ‘Enter x: ‘, and so on.
• Store the values in variables called ‘x’, ‘y’ and ‘z’.
• Run your program to test it. Use x = 10, y = 10, z = 10. The debug panel (bottom-right)
should show that you have three variables set to “10”.

Solution

Stage 4- Convert the variables to integers

In Python, the string “10” is different to the number 10.


First, we need to convert each of the variables to integers before we can use them as lengths in
our code.

Tasks
• Use the ‘int()’ function to convert each of your variables to integers - for example ‘x =
int(x)’
• Run your program to test it. Use x = 10, y = 10, z = 10. Check that the debug panel (at
the bottom right) shows that your variables are integers now.

Solution

9
Lesson 2 – and/or
Stage 5- Is it an equilateral triangle?

We can use ‘if’ and ‘and’ to check if the user has described an equilateral triangle (one with all
three sides equal)
We’ll check if ‘x’ is equal to ‘y’ and ‘x’ is also equal to ‘z’.

Tasks
• Use one ‘if’ statement that tests whether ‘x’ is equal to ‘y’ and ‘x’ is also equal to ‘z’
• If this is true your character should say ‘Equilateral triangle’ for 5 seconds.
• Run your program to test it. Use x = 10, y = 10 and z = 10. Check that your character
says ‘Equilateral triangle’.

Solution

Stage 6- Is it an isosceles triangle?

We can use further statements to check if the triangle is isosceles.


An isosceles triangle has two sides that are equal. It could be x and y, or y and z, or z and x

Tasks
• Add one ‘elif’ statement which checks if x and y are equal, or if y and z are equal, or if z
and x are equal.
• If this is true the character should say ‘Isosceles triangle’ for 5 seconds.
• Run your program to test it. Use x = 10, y = 10 and z = 15. Check that your character
says ‘Isosceles triangle’.

Solution

10
Lesson 2 – and/or
Stage 7- It must be a scalene triangle

If it’s not equilateral or isosceles it must be a scalene triangle.

Tasks
• Add one ‘else’ statement at the end of your program. Inside, the sprite should say
‘Scalene triangle’ for 5 seconds. Your program should now contain one ‘if’ statement, one
‘elif’ statement and one ‘else’ statement.”
• Run your program and enter x = 10, y = 12 and z = 15. Check that your character says
‘Scalene triangle’.

Solution

11
Lesson 2 – and/or
Stage 8- Refine the code to anticipate user errors.

Run the code and enter some letters instead of a number. You will get an error message and the
program will fail.
We need to check if the user has entered only digits (0-9) using the ‘isdigit’ function.

Tasks
• Before converting each variable to an integer, write an ‘if’ statement and use the
‘[Link]()’ function to check that ‘x’ and ‘y’ and ‘z’ contain only digits.
• Move the rest of your code that identifies the triangle inside the ‘if’ statement. Use an
‘else’ statement to make the character say ‘Please enter only numbers’ if this check fails.

Solution

12
Lesson 2 – and/or
Stage 9- Debug

There is a logical error in this code - for some triangles it gives the wrong answer. What is wrong
with it?

Tasks
• Test this code using measurements for the three types of triangles. What is wrong? Fix
the error!
• Check that your code works. Use x = 10, y = 20, z = 10. Check that your character says,
‘Isosceles triangle’.

Solution

Challenge

Add in code to show the correct triangle and hide the others.

Suggestions
• Set all the triangle images to hide at the start of the program.
• Add code to show the relevant type of triangle when it has been identified.
• Some input is invalid - for example x = 1, y = 1, z = 100. There is simply no way to draw
a triangle like that. Include a way of checking this in your program.

13
Lesson 2 – and/or

14
Lesson 3- Loops in Action

Stage 1 – A basic loop

The ‘range’ keyword returns a special Python object that represents a sequence of numbers.
‘range(N)’ gives you a sequence of ‘N’ numbers starting from zero:
The number ‘N’ is not included

Tasks
• Create a ‘for i in range(N)’ loop for which ‘i’ takes the values 0, 1, 2, 3..., 100. What is the
correct value of ‘N’? Print the value of ‘i’ each time.

Solution

Stage 2- Start and stop

You don’t have to start at zero, you can specify the ‘START’ and ‘STOP’ values for a range.
Range(START, STOP)’ will start from ‘START’, and go up to ‘STOP’.
The number STOP is not included - it stops before the number STOP is reached.

Tasks
• Create a ‘for i in range(START, STOP)’ loop for which ‘i’ takes the values 1 up to 8. What
are the correct values for ‘START’ and ‘STOP’? Print the value of ‘i’ each time.
• Create a ‘for j in range(START, STOP)’ loop for which ‘j’ takes the values -3 up to 3. Print
the value of ‘j’.

Solution

15
Lesson 3- Loops in Action
Stage 3- Step size

You don’t have to go in steps of 1 each time, you can step by any amount.

Tasks
• Write a ‘for i in range’ loop that prints the numbers: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21
• Write a ‘for j in range’ loop that prints the numbers: 100, 103, 106, 109, 112, 115.
• Write a ‘for k in range’ loop that prints the numbers: -10, -6, -2, 2, 6, 10.

Solution

Stage 4- – A range inside a range

We don’t have to just print things inside a range, we can use other code too.
We can even place a range inside another range.

Tasks
• Create a ‘for i in range’ loop where ‘i’ takes the values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
• Inside the loop, create a ‘for j in range’ loop where ‘j’ takes the values 0, 1, 2, 3, 4, 5, 6, 7.
• In design mode there is a sprite called ‘arrow’. Inside your loop, set the ‘x’ position of
‘arrow’ to ‘100 × i’ and set its ‘y’ position to ‘100 × j’.
• Run your code. Check that the arrow moves across and down the screen.

Solution

16
Lesson 3- Loops in Action
Stage 5- Square numbers - debug
Someone has written some code to print the square numbers from 3 squared (9) up to 6 squared
(36), and to draw pictures of each one.
It’s not working at the moment. See if you can fix it.

Tasks
• Fix the code. When you’ve finished it should print the square numbers from 3 squared
(9) up to 6 squared (36), and each one should have a picture made up of O’s to
accompany it.

Solution

Stage 6- Times tables

In the next stages we will create an activity based around times tables.
Using a for loop and a range we can print out a whole set of times-tables quite easily.

Tasks
• Create a ‘for i in range’ loop where ‘i’ takes the values: 1, 2, 3, ... , 12.
• Inside, print the value of ‘i’ × 5.
• Check that it prints the five times table, from 5 up to 60.

Solution

17
Lesson 3- Loops in Action
Stage 7- Interacting with the user

To improve our program we can ask the user what times table they would like to print.
Remember that when you get numerical input you need to convert it to an integer first.

Tasks
• At the top of your program, use an ‘input’ command to ask the user ‘Which times table
should be printed?’ Create a variable called ‘number’ and store the user’s response in it.
• Convert ‘number’ to an integer. Use the ‘int’ function to do this.
• Edit your for loop it so that it prints the first 12 entries of the selected times table,
starting with 1 × number.
• Test your code by entering ‘7’. Your code should print the first 12 members of the 7
times-table: 7, 14, 21, ... 84

Solution

Stage 8- Formatting the times table

It will be more user-friendly to print the times-tables as sentences.


For example “5 times 7 is 35, 6 times 7 is 42 etc...”

Tasks
• Edit your code so that the print out says (for example) ‘6 times 7 is 42’, each on a new
line. The easiest way to do this is to write: ‘print(i, “times”, number, “is”, ...)
• Test your code by printing the first 12 entries from the 7 times table again, from ‘1 times
7 is 7’ up to and including ‘12 times 7 is 84’.

Solution

18
Lesson 3- Loops in Action
Challenge

Have a go at these challenges to improve your magic 8 ball game even more!

Suggestions
• Use the ‘isdigit’ method to check if ‘number’ consists of only digits before trying to
convert it to an integer. If not, provide the user with an error message.
• Choose a background and add a sprite to the screen called ‘character1’. Make
‘character1’ show a speech bubble saying “Which times-table do you want to know?”.
• After a few seconds ask the user to input the value of ‘number’.
• Add another sprite called ‘character2’ and make them recite the times table in speech
bubbles, with a gap of 1 second between each one.
• Try to add more user input - so that the user can say which times table, where to start
and where to end for example.

19
Lesson 4- Loops and Selection

Stage1- ‚while‘ loops and ‚break‘ statements


In Python, ‘while’ loops continue repeating the code inside them while a test is true.
The ‘break’ statement in Python can be used to break out of a loop.

Tasks
• Create a variable called ‘question’ and set it equal to 10.
• Create a while loop which runs forever. In Python you can write ‘while True:’ to achieve
this.
• Inside the loop, print the value of ‘question’ and then reduce ‘question’ by one.
• Write an ‘if’ statement that checks if ‘question’ is zero. If so, use ‘break’ to quit the loop.
• At the end of your program, print ‘Done’ so that you can check the code doesn’t run
forever.

Solution

Stage 2- Importing libraries


Let’s start making the game itself, using a while loop.
First, Python has built-in libraries with procedures that we can use and we need to ‘import’ them.

Tasks
• Import the ‘random’ library. This should go at the top of your program.
• Run your program and check it still works.

20
Lesson 4- Loops and Selection
Stage 3- Beginning a magic-8-ball game

The magic-8-ball will ask the user to think of a question and will answer using a random
response, until they quit.

Tasks
• Keep your ‘while’ loop but remove the code where ‘question’ is set to 10, the code where
‘question’ is printed, and the code where it is reduced by 1.
• Inside the ‘while’ loop, use the ‘input’ function to tell the user ‘Ask the magic 8 ball a
question: (leave blank to quit)’ and assign that to the ‘question’ variable. Each time the
loop runs the user can ask a new question.
• Next, use an ‘if’ statement to check if ‘question’ is equal to an empty string (‘’ or “”). If so,
‘break’ out of the loop.
• Test the code, does it ask you for a question and then keep doing so until you leave the
response empty?

Solution

Stage 4- Choose a random answer

If ‘question’ is not empty, we can use an ‘else’ statement to continue the game. We will use
random numbers between 1 and 8 to generate an answer from the magic 8 ball.

Tasks
• Write an else statement after your ‘if’ statement. The code inside will execute if ‘question’
is not empty.
• Inside, make a variable called ‘answer’ and use [Link] to assign it a random
number between 1 and 8 inclusive.
• Test your code by asking the question “Will it rain tomorrow?” a few times. Check that
you can see the random values of ‘answer’ in the debug panel (bottom right). When
you’re ready leave the question blank to exit the loop.

Solution

21
Lesson 4- Moving Sprites
Stage 5- Add elif/else statements

We can use if/elif statements to print the eight possible answers that the 8 ball might give.

Tasks
• Use an ‘if’ statement to print “The magic 8-ball says: Definitely not” if ‘answer’ equals 1.
• Use six ‘elif’ statements to check if ‘answer’ is equal to 2, 3, 4, 5, 6 or 7, and print
appropriate responses of the form “The magic 8-ball says: ...”
• Use a final ‘else’ statement at the end to print an appropriate response if ‘answer’ equals
8.
• Test your code by asking the 8 ball “Will it rain tomorrow?”. When you’re ready, leave the
question blank to exit the loop.

Solution

22
Stage 6- Debug

There is a bug with this code. The answers are meant to be random but a lot of the time it says
‘Ask me again later’.
Can you see why?

Tasks
• Fix the bug - make sure that it always gives a different random response for every
question.
• Test your code a few times. When you’re ready, leave the question blank to exit the loop.

Solution

23
Lesson 4- Moving Sprites
Challenge

Have a go at these challenges to improve your magic 8 ball game even more!

Suggestions
• Change the print code so that the user sees an alert box with the response and is asked
if they have another question or not.
• Handle the possible responses by the user (‘yes’, ‘y’, ‘no’,’n’ and other possibilties). If they
say ‘yes’ or ‘y’ then prompt them to ask another question.
• Go into design mode. Make the 8 ball look more circular by increaasing the number of
sides.
• Write code to make the 8 ball speak its answers using speech bubbles.

Possible Solution

24
Lesson 5- Functions

Stage 1- Customize the scene

In this activity you will be making a number guessing game. Customize the design view but keep
the same object types.

Tasks
• Customize the design view but keep the same object types - one sprite and two text
areas with blue backgrounds.
• Ensure your sprite is called ‘character’ and that the text areas are called ‘too_low’ and
‘too_high’ and that they contain the correct text.

Solution

25
Lesson 5- Functions
Stage 2- Create variables and import modules

Initialize the variables that will store information for our game.

Tasks
• Import the ‘random’ module and the ‘sleep’ module from the ‘time’ package.
• Create a variable called ‘guess’, set to 0 to start with
• Create a variable called ‘answer’ and initialize it to a random number between 1 and 100
using ‘[Link]’.
• Run your program. Check that you can see the value of the variable ‘answer’ in the
debugging panel at the bottom-right of the screen. It will change each time you run the
program.

Solution

Stage 3- Ask the user to guess

Interact with the user.

Tasks
• Add code so that your character says: ‘I am thinking of a number between 1 and 100. Try
and guess it’.
• Use the ‘sleep’ command to make the program wait 6 seconds so that the player has
time to read the instructions.
• Next, use an ‘input’ box that says ‘Guess:’ and set the value of the variable ‘guess’ to the
user response.
• Convert ‘guess’ to an integer using the ‘int’ function.
• Test your program by entering the guess 50. Check that the variable ‘guess’ has the
value 50 in the debug panel (bottom-right)

Solution

26
Lesson 5- Functions
Stage 4- Check if they got it right

We will give feedback to the user if their guess was too low or too high. First, let’s check if they
got it right.

Tasks
• Add an ‘if’ statement that makes the character say, ‘Well done!’ if the guess is correct (if
guess equals answer).
• Test that it is working. You can use the debugging panel to see what the correct answer
is!

Solution

Stage 5- Expand the ‚if‘ statement

If you guessed too low, we need to tell the user to try again.

Tasks
• Next, add an ‘elif’ statement to test if the guess is too low.
• If it is too low, your character should say ‘Too low!’
• Also, the background colour of the ‘too_low’ text object should be set to ‘red’
• Test your program by typing in a number less than the value of ‘answer’ (shown in the
debugging panel)

Solution

27
Lesson 5- Functions

Stage 6- Finish the ‚if‘ statement

If the guess wasn’t correct and it wasn’t too low, then it must be too high.
Let’s tell the user if their answer is too high.

Tasks
• Add an ‘else’ statement at the end of your program. Your program should now contain
one ‘if’ statement, one ‘elif’ statement and one ‘else’ statement.
• The code inside the ‘else’ statement will execute if the guess wasn’t correct and wasn’t
too low, so it must be too high. Inside the ‘else’ statement make the character say, ‘Too
high!’
• The background colour of the ‘Too high’ text object should change to red.
• Test your program by typing in a number more than the value of ‘answer’ (shown in the
debugging panel).

Solution

28
Lesson 5- Functions
Stage 7- More than one guess.

Let’s add a ‘while’ loop so the game continues if you get it wrong, so that you can carry on
guessing.

Tasks
• Add a while loop at the start of your program, after the ‘guess’ and ‘answer’ variables are
initialized and the character has said “I am thinking of a number”, and before the ‘input’
statement. The while loop should run while ‘guess’ is not equal to ‘answer’.
• Inside the while loop, set the background color of both text fields to blue. This will make
sure that they go back to blue before each guess.
• Indent the rest of your code so it is completely inside the while loop. This will enable
guesses to be made again and again.
• At the end of your code, after the ‘else’ statement, use a ‘sleep’ command to sleep the
code for 1 second, so that users can see the text fields go red before they go blue again.
• Test your program. Please get the answer too high once, then too low once and then get
it correct.

Solution

29
Lesson 5- Functions
Stage 8- Creating a function

Put the game code into a function so that you can play over again. Each time you call the function
we will execute all of your code.

Tasks
• Add a ‘Play’ button to the design view. Call it ‘play_button’.
• Define a function called ‘play_game’.
• Move all your code (except the imports) inside the function.
• Write some code to call the function when the button is clicked. Use ‘[Link].
when_click’ to do this.
• Test your program. Click “Play” and get the answer correct. Click Play again and check
that you get a brand new game. Get it correct again.

Solution

30
Lesson 5- Functions
Challenge

Have a go at these challenges to improve your quiz.

Suggestions
• Add some code to keep a record of how many guesses it took the player and let them
know at the end of the game. Try to use a function for this.
• Make the “Play” button hidden when it is not needed and make it visible when the player
has guessed correctly.

Possible Solution

31
Lesson 6 – Using sound in a game

Stage 1- Set the scene

In this game, the player will be asked to choose a door to go through. One will hide a scary
monster and the other two will be safe. First let’s set up the scene.

Tasks
• In design view, change the background if you want to.
• Set up a scene with three closed doors (choose what images you want to use) and three
text fields.
• Name the doors ‘door1’, ‘door2’ and ‘door3’.
• Name the labels ‘label1’, ‘label2’, ‘label3’.
• Make the text in the labels say ‘Door 1’, ‘Door 2’, ‘Door 3’ and position them near the
corresponding doors.

Solution

32
Lesson 6- Using sound in a game
Stage 2- Add a character
When the player chooses the wrong door, a scary character will attack them. Let’s add that next.

Tasks
• Add a scary character. Name it ‘character’
• Set it to hide at the beginning of the game. It should be large and positioned in the centre
of the screen.

Solution

Stage 3- Initialize the game.

Let’s initialize the variables we need.

Tasks
• import the ‘random’ module and the ‘sleep’ module from the ‘time’ package. These
should go at the top of your program.
• Create a variable called ‘playing’ and set it to True.
• Create variables called ‘score’, ‘character_door’, and ‘chosen_door’ and set these all to
zero.

Solution

33
Lesson 6- Using sound in a game
Stage Stage 4- Add a loop

We need the game to continue playing, asking the player which door they want to open.
A while loop is perfect for this.

Tasks
• Add a while loop that runs while ‘playing’ is True. In Python you can use ‘while playing:’
to achieve this.
• Inside the loop, use ‘[Link]’ to set ‘character_door’ to a random number
between 1 and 3 inclusive.
• Also inside the loop, ask the user to input which door they want to choose. Tell the user:
‘Choose a door 1, 2 or 3 (0 to exit)’. Set ‘chosen_door’ to the user’s response. Convert
this to an integer using the ‘int’ function.
• Use an ‘if’ statement to check if the user has entered 0. If so, set ‘playing’ to False. This
will end the game.
• Test your game a few times - check that you get different values of ‘character_door’ (in
the debug panel at the bottom right). When you’re ready, end the game by choosing
door 0.

Solution

34
Lesson 6- Using sound in a game
Stage 5- Handle the user choice 1

We need to make the character appear if the user chooses their door, and the game should end.

Tasks
• Use an ‘else’ statement to continue the game if the user doesn’t enter 0.
• First, play sound of a door opening and sleep for one second so that the user can hear
the whole sound.
• Next, write an ‘if’ statement to check if the user has selected the hidden character’s door.
If so, the character should be shown, a scary sound should play and ‘playing’ should be
set to False.
• Test your game by looking in the debugger panel and choosing the hidden character’s
door on purpose. Check that the character appears, the game ends and your score stays
as zero.

Solution

35
Lesson 6- Using sound in a game
Stage 6- Handle the user choice 2

If the user chose a safe door, we need to tell them and increase their score by 1.

Tasks
• Use a final ‘else’ statement, and inside show an alert using ‘[Link]’, which says “You’re
safe!”.
• Also increase the value of ‘score’ by 1.
• Test your game by looking in the debugger panel and choosing a ‘safe’ door (one
different to character_door). When you’ve played a few times and you’re ready, enter 0
to quit. Your score should be least 1 this time.

Solution

36
Lesson 6- Using sound in a game
Stage 7- Add visual effects

Let’s make the doors open and close by changing their images using the ‘set_image’ method.

Tasks
• Before playing the sound of the door opening, change the image of the door the user
chose so it looks open. For example, if they chose door 1, use ‘door1.set_image(...)’, and
so on for door2 and door3.
• Test your program by clicking on door1. Check that the image of door1 changes to an
open door. When you’re ready, enter 0 to end the game.

Solution

37
Lesson 6- Using sound in a game

38
Lesson 6- Using sound in a game
Stage 8- Debug

To finish off our game, it would be nice to add a ‘Game over’ alert and tell the user their score.
Someone has written a game similar to yours, but it has two bugs.
First, the game never ends - it keeps on playing even after the tiger attacks you.
Second, the “Game over” message doesn’t work.

Tasks
• Fix the code so that it doesn’t keep asking you to choose a door after you have been
attacked by the tiger.
• Fix the error in the “Game over” alert and play the game again. When you’re ready,
choose the hidden character’s door on purpose and check that the alert appears telling
you your score.

Solution

39
Lesson 6- Using sound in a game
Challenge

Here are some ideas for how to improve your game.

Suggestions
• Make sure that all the doors are closed before the next time it asks you to choose a door.
Currently they stay open.
• Add some success sounds when the user chooses a ‘safe’ door.
• Add an on-screen display so the player can see their score at all times. Make it update
when the score changes.
• Position the character closer to the door they are supposed to be hiding behind to make
it look more realistic.

Possible Solution

40
Lesson 6- Using sound in a game

41

You might also like