Fish - Computer Kiddos Wiki

Scratch Programming Handbook home sitemap links
Fish !
Fish! is a simple game that can be built quickly by a novice scratch programmer. The main character is a shark which is controlled by the mouse and
wanders around the screen eating fish. Yellow fish taste nice but eating a red fish will give the shark indigestion.
The basic principles behind of this type of game can be used to make many different games simply by changing the background and the graphics on the
sprites. A game set in space with a rocket chasing astronauts and aliens would be simple to create using fish! as a base.
We can build the game from scratch in simple stages:
1.
2.
3.
4.
5.
6.
Create the shark sprite and control movement with the mouse.
Create the yellow fish sprite with random movement.
Change the shark costume when touching the yellow fish so it looks as though it is eating the fish.
Make the yellow fish disappear when it is eaten.
Copy the yellow fish sprie so that there are several fish in the game.
Add a red fish and the behaviour for the shart to get indigestion.
Shark Costume
Select the scratch cat sprite and change the costume to look like a shark by pressing the Import button on the Costumes tab. Select shark-a from the
animals folder and then delete the other costumes by pressing the delete buttons. You can also rename the sprite to shark.
Mouse-following Shark
Creating sprite behaviour to follow the mouse is made easy by the point towards block available in the motions tab. Select the block and change the
setting to show the mouse-pointer as shown below. A forever loop taken from the controls blocks to control the movement and the shark first points
towards the mouse and then moves 5 steps. You can control the speed of the shark by changing this setting to speed up or slow down the action. It is
good practise to always add a wait block whenever you have a forever control loop to make sure that all sprites can take their turn. Finally, you can
attach the forever loop to the green flag so that the shark starts moving when the game starts. Notice also that the initial size of the shark is set here
also.
Random Movement Sprite
The yellow fish sprites move randomly around the screen. Create the sprite by pressing the Choose new sprite from file button.
Choose the fish3 image from the animals folder and remember to name the sprite yellowfish.
Random movement is created by using a forever loop to make the fish move at a constant speed using 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 shark sprite follows the mouse and the yellowfish sprite moves randomly around the
screen. The game at this stage is available called fish-01.sb and is available for reference.
Show the Shark eating the Yellowfish
Import a second costume for the shark sprite using the shark-b image from the animals folder. We can now alter the forever loop controlling the shark's
behaviour and change the costime whenever the shark is actually touching a yellow fish by using the touching sensor inside an if-else control block.
The shark tunrs towards the Yellow Fish and opens it's mouth if it is touching the yellowfish otherwise the costume has mouth closed.
We also tidy up a feature of the mouse following behaviour which makes the shark flash if the mouse stops moving by only moving towars the mouse if we
are more than a short distance away. The project thus far is saved as fish-02.sb.
Actually Eat the Yellow Fish and increase the Score
Make a variable to show the score and make sure that it is made available to all sprites. Select the stage and 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 yellowfish sprite using the touching sensor block to get the sprite eaten if it is touching the
shark sprite. The behaviour plays a sound and increments the score variable before using the hide block from the Looks blocks to disappear from the
game. The corresponding show block is added at the start of the game to reset the game when the green flag is clicked.
The following blocks allows the yellowfish sprite to to appear from the left or the right of the screen when the game starts and also when it has just been
eaten. The behaviour 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. This behaviour is added when the game starts and just
after the sprite has been eaten.
The behaviour of the yellowfish sprite is now complete but we want to have more than one fish in the game. We can now export the sprite and reimport as
many copies as we like. Select the sprite and export it anywhere onto your local drive and 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 yellowfish sprite to set the if touching shark control block to specify the shark sprite. You may also need
to save and reopen the project for the scoring to work with the new sprites. The game is functional now and is available as fish-03.sb.
Create the Red Fish sprite
The game is nearly finished now. Import another yellowfish sprite but this time change the name of the sprite to redfish and the costume to the fish4
image in the animals folder. Remember to set the sprite to the shark in the if touching control block. The behaviour remains the same as the Yellow
fish as we will be altering the shark's behaviour when it touches the red fish.
Import the shark-c costume from the animals folder into the shark sprite and add the following behaviour inside the forever loop which will play a drum
and change the colours of the shark before seting the score to zero and putting the costume back to normal.
Change the Background
The final step is to select the stage and import the underwater image from the nature folder as the background. The game is now complete and
available as fish-04.sb and also on the scratch website. It should be easy to change the background and sprite costumes to customise the game to
whatever look and feel you want.