How to make a rock, paper, scissors game in Scratch

How to make a rock, paper, scissors game in Scratch

How to make a rock, paper, scissors game in Scratch

Content Protection by DMCA.com

Scratch can help you create a Rock, Paper, Scissors game right on your computer. Then you can share this game with your friends and help them understand Scratch better.

Scratch programming is a great way for kids and teens to start learning programming and building fun games, stories, and animations they love. Creating games on Scratch is very simple. Below is an example. The article will hDetailed instructions on how to make a Rock, Paper, Scissors game in Scratch.

1. Set up the character and background

Start by choosing the two character images (sprites) you want. Think about the theme you want for the game. One character will represent the player and the other character will represent the computer. After selecting two images, add a background to the game.

Set up the background

2. Create variables

Next, think about important information that needs to be kept track of while playing Rock, Paper, Scissors. This information is stored in variables. For example, we need to track the choices and scores of both the player and the computer. This means we need to create a total of 4 variables! We can do this by going to section ‘Variables’ and click the button ‘Make a Variable’. When doing this, make sure it is selected ‘For all sprites’ is selected. Here is an example of one of the variables named ‘Player’s choice’.

After creating all four variables, your screen will look like this:

Read More  Instructions on how to use Antutu Benchmark

Create your variables

3. Reset the variables to 0 after each round

Variables are reset to 0 after each game. Add this code to the player sprite:

At the beginning of the game Rock Paper Scissors, the player and the computer have not made any choices yet, so everyone’s score is 0.

Reset variable to 0

4. Let the player choose Rock Paper Scissors

The sprites and variables are now ready, so you can start programming the player. Take a look at the table below, it shows the keyboard key and ‘Player Choice’ variable updated for Rock Paper Scissors. If the player wants to choose Rock, they need to press the ‘R’ key on the keyboard, and we will set the ‘Player Choice’ variable to 1. If they want to choose Paper, they need to press the ‘P’ key, and the ‘Player Choice’ variable will be set to 2. Finally, if they want to choose Scissors, they need to press the ‘S’ key, and the ‘Player Choice’ variable will be set to 3.

Rules for playing the game Rock, Paper, Scissors

Make sure you understand what the table means before continuing.

Now, let’s see how to translate this into code!

How to check if the player pressed any key on the keyboard?

To do this, use 3 blocks if-then from the control category and the spacebar press blocks from the sensors category. Every block if-then will sense whether the player presses a different key or not. Once you have prepared all three blocks if-then don’t forget to add a forever block around the whole thing. This ensures the game always checks to see if any keys are pressed. Inside each block if-then it is necessary to set Player’s Choice to the correct value. The code will look something like this:

Program how the player selects keys

You can make this more interesting by having the sprite say something every time the player makes a choice. Your code might look something like this:

Read More  Location of NPCs in Sailor Piece

Characters chat

5. Programming AI machines must choose Rock, Paper, Scissors

Now, when the game allows the player to make choices, you have to let the AI ​​or machine know that it can also make its own choices.

To make sure the computer knows it’s its turn to choose, you need to design a notification. You will find the required block in the events category. Select and drag a broadcast message block (broadcast message1) into each if-then condition block. Be sure to create a new message and name it ‘player made choice’. Your code will now look like this.

Scratch programming the game Rock, Paper, Scissors

Next, the machine must be able to receive that message, so we need a block from the event category again. This time, you need the ‘when I receive’ block. This block will activate the machine’s code once the player makes their choice. The machine’s code will look like this:

Two characters talk

6. Tell the computer to choose Rock, Paper, Scissors

Now that the computer knows it’s your turn to choose, you need to add code so it actually chooses rock, paper, scissors. Since the computer can’t think for itself, you have to let it choose randomly. To do that, you need the ‘randomly’ command block from the Operators category.

Remember the table that says Rock, Paper, Scissors is represented by the number 1, Rock, Paper, Scissors by the number 2, and Scissors by the number 3? We need to set the variable ‘Computer’s Choice’ into a random number that we generate so we can track the computer’s choice. Your code will look like this:

Scratch Rock Paper Scissors Game

Now that the computer has made its choice, we can make the game more interesting by letting the computer speak its choice. To do this, we’ll check what Computer’s Choice’ is. If it is 1, tell the computer to say ‘Rock, Paper, Scissors!’. If it’s 2, the computer will say ‘Paper!’. Finally, if it’s 3, the computer will say ‘Pull!’. How to check if one thing is equal to another? We will use blocks ‘=’ from the category Operators with blocks if-then !

The calculator’s code will now look like this:

Read More  How to best optimize settings to play Arknights: Endfield on PC

Code for computers

7. Scoring

Games always need to have winners and losers. This is why the variables ‘Player’s Choice’ and ‘Computer’s Choice’ are so important! They store both the player’s and the computer’s choices to know who won.

Let’s list all possible outcomes:

Results spreadsheet

Let’s start translating this into code.

After the computer sprite announces its random selection, use conditional statements to determine the result. This example creates three if-then conditional statements, one for each possible player choice. For example, if the computer chooses 1 (Stone), then we check whether the player’s choice equals 1 (Stone), 2 (Paper) or 3 (Scissors). Depending on the player’s choice, the message will be Computer Win, Player Win or Draw. We can also increase the Player’s score or the Computer’s score depending on who wins the score.

We’ll continue this process by adding three if-then statements to the conditional statements that test whether Calculator’s choice is equal to 2 (Paper) or 3 (Scissors).

Conditional commands in Scratch

8. Win or lose the game

Finally, we can end the game if the Player’s Score or the Computer’s Score reaches a certain target point. The example will use a target score of 5 points, but you can customize this number depending on how long you want the game to continue.

First, create two backgrounds that you will use, one if the player wins and another if the computer wins.

After your code calculates the score by determining who wins the point, you tell the computer to check whether the player or computer has reached the target score. If the Player Score or Computer Score variable is 5 (your target score), then toggle the background and stop the code.

Convert background font in Scratch

Don’t forget to switch the background back to a textless background when you click the green flag.

Feel free to continue adding more features and capabilities to this game, such as background music, creating animations, even creating player levels. Be creative and most importantly have fun!

GameHov is group of expert in gaming industry that cover all gaming news from e-sport to casual video entertainment.

Post Comment