Word games

Quiz time with Scratch - Lesson 6

Objectives

  • Devise, test and refine effective control sequences incorporating use of lists, variables, conditional statements, broadcasts, inputs and outputs
  • Uses post-tested loops e.g. ‘until’, and a sequence of selection statements in programs, including an if, then and else statement
  • Uses a variable and relational operators within a loop to govern termination

Lesson Resources

Introduction

In this final lesson we are going to move away from number games to words, to a word guessing game similar to the classic game of Hangman. If you happen to be unfamiliar with the game, here’s how to play:

  1. Player one thinks of a word and marks out the number of letters the word contains with dashes on paper. 
  2. Player two must guess letters in the word, one at a time. 
  3. If the letter they guess is in the word, player one must write it in, showing where in the word it occurs. If player two guesses a letter that is not in the word, player one adds a part to a drawing. In the original version it was a ‘hangman’, however, we feel that times have changed and perhaps other drawings are now more appropriate. 
  4. If player two guesses the word before the drawing is complete they win, if they make enough mistakes that the picture is completed, player one wins.

 

There is further explanation of the game and variations on it HERE. You can use any drawing that can be split into 8-10 parts and the drawing can ether be added to or taken away as incorrect answers are given. More on that later. 

 

Planning the game

To begin the lesson, play the normal game with the class, or get them to play with a partner so they get familiar with the workings of the game. 

Ask them to discuss these questions with a partner: 

  • If we are going to code this game what things do we need to consider? 
  • What definitely needs to be a feature of the game?
  • What could we leave out or simplify?
  • Can you foresee any tricky bits?
  • What features from the projects we’ve already made over the last few weeks can we use again? 

Then discuss as a group and ask what needs to happen first?

Initially limiting the length of the word will make it much easier to code and test, as will having a consistent length of word. So in this example we’re going to focus on five letter words. (There are hundreds to choose from, but here’s a list if you need it). 

 

Create your word list

Start a new Scratch project and go the variable section. Create a new list and to begin with, add four five-letter words to the list. 

 

 

Each time we play the game we want the computer to choose a word from this list at random, this will be the word we guess. Ask the students to make a variable called ‘chosen word’ and to code their game to set it to a word from the list at random when the game starts. The number range in the  ‘pick random’ block will need to match the length of the list you create. At the moment, we only have only four words (we’ll make it longer later).

 

 

Next, we need to identify each letter of the word for the player that is guessing. Ask the students to make five new variables, one for each letter in the words, call them simply ‘letter 1’, ‘letter 2’ etc.

 

 

On the stage, switch the view of each variable box to the large readout option (right-click the variable, or double click it):

 

 

Arrange them in a row, being really careful to keep them in order!

 

 

Set the variable of ‘letter 1’ to a dash. 

 

 

 

 Add this to your stack, do the same for the other four letter variables as well. 

 

 

When the students click the flag now, it should start the game with a word being chosen from the list (which is visible at the moment) and an empty box for each letter. Now they need to introduce the game to the player with a bit of speech, ask them to add some ‘say’ blocks to tell their player what to do, something like;

 

 

And then create a broadcast called ‘ask letter’ that will start the question and answer stack of code. 

 

 

This is going to begin (after the receiver block) with an if/else statement, and the question is going to go in the ‘else’ part (we’ll come back to the ‘if’ later). Invite the player to guess a letter and type it in the box.

 

Correct answers

Now we need to determine if the letter they submit is in the ‘chosen wordvariable (which was generated from our list). This needs to be checked one letter at a time, i.e. is it the first letter? Is it the second letter? etc. Can you build an ‘if’ statement that checks if their answer is equal to the first letter of the chosen word? 

This code does that:

 

 

It also tells the player they are correct and sets our on-screen variable box for letter 1 to the correct answer, so that letter is now filled in in the puzzle. 

 

 

It also uses a new variable (‘Correct letters’) to keep track of how many letters have been guessed correctly.  1 is added to this variable if the answer is correct. 

 

 

This script then needs to be duplicated (right click it from the top) and adjusted for the other four letters. What will we need to change in the code to make it work for the other letters?

 

 

This will then check the answer each time against each of the five letters in the word. It will also fill them in in the answer boxes and tell the player if they are correct with any of them. It will also work if there is a double letter in a word. 

It then invites them to pick another letter when the  broadcast  ‘ask letter’ is triggered at the end of this stack, so our script loops back to the start so the player can try the next letter. 

This whole new stack can now go under the question block in the ‘else’.

Get the students to test this now. As the ‘Chosen word’ variable is still on screen they can enter the correct letters in any order. 

Before we move onto wrong answers, there’s a bit more to do with our new variable ‘correct letters’. This will act as a score for the game (one point for each correct letter guessed).

We’ll add set ‘correct letters’ to 0 to the start of the game code so we always begin the game on 0.

 

 

How many points do we need to win? How can we code the game to tell us when we’ve won?  Challenge the students to add this element to the top of the second stack of code we created (above the if/else block):

 

 

This new code checks for when the ‘correct letters’ variable is equal to 5 and then says well done and confirms the correct answer by reporting back the variable to the player. The game is then stopped with ‘stop all’.

 

Wrong answers

The last segment of creating the game is to code what happens when answers given are incorrect. There are a few elements to consider here but we’ll start with the code for the game recognising a wrong answer.

Ask the class - How could we use this collection of blocks? 

 

 

While the order of the blocks doesn’t really fit with the way we would say this in a normal sentence (as is often the case with coding), adding the ‘not’ block means ‘if the answer given is not the first letter of the chosen word....’ 

 

 

This will go some way to checking if the answer is wrong. However, it’s not quite as simple as duplicating this for each of the five letters, as this will test if our answer is wrong for the first letter, or the second letter, or the third, fourth and fifth letters on their own. This wont work.

 

 

For example, if the word we are guessing is ‘mouse’ and we guess an ‘o’ then it is not correct for letter 1 so we would get a ‘sorry it’s not there’ message with this code, however, it is correct for letter 2, so that message would not make sense to the player. 

 

To solve this we need to nest the ‘if’ blocks inside each other like this:

 

 

This will check if letter 1 is wrong, then only if it is, it will check if letter 2 is also wrong, if it is it will check letter 3 and so on. Therefore, you will only get the ‘Sorry, it’s not there’ message if the answer does not match any of the five letters in the word. 

Two broadcasts also need to be added to the final ‘if’ block; the ‘ask letter’ command that will then trigger the player to be asked for another letter, plus a new broadcast called ‘wrong’. This whole stack of blocks can then be attached to the rest of the question/answer stack, being careful to move the final ‘ask letter’ broadcast to underneath these new blocks:

 

 

Drawing the countdown 

The broadcast of ‘wrong’ is going to trigger each part of the drawing you use to appear or disappear. There are lots of possibilities you could use for this such as food being eaten (draw it on a plate and take ‘bites’ by rubbing parts away with each wrong guess) or a candle or fuse burning down, and many more. Encourage the children to be creative and come up with their own ideas. Whatever they choose, the principle of creating it is the same. 

Here we'll demonstrate an example of a candle burning down, each incorrect answer burns some of the candle away, when the candle goes out, the player has lost the game. 

First create a new sprite with the paintbrush option:

 

 

If you are using a shrinking object, draw the full picture for the first costume. We made use of the oval and rectangle tools to speed this up and keep it neat. 

 

 

Once you’ve drawn the sprite you can adjust its size in the sprite settings by changing the size number. 

 

 

Then right-click the costume’s thumbnail on the left of the screen and duplicate it.

 

 

In the duplicated costume 2, use the rubber to rub away part of the sprite. For the candle we need to rub away a stripe from the middle so the flame is still visible.  

 

 

You can then use the select tool (arrow) to drag a box around the flame, wick and top part of the candle and move them down to create a shorter candle. 

 

 

Repeat this process by duplicating costume 2, rubbing out another small section and moving the top part down again. Repeat until the candle is at the bottom, and in the final costume we have rubbed out the flame as the candle has burnt away.  

 

 

We’ve done this over 9 costumes.

 

 

It’s up to you how many steps you add, but remind the students that the number of steps in the drawing is the number of attempts the player gets to guess the word.  

Next we need to add some simple code to this sprite. When the game starts (with the green flag) we want this sprite to be on costume 1. We also need to create another variable called ‘wrong answers’ and set this to 0 at the beginning of the game.

 

 

When the broadcast of ‘wrong’ is received (each time the player gives a wrong answer) it adds one to this variable and also moves the sprite’s costume onto the next part of the drawing (make sure the costumes are in the right order!).

The game is almost complete, but we now need to work out how many wrong answers the player is allowed, in this example it’s eight as there are eight parts to the candle drawing (not including the full candle in the  first costume), you could extend this if you wanted to give them more chances.

Earlier we left the ‘if’ section from the first ‘if/else’ block empty, let’s return to that now. What do we want to happen if the wrong answer equals 8? At this point we need a ‘game over’ style message, and perhaps give them the answer. 

This code does that and also then stops the game. So now, each time the ‘Ask letter’ broadcast is received, the game checks to see how many wrong answers have been given, if it’s not eight, it asks them to try another letter, if it is eight, the game finishes!

 

 

The final step is to go back to your variables category and hide some of the elements by unticking them. The only visible elements should be the five letters, the rest should be unticked and hidden.

 

 

It’s recommended that you add some more five letter words to your ‘words’ list before unticking that as well. There are forty words in this completed example project.

 

 

Plenary

As with all the projects, ask the students to play each others’ games to test them, provide feedback and make suggestions for any bugs they find. 

Finish by discussing some of the following to evaluate their understanding of the concepts covered in this unit: 

  • How could we further develop this game? 
  • How could you add the incorrect letters the player guesses to the screen as they go?
  • Which was your favourite of the projects you’ve created in this unit? Why? 
  • Can you now explain what a variable and list are? 
  • What is the difference between an ‘if’ and ‘if/else’ block? 

 

< Previous Lesson