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:
First we need to open up a graphic to represent the main character in our game (the one moved around by the human player)
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.
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.
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.
We want our player to detect that it has "caught" the coin and "celebrate"
When you are done it should look like
Test your code at this point and see how everything looks.
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.
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.
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.
The game is nearly finished now. However, we want to add in the "evil leprechaun"
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.
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.
This game is based on the fish game from redware.com which in turn is based on a game distributed with Scratch (redware's is better in the long run)