Posted on

Scratch Coding – Fall and Catch

Today we are going to make a game where we must catch something falling from the sky.

My game has a basketball falling and a bowl to catch the ball. We are going to first use the keys to control the position of the bowl. The balls are going to drop from a random position and fall down. We want to keep a count of how many times we catch the ball.

If we break down our game into parts, we find that we can look at a few small tasks which will make up the whole game:

  • Move the bowl backwards and forwards
  • Make the ball fall
  • Keep count of how many balls are caught
  • Reset the game and make sure everything is in the correct position

Task 1: Move the bowl backwards and forwards

Rather than using a green flag hat block to start our game, we are going to use the Key Press hat blocks to run some script when we press the left or right arrow keys.

On our Bowl Sprite:

Now we only want our bowl to move left or right, not up and down. Moving left and right is on the X axis:

Use the ‘change x by’ block to make it move. Since moving left on the X axis is moving closer to zero, we need to make this a negative number.

Test it: Test to see if you bowl moves correctly.

Task 2: Make the Ball Fall

Before we can make the ball fall down; we need to make sure it is in the correct position for it to begin falling.

Let’s use a Green Flag Hat Block to help us find a nice random position.

On our ball sprite:

I’ve used the random-position block (this is the go to ‘mouse position’, but with the option changed).

Test: What happens, when we press the green flag button a few times?

We find that the ball will move to any height, this is going to make it hard for us to catch the ball. We need to make sure that the ball stays at a nice height so that we have a chance of catching it.

Let’s make sure the ball stays at a nice height:

Now we set the Y position to 180, so that no matter where the random position is, we set the height to 180 regardless.

Test: Does the ball position look good?

Now it’s time to make the ball fall to the ground. We always want the ball to be moving down, so we are going to use a ‘forever’ block.

We also want to move down, which is on the Y axis, but using a negative number:

Test: Does it work?

We now find that the ball sticks to the bottom of the screen. We need to be able to test to see if we hit the bottom and restart the ball falling again.

To test something out, we use the ‘If’ Block. For our ball we want to test to see what the y position is. We know that -180 is the very bottom of the screen, so we should be safe testing this at -170.

Test: Does our ball reset now?

Task 3: Keep count of how many balls we have caught

Before we can count how many balls we have caught, we need to be able to test to see if the bowl has touched any balls.

On our ball sprite, we need to always be testing to see if we touch the bowl. If we always want to be testing something, we use a forever block and an if block together:

I’ve added a nice pop sound. You should add a sound to your project and play it when the ball is caught.

Sounds can be found on the sound tab. A new sound can be found by pressing the ‘Choose a sound’  button.

Test: Does the ball make a sound each time it is caught?

Now that we have our ball and bowl testing to see if they touch, we can easily add a score to our game.

Let’s add a new variable called ‘Score’.

Since the score is used over the whole game, we can set it ‘For all sprites’.

We need to change the score when the ball touches the bowl. We already have some code to play a sound when this happens, this will also be a good spot to change our score.

We add the score to the very end of the If block.

Test: What happens to the score if we restart our game a few times?

Task 4: Reset the game and make sure everything is in position

We should change the ball sprite, so that the score is set to zero when the green flag button is pressed.

Now that our game is working properly, we should also make sure that the bowl is put in the correct position also.

On the bowl sprite:

We added a green flag hat block and then set the Y position to -125 (your number might be different).

Extras

Loose points when the ball hits the bottom of the screen:

We can remove points from the player when the ball hits the bottom of the screen. Remember that we reset the ball position if it got to -170 on the Y axis?

This is also a good spot to change the score by -1:

Adding extra balls to the game:

Catching one ball at a time is cool, but can we make it a little harder?

Use the duplicate option to create more game balls.

Change the colours of the sprite when the player gets a certain score:

We should test the score. To do this, let’s add to the existing forever block on the ball sprite:

Now we want to make sure that the ball changes colour: