Creating a Game

In this game you will control a small character (a happy face) and move your player around the screen by using the mouse.  You will want to get points by collecting gold coins.  However, watch out for the leprechaun.  He isn't happy with you taking his coins.  If he catches you he will take all his coins back.

The finished game will look something like this:




We can build the game in simple stages:


Main Player Costume (Step1)

First we need to open up a graphic to represent the main character in our game (the one moved around by the human player)

  1. If you haven't done so, open a new scratch project (use the "New" button).
  2. Select the costumes tab from the center work area and change the costume to look like a green "smiley" face by pressing the Import button on the Costumes tab.
  3. Switch to the irish folder located on the desktop of your computer by pressing the desktop button on the left side and then double clicking on the irish folder.
  4. Select the image named   player-normal from this and then delete the other costumes by pressing the delete buttons. You should also rename the sprite to player.


Mouse-following Player (Step2)

Now we want to write a script to add behavior to this character which will have him follow our mouse around on the screen.  This is a fairly easy thing to do in Scratch by using the pre-defined "point towards" command.

  1. Switch over to the Scripts tab.
  2. Add the point towards block available in the Motions tab into the script area
  3. Select the block and change the setting to show the mouse-pointer as shown below.
  4. A forever loop taken from the Controls blocks is used to control the movement so the player first points towards the mouse and then moves 5 steps. (You can control the speed of the player by changing this setting to speed up or slow down the action).
  5. It is good practice to always add a wait block whenever you have a forever control loop to make sure that all sprites can take their turn.
  6. Notice also that the initial size of the player is too big so we can use a set size block to change this.
  7. Finally, you can attach the forever loop to the green flag so that the player starts moving as soon as the game starts.



Test this now. Press that green flag above the stage and move the mouse around to watch the happy face follow your mouse.

Notice that when the happy face actually catches your mouse pointer it kind of flickers around and looks ugly.  We can fix this little bug by only moving the player sprite if it more than some small distance from the mouse pointer.

  1. Select an if block from the Control section
  2. Select a greater than block from the Numbers section
  3. Select a distance to mouse pointer block (from the Motion section)
  4. Put the pieces together so that it says "if distance to mouse pointer > 5"
  5. Remove the contents of the forever block from before and put it inside this if block, then put this entire if block back inside of the forever loop.  It should look like the code below


Random Movement Sprite (Step 3)

So the next part of our game is to add in a gold coin that moves randomly around the screen while we chase it. 

Create this new sprite by pressing the Choose new sprite from file button. (located just below the stage)

Choose the coin image from the irish folder and remember to name the sprite coin.

Random movement is created by using a forever loop to make the fish move at a constant speed and the random block from the Numbers section to turn the sprite a little with each loop as shown below.

The game now has two sprites which move around the screen. The player sprite follows the mouse and the coin sprite moves randomly around the screen.


Change the player's costume when it catches the Coin (Step 4)

We want our player to detect that it has "caught" the coin and "celebrate"

  1. Click on the player icon from under the stage to switch back to editing that sprite.
  2. Switch to the costumes tab and import a second costume for the player sprite using the player-happy image from the irish folder.
  3. We can now alter the forever loop controlling the player's behavior and change the costume whenever the player is actually touching a coin by using the touching sensor inside an if-else control block. If the player detects it is touching the coin sprite it turns to face the coin and switches to the happy face.
    1. Add an if-else control block.
    2. In the condition slot add the "touching coin" block from the Sensing section
    3. In the if-true section add the point-towards coin block and the switch to costume "happy" block
    4. In the else section add the switch to costume "normal" block
    5. Add this to the forever block by first removing the current contents of the forever block, adding it to the if/else block and then returning this whole set to the forever loop

When you are done it should look like

Test your code at this point and see how everything looks.


"Take" the Coin and increase the Score (Step5)

Games are more fun if you can get some sort of "high score."  To do this we need to keep track of how many coins the player catches. 

  1. Select the stage control icon from the area under the actual stage.
  2. Select the Make a variable button from the "Numbers" section and give it the name score.
  3. In the scripts area for the stage add a control block to set the score to zero each time the game starts.


Now add an if control block inside the forever loop of the coin sprite using the touching sensor block to have the sprite disappear if it is touching the player sprite.

  1. Add an if block to the script are of the coin.
  2. Make it's condition be if "touching player"
  3. Add blocks to "wait 0.25 seconds", "play sound pop" , change score by 1, and hide  as shown below.
  4. Add this to the beginning of the coin's forever block (to do this you may need to remove the current contents, join the current and new blocks, and then return the whole unit.
  5. Add a corresponding show block  at the start of the game to reset the game when the green flag is clicked.

Test your code by pressing the green flag and trying to catch the coin. 


Notice that if you do catch the coin that you will need to press the flag again to get the coin back again.  That probably doesn't make for a very good game because we will only ever be able to get a score of one.  Let's modify our coin sprite so that it randomly appears from the left or the right of the screen when the game starts and also when it has just been eaten. The behavior waits up to five seconds and then places the sprite randomly on the left or the right of the screen by exploiting the fact the the coordinates of the screen vary from -240 to +240 on the x-axis and -90 to +90 on the y-axis.

In the script area for the coin create the following block of code:

We want to use this block of code in two situations

Right click on the block of code and select "copy" to make two sections of this code

The first copy of this block should be added right after the "hide" when the coin gets "captured"

The second copy should get put right after the "start" flag (notice I also added in a "hide" block so that the coin isn't visible during this wait time:

Test this code out.  Notice that now you can chase around one coin but that it will, eventually, reappear after each time you catch it.


FINALLY, the behavior of the coin sprite is complete.  But suppose that we want to have more than one coin in the game. We can now export the sprite and reimport as many copies as we like. Select the sprite and export it into the irish folder on your desktop.  Then use the Choose new sprite from file (see above) button to import several copies of the sprite into the game.


You will just have to edit the script of each new coin sprite to set the if touching player control block to specify the player sprite. You may also need to save and reopen the project for the scoring to work with the new sprites.


Create the Leprechaun sprite (Step6)

The game is nearly finished now. However, we want to add in the "evil leprechaun"

  1. Import another coin sprite
  2. Change the name of the sprite to leprechaun
  3. Change its costume to the leprechaun image in the irish folder. (you also may delete the coin costume)
  4. Set the sprite to the player in the if touching control block.

At this point you can actually "play" the game, but the behavior of both the leprechaun and the player are set so that the player "captures" the leprechaun.  We want to modify this behavior so that the leprechaun takes all the player's coins (set the score to zero) and the player turns sad.  However, it turns out that we will add that to the player's scripts rather than the leprechaun's scripts.

Switch back to the player sprite

Import the player-sad costume from the irish folder into the player sprite and add the following behavior inside the forever loop which will play a drum and change the costume of the shark before setting the score to zero and putting the costume back to normal.

You should add this to the forever loop for the player. 


Change the Background (Step7)

The final step is to select the stage and import the forest image from the irish folder as the background.

The game is now complete.  Enjoy your game.


Full Credit where credit is due

This game is based on the  fish game from which in turn is based on a game distributed with Scratch (redware's is better in the long run)