You are on page 1of 6

Coder Dojo Hangman

This game is a simple version of Hangman for Scratch. You have to guess the letters in the word. If you get a letter wrong, a body part is added to the hangman. This game will introduce you to lists. Lists are collections of variables, in other programming languages these are called arrays. There are three separate things we want our game to do. 1) Choose the word we have to guess 2) Gather our guesses and see if they are in the word 3) create the hangman graphic For step 1 we will start by creating a list for our words we have to guess. We will call this dictionary and we can add a few words to allow us to test the programme. The words we are going to add are coder, dojo and scratch. We can add more words when the programme is finished to make it harder. We now need to create a variables. Create a variable to record the word you have to guess and call this hiddenWord. The following script works out how many words are in the dictionary list and picks a random number from the list. We can put this script in the stage.

Next we need to create a variable to store the blank word, we will fill this with an underscore symbol for each letter in the hiddenWord. We can call this variable blankWord. First we blank the variable and then we join the _ symbol to the blank word for every letter in the hidden word.

Then we need to create a list to store all the letters we have guessed that aren't in the word. We can call this lettersGuessed. Then we need to initialise this list. This is done by deleting any letters that are in the list from a previous game. After this is done we need to message to the other parts of the programme to get it to go to the next stage. We do this by broadcasting a message, which we call displayWord here. Our final script for our stage is now this.

For our next step we can create a new sprite called typewriter. This sprite will write our letters across the screen and ask us to guess the letters. We need to create a different costume for each letter of the alphabet and another one for the underscore symbol _. This is needed to show the amount of letters in our word. The costumes are created using the text function in the costume painter. It is important that each costume has the letter in the exact same spot so the word looks even and neat. This can be achieved by copying the costume and just changing the letter.

Now to create the scripts for this typewriter. This script will act on the broadcast of displayWord we

sent from the stage. When we receive displayWord we want the typewriter to write out the blank word and the list of letters we have already guessed. We need to create a new variable tempcounter which we use to count through the letters. This is called iteration.

Next we need to do the same for the lettersGuessed. The script now looks like this.

We can now add the script to check if our blankWord is correct or if we have run out of lives by adding the following to the end of our script.

The next park of our script asks the player what letter they want to choose and then compares that to the hiddenWord. We use our tempCounter again here to iterate through the letters in the word, but we also need to create 2 new variables, one to store our blank word while we work on it called tempWord, and the other to record whether we have correctly guessed the word called letterGuessed.

The end of this script the calls the displayWord broadcast which starts the process of displaying the word again.

We also need to add a short script to hide our typewriter so we can't see it when it's not typing.

Our programme should work now, but we don't have the hangman character appearing so we need to create a new sprite called hangman and create a new costume for each of the 5 lives we want to have.

We then can add the following script to get the image to change when the lives are lost,

we add the one because when we start our lettersGuessed list is empty and we don't have a costume0.

We then add the following scripts to get the hangman sprite to tell us if we won or lost.

What ideas can you come up with to improve this programme. Perhaps you can add a separate sprite to allow players to add they're own words to the dictionary.