Remote Control Fish
Ok, now it’s time to make the fish swim on its own. To do this, you’re going to need a new kind of block: a Control block.
Select your fish sprite.
Drag a when green flag clicked
Event block, a forever
Control block, and a move 10 steps
Motion block into the sprite panel, like this:
What does the new block do?
Control blocks make your program do things a certain number of times, or under certain conditions.
Here, the fish does whatever is inside the forever
block over and over again on a loop, forever. So once it has done the last thing (block) inside the forever
block, it starts over at the top and does everything again, and so on.
Now click the green flag and watch what happens!
Well, that fish just crashed into the side of the Stage, and it was moving far too fast for your shark to catch.
First, you need to slow the fish down. That’s actually pretty easy, you just need it to wait for a little while after it moves those 10 steps. There’s a Control block that will help you here:
Add the wait
block into your code inside the forever
block, and change the number to 0.5
, like this:
Making adjustments
The number you set in the wait
block says how many seconds you want the fish to wait. 0.5
is half a second.
You can test out different values to see which is the best for the game. And remember that you can change the number of steps inside the move
block too!
The fish moves now, but you need it to bounce off the edge of the Stage too. Yet again, there’s a Motion block for this!
Find the if on edge bounce
block, and add it in after the wait
block.
What does the new block do?
The if on edge bounce
block checks if the sprite is touching the edge of the Stage and, if it is, it turns left, right, up, or down as appropriate.
Of course, this will lead to an upside-down fish, so you need a set rotation style
block again.
Update your code to set the rotation style of the fish to left-right
at the beginning of the sprite’s script:
The fish moves backwards and forwards now, but only in a straight line — a bit too easy for the player to catch with the shark! You need to make the fish less predictable.
You already know from a previous step how to make a sprite turn, so start there.
Add a turn into the fish’s swimming instructions, and click the green flag.
It’s better, but there’s still too much of a pattern. It needs to be more random. Luckily, Scratch can do random for you! You’ll just need a new kind of block, called an operator block.
What’s an operator?
Operators take in one or more values (like numbers, text, or True/False
values) and give back a single value. You can tell the kind of value it will give back by the shape of the block: round ends give numbers or text, pointy ends give True/False
.
Find the pick random
operator block, and plug it into the turn degrees
Motion block by clicking it and dragging it into the field where you set the number of degrees.
Note: you can change the minimum and maximum numbers it will pick, but the default values (1
and 10
) are pretty good for this game, so you can just leave them.
Click the green flag to run the code!
So what does the forever block do now?
The forever block now makes the fish sprite do four things in order:
- Move forward
- Turn a little bit
- Wait briefly
- Check whether it’s at the edge of the Stage
Once the sprite has done the check, it will start at the beginning of the loop again and move, turn, wait, check, for as long as you let your Scratch program run.
Cool! Next up: catching that fish!