- How to animate a sprite
- How to make a ball bounce in Scratch
- What a variable is and how to use variables in Scratch
- How to make our sprite move based on keyboard input
- I will be able to make a sprite move using keyboard input
- I will be able to implement variables into my game
- I will be able to explain the difference between an animation and a game
In this lesson we are going to make a game where we bounce a ball into a certain part of the screen.
Finished product
You can see a preview of the finished game below:
Getting started
Follow these steps to get started with your game:
- Add a new ball sprite into your game
- Rename the ball from Ball to BouncingBall and the cat to Cat
Setting up the ball
The first step is to add your ball to your scene. One you have done that, make sure to select the ball.
We need our Ball to move around the screen whilst the game is running. To do this we need the following blocks to our Ball:
The table below explains what each of these blocks does.
Block | Purpose |
---|---|
Runs when the green flag is clicked | |
Keeps doing something forever, as long as the red stop button is not clicked | |
Makes the Ball move 10 steps in the direction it is facing | |
If the Ball is touching the edge of the scene, it will bounce back the way |
Making the ball bounce when it touches the cat
The next stage of this is to make our ball bounce when it touches our cat. To do this, we need to add more code to our Ball. We need to add the code shown below:
Once again, the following table explains what each block does.
Block | Purpose |
---|---|
Keeps doing something forever, as long as the red stop button is not clicked | |
Checks to see if the Ball is touching the Cat sprite. If it is, the blocks inside this block are activated and run. | |
Plays the Boing sound | |
Turns the Ball 135 degrees | |
Moves the Ball 30 steps away from where it is, stopping it from getting stuck |
Adding keyboard controls to our cat
The next part of the game is to make our cat move. To do this we need to add keyboard controls to it.
Make sure you are selected on the Cat sprite.
Find the when space key pressed Try block under the Events tab. Change the drop down from "space" to "up arrow" and then create one for each of the arrow keys so that it now looks like:
Next we are going to make our cat move when the user presses a key. Find the point in direction 90 and move 10 steps blocks under the Motion tab. Add these blocks so that they attach underneath the arrow keys as shown below:
Stopping our cat going upside down
Did you notice that our cat went upside down when he was moving?
To fix this we need to change our cat so he can't go upside down by clicking Direction at the bottom next to where we name our sprite and selecting the option highlighted red below:
Developing our score system
Our score system requires us to save the number of points the player has. To do this we must use something called a variable.
A variable is like a container that can store data whilst a computer program is running. Variables are given meaningful names such as points or lives and are also given a value.
In Scratch, we create a variable using Variables tab on the left side. To create the variable select Make a variable and then give it a name, in this case we are going to use points as the name.
This variable will store our points as the game runs.
Variables can be set to a value or changed by a value. Now that you have created a variable you should see it at the top left of the scene.
Getting points
For this to become a game we need to add points when the player manages to get the ball into the right place.
For this to happen we are going to add an area on the screen where ball must go for the player to gain a point.
We are going to need to change our backdrop so that it has a red zone on it. Click on your backdrop at the bottom right-hand side so you get a screen that looks like:
Find the Fill tool on the screen and make sure it is set to red. Make sure to set Outline to nothing by clicking the button at the bottom on the Outline box as shown:
Now select the Rectangle tool which has been highlighted red below:
We are now going to draw a red rectangle on our backdrop so that it looks like the following picture:
When you have done that, click on the Ball sprite and make sure to click on Code at the top left:
Increasing points when the player scores
The next thing we need to do is make it possible for the player to gain points when they touch the red zone.
Add the following blocks (you will find the second one under the Variables tab):
Now change the blocks so that it looks like:
Click on the purple zone in the touching color block you have just created so you get a colour selection box like below. Click on the colour picker:
Select the red bar we have just created in the backdrop.
Now to increase the number of points the player gets each time the ball enters the red zone we use the change points by 1 block.
Add it to your blocks so that they look like:
Stopping the ball getting stuck in the red zone
When the ball enters the red zone, you may notice that you get more than one point. To fix this, we need to change our code to:
What this will do is move the ball into the centre on the screen when it enters the red zone.
Ending the game
The game should have a way of ending it. In this game, we are going to make it end when the player gets 10 points.
Add the following blocks to your Cat or Ball β it doesnβt matter which:
Now find the = 50 in the Operators tab and drag it into top part of the if block. Change the 50 to 10.
Find the points block in the Variables tab and drag it into the white circle in the previous block so that it looks like points = 10 block.
Finally, add the stop all block to the if block. Your final code should look like:
-
1
Upload your screenshot below:
-
2
Which word defines the difference between a game and an animation?
-
3
If the colour picker shown below was a slightly different shade of red, what would happen?
-
4
What might happen if the goals for both players are both the same colour?
-
5
What might happen if the players are the same colours as the goal colours?
-
6
How well do think you completed this task?
-
7
What parts of this task, if any, did you struggle with?
If you finish the task, try these.
- Add a purple zone where you lose a point if you enter it. Your code should be the same as before except you will need to change the colour and the points will now need to be -1 instead of 1. change points by -1
- Add in a second player who is controlled with the WASD keys.