The aim is to develop a Scratch game in which objects fall from the night sky and a single figure has to dodge them by moving sideways along the ground base line. The score is the number of objects avoided. This app has a lot of code that might be off-putting for a beginner but much of it is duplicated so there is actually not a lot of coding involved.
This time we will use the default Scratch cat icon, called Sprite1, as our figure and we will find a suitable falling object from the Scratch sprite library. We will end up with three blue dogs falling from the sky as illustrated by the video clip below.

Creating the Sprites

This time we will use the built-in Scratch sprites by clicking the New sprite, library link (the first one on the left) to open the sprite library then choose something you would like to rain down on the Scratch cat. In this example we will use a dog sprite. So we double click that to open a sprite, probably called Dog, in the sprite pane, alongside Sprite1. We are going to have three of these dogs so it is a good idea to rename this one as Dog1 - as usual, by right clicking then choosing info, editing the name and exiting with the blue left arrow.

So now we have two sprites, Sprite1, the cat sprite, and Dog1, a dog sprite, and, of course, the stage sprite. We are not going to put any code in the stage in this app but we do want to change the background color to black. So click on the Stage icon to select it then in the costume pane on the right (called a backdrop for the stage ) select the paint pot tool and a black color then drag the tool onto the backdrop and click to fill it with black color. So now we have a black background.

 In this app we are going to code only the cat and the dog. We start with the cat so click on Sprite1 to select that. Its costume pane shows two costumes - the default cat costume and a second one below it. We can switch from one to the other to give the impression of a walking cat. However, they are both rather large so we select each costume in turn and click on the cat figure to select that. It now appears with handles on all corners and you can drag one of the corner handles to resize the costume. The stage should now look like Figure 1.
The Scratch stage
Figure 1. The stage script.

The Cat Sprite

Now we can program the cat sprite. In the Sprite1 Scripts pane we can start the app with a green flag code block. Then we send the cat to x=0, y=-150 which is in the middle of the base line of the screen. Then we point it in the direction 90 degrees, which is to the right, just to have a starting direction and we use the “show” block to ensure it is showing. There are two other blocks we need in order to initialize our app. We need to go to the Data palette and create a score variable then in the Sprite1 script, initialize it to 0. You should also tick the box next to the variable so that the score value is always displayed on the screen.

And we need one other block before we start the app. This is to “set rotation style left-right”. To explain – all sprites are created by default pointing to the right, including the cat and the dog sprites that we are using. If we then use a Motion block to point it in another direction it will rotate to do so. We want the sprites to remain in the horizontal direction so we have to set the rotation style to “left-right” or “don’t rotate”. If we forget to do this we will soon be reminded when the sprites point in strange directions.

So the script now looks like that shown in Figure 2..

The cat script
Figure 2. The initial cat script.

The Game Action

The action takes place by first broadcasting a message to launch1 which will be received by Dog1 and start its movement. Then the cat goes into a forever loop in which it waits 0.1 seconds, just to get a reasonable speed (which you might need to tune a little). Then it tests to see if it is touching Dog1 and if so the cat is hidden and the game ends.

In fact, with this code, the cat is not actually moved. It is stationary until an arrow key is pressed. We want the cat to move to the right only if and when the right arrow key is pressed and to the left if and when the left arrow key is pressed. So we have to add a couple of scripts to the cat sprite. These are executed when the arrow keys are pressed. One turns the cat to the right, at 90 degrees and the other turns it to the left, at -90 degrees. If we had not changed the rotation style this would leave it upside down. As it is, it simply points it and faces it to the left. Then each script moves the cat 10 steps in the direction it is pointing then changes its costume.

So the cat faces left or right and moves in that direction as long as the left or right arrow key is pressed and switches costume every 10 steps giving the appearance that it is walking. As a final touch, we also allow it to move only if it is not at the edge of the screen, where it could hide from the falling dog, so the scripts are as shown in Figure 3.

The cat movement scripts
Figure 3. The cat movement scripts/div>

The Dog Sprite

Now we can program Dog1 to respond to the launch1 message. So it starts with a “when I receive launch1” message then it should move to the top of the screen, point downwards then keep moving down until it hits the bottom of the screen This last action requires a repeat until loop that ends when the y position of the dog is less than -180, say. The loop may of course be interrupted by the cat script when the two sprites touch each other. Otherwise, when the dog reaches the bottom of the screen the score is incremented, The dog sound is played from the Sounds palette, which is a barking noise, then the script broadcasts the launch1 message to itself which restarts the action with another version o  the dog sprite. The sound effect was recorded with the dog sprite and can be seen in the Sounds tag of its Script and Costumes pane.

The final script is shown in Figure 3. It is quite a short script but there are three additional features that complicate matters slightly. First we have to us the “set rotation style left-right” block again or the dog will come down nose first. Then we would like to start the dog at some random position so that the player has something to think about. So our “go to” block starts the dog at a random value of x between -180 and -60, which is somewhere in the left third of the screen. We will eventually create two other dogs which will start in the other two thirds so the whole screen is covered.

Then, to add further difficulty for the user we would like to create random dog speeds. Then they would not all arrive at the bottom at the same time and might finish and restart at different times. For this we need a speed variable, speed1, created as usual from the Data palette and seen by all the sprites. We start by setting the value of speed1 to a random number between 3 and 6, but again, you might like to fine tune this.

So the Dog1 script is as shown in Figure 4.

The Dog1 script
Figure 4. The Dog1 script.

If we now run the program it will show a single dog falling down the screen. You will be able to use the arrow keys to move the cat left and right. If the dog hits the cat the game should end. If the dog misses the cat another dog will be started from a slightly different position at the top of the screen.

More Dogs

So far so good. We have a working app that lets you play cats and dogs. But only one dog is rather boring so we will add two more. We will do this the easy way which is to right click on the Dog1 icon then choose the duplicate option which will create another icon called Dog2. This will have the exact same code and costumes as Dog1. If we repeat the process, we will get a third dog, again with the same code and costume.

All we have to do to finish our game is to edit the scripts for Sprite1 and the dog sprites to deal wit the additional dogs. We will do the dogs first. Each sprite has to be edited at 5 places. The launch1 message names have to be changed in two places to launch2 and launch3 respectively. Two new variables have to be created, called speed2 and speed3 and these have to replace speed 1 in the two locations where it is used. And finally, the random number starting positions have to be changed from the -180 to -60 of Dog1. The new values are -60 to 60 for Dog2 and 60 to 180 for Dog3.

Also, we have to edit the Sprite1 script to accommodate the extra dogs. The simplest way to do this (although not the most efficient) is as illustrated in Figure 5.  Here we have just added two more message broadcasts of launch2 and launch3 to start the other two dogs and we have added test routines inside the game loop to test for the cat touching any of the three dogs. We did this by right clicking on the “if” block then selecting duplicate to produce a copy of the conditional code without having to write it all over again.

The final cat script
Figure 5. The final cat script

What Next?

That’s it. When you start the app it runs as shown in the video clip above. It is a long app and quite complicated in places but worth the effort in terms of the final result. However, there is still room for extension and improvement. For example the score could be presented more artistically than in a variable watcher. You might prefer some different library sprites and you might add some more of them, maybe even of different types. There is certainly room for more sound inserts. You could end the app on a more positive note when the user has avoided, say, 30 dogs then move on to a higher level of difficulty.