Posted on

Scratch Coding – Rock Paper Scissors

The goal of this game is to beat the computer in a game of rock paper scissors.

The game should start by displaying the options to the user:

The user should then click on the sprite that represents the option they wish to take.

The options should then hide and the hands should move up and down and revel the winner or loser.

We will need two variables that will store the selected position:

The first task of each clickable option sprite is to reset its visibility when the game is started:

Next is to set the player variable to the matching value of the clickable sprite:

Notice that we are using the ‘when this sprite clicked’ block. Each different sprite will set the Player variable to the value that matches the option chosen (‘P’, ‘R’, ‘S’, for ‘Paper’, ‘Scissor’, ‘Rock’).

We broadcast a message to all sprites when the game starts.

On our backdrop we can first setup the correct starting backdrop, which is the blank template.

When the backdrop receives the ‘StartGame’ message, we can then pick a random number that will represent the computer’s selected position.

The code will pick a random number between 1 and 3 (the number of options that can be selected). It will assign to the computer variable a character that we can later use to compare against the player variable to determine a winner.

We will broadcast a message to the player arm and send a message to the computer arm with:

  • ‘Person Show’
  • ‘Computer Show’

We wait 3 seconds for the arm moving animations to complete.

Then we compare the two variables and set the backdrop to who the winner is.

The first comparison we look at is to see if the player and the computer have the same value, if they do, then we know the game is a tie.

Work through each of the comparisons and try testing your game to see if you have captured them all.

Finally wait 5 seconds and then send a ‘reset’ message that can be used to setup the sprites again for another game.

The completed code:

The stage should have the following code:

The computer arm sprite should have the following code:

It listens for the ‘ComputerShow’ message, which it will use to animate the arm moving and then will switch to a sprite that has the correct animation.

The person arm sprite should have the following code:

The animation is the same, however we change the animation based on the player variable.