Posted on

Scratch Coding – Present Drop

Our last game this year is a Santa present dropping game. The code is very similar to the ball catching game, but instead of the balls dropping from random heights, we are going to have the houses and stockings move along the bottom of the screen. Santa must drop off presents.

We already have a number of assets ready to use:

We have to do a few tasks before our game will come to life:

  • Make it snow
  • Make it look like Santa is flying
  • Make Santa drop a present
  • Have the present fall to the ground
  • Check to see if the present hit the target and keep score

Make it Snow

Let’s start with the first task, we have a snowball sprite and we need to make it look like it is snowing.

Because we want a lot of snow on the screen at once, we are going to create a clone of the snow sprite. A clone is just a copy of a sprite. We want the size and positon of our sprite to be random near the top of the background.

Notice how we use the ‘Create clone of myself’ block? We can now use the ‘when I start as a clone’ hat block to make the cloned sprite move. We also make the size of the snowball random, this will make the scene look like it has depth.

We want our snow to fall to just above the bottom of the screen:

We hide the snow once it gets near the bottom of the screen and then finally we use the ‘delete this clone’ block to delete the clone from the screen.

Task: Test this out.

We should be able to see the snow falling.

Make Santa Fly

The next task we need to undertake is to make it look like Santa is flying. We want our houses to move along the bottom of the screen, moving from left to right. Since we have 10 house choices to move across the screen, we can’t start them all moving at once, otherwise there will be too much going on for the player.

We need a way to randomly pick a house and then tell it to move across the screen.

On the backdrop, let’s use a green flag hat block to always be picking a random number for the house to show on the screen.

First up, lets’ create a new variable that will store the random house number that we are going to pick.

This random house number variable will be set with a number between 1 and 10 (the same number of houses that we have).

We then send a message to the sprite that we want to have move. For example, House1 will receive a message if it has been randomly picked.

When the house does get its message, we want to have it move across the screen:

Task: Test this out, do the houses move across the screen?

We have a couple of problems with this. When the house gets send another message, it will restart its movement. This isn’t what we want to have happen, so we need to write a little bit of script to prevent this from happening.

The logic should look like this:

How do we tell if the sprite is moving?

We can check to see if the sprite is still in the starting position. If the sprite is at the same X position as when the game starts, then we know the sprite is not moving. Otherwise we just tell it to keep moving. We need to use an If Then / Else block for this.

The script on the house now looks like:

We need to make sure that we set it back in the starting position when the game finishes and when the game is restarted by clicking on the green flag block.

Test: Does our house move across the screen correctly?

We can now copy this to each house sprite. Be sure to change the message received block so that it matches the house number.

Making the Background Move

To help add to the movement effect, we can make the large background image move slowly across the screen. We want this background to be the last sprite on the backdrop, so we will also set the layer property to make sure it is at the back.

On the background sprite:

We also want Santa to be able to fly higher or lower by using the up and down arrow keys.

On the Santa sprite, we can add the following:

When the up key is pressed, we add more to the Santa sprites Y axis, this will make him move further up the screen.

For the down movement, we don’t want Santa to hit a house, so we will make sure that we only reduce the Y axis if he is higher than 20 pixels on the screen. Which will be just above the house sprites.

Task: Test it out – Does it look like Santa is flying?

Dropping the Presents

Now that Santa is flying high, we can make him drop a present when we press the space key.

On the present sprite:

Again, we use the clone blocks, this will let use have more than one present on the screen. When we start the present as a clone, we pick a random costume, which will pick a different colour for the present. We then move the present up to where Santa is on the screen. We then repeat moving the block downwards on the Y axis until we either get low on the screen (less than -120) or we touch the green drop target. At the end we delete the sprite clone.

Keeping Score

We just need a variable now to keep score.

This variable will be reset to zero when the green flag hat block is selected.

On the backdrop:

We also then need to increase the present count when the present hits the drop zone.

On the present sprite: