Sometimes videogames are there to bring the player into the world of their favorite stories or movies, like Atari’s Star Wars game or E.T. the Extra-Terrestrial. That was supposed to be the case with Spy Hunter. It was going to be a video game adaptation of the James Bond world, but Bally Midway couldn’t get the license.
What you get instead is an endless racing game where the player has to drive among other cars while remaining on the road, saving the good guys and destroying the bad ones. The car can pop into a weapons truck and get more supplies, including guns and missiles.
There has always been violence in video games. Some people think that’s a good thing because it’s a way of processing anger without having real consequences. Others think it’s a bad thing that turns people more violent because they observe it on the screen. We decided to leave the beaten path of violence and make a game like Spy Hunter that doesn’t involve a single gun, and we challenge you to think outside the box when designing your games and consider how you can be creative instead of repeating old video game clichés.
Mermaid Splash is a racing game, swapping out a car for a mermaid. Unlike Spy Hunter, with its narrow road lined by grass on either side, the whole screen is the ocean and fair game for Octavia the mermaid to stay in-bounds. Instead of shooting at enemy cars, Octavia needs to dodge the jellyfish and turtles shown in figure 1. The ocean is endless, and the game is similar to School Escape in that it creates that endlessness by giving the illusion of movement without having Octavia move.
Driving and racing games are still popular today, though most games have switched from the old third-person, top-down perspective to a first-person perspective to give the player the sensation that they are the one driving the car. Think about which gaming perspective is more challenging and interesting as you start designing your own racing games.
Part of being a programmer is writing scripts that do a lot without taking up a lot of space. In this game, you’ll learn how to combine similar sprites in order to reduce the number of scripts while keeping the same number of obstacles.
In this section, you will learn
Eleven scripts will help you make your first racing game—and the last game in this book. By the time you hit the last page of this section, you’ll know how to make seven different kinds of games.
It’s time for your pre-coding ritual. Get your sprites and the Stage ready for Mermaid Splash.
If you skipped the previous section, go back and finish it or go to the Manning site and download the background and sprites for Mermaid Splash. The directions for importing are the same as in chapter 5. You should have Octavia the mermaid, a turtle, jellyfish, retry button, barrier line, and the ocean background.
You need to combine the turtle and jellyfish into a single sprite. Navigate to the Sprite Zone and click the jellyfish. Open the Costumes tab in the Block Menu and drag the sprite from the Costumes tab into the turtle sprite in the Sprite Zone. Make sure the jellyfish transferred into the turtle sprite by clicking the turtle sprite and looking at the Costumes tab to make sure there are two costumes. Rename this combined sprite Animals, as shown in figure 2. Delete the single jellyfish sprite.
All the sprites are their original size and have not been enlarged or shrunk. Drag the mermaid to the center of the Stage, a centimeter from the bottom (see figure 2). Move the retry button to the center of the Stage. Everything else will be positioned with code.
The mermaid is the equivalent of the car in Spy Hunter. Hopefully, seeing a mermaid instead of a vehicle will help you get creative with your future racing games. The player will be able to move the sprite back and forth along the bottom of the Stage using the arrow keys in order to dodge the jellyfish and turtles. The sprite itself will move, too, because it’s a coded animation. Her arms will move up and down, as if doing the butterfly stroke to propel herself through the water. Octavia has four scripts: a movement script, animation script, hiding script, and distance script. All scripts in this section are applied to Octavia, so go put the blue box around the mermaid sprite in the Sprite Zone and don’t move the blue box until you program the animals (jellyfish and turtle) in the next section. Remember, the names or values on your blocks may differ slightly from time to time, so use the completed script images to make sure you chose the correct block.
Octavia the mermaid needs to get away from the jellyfish and turtles trying to catch her (figure 3), but currently she can’t move.
This script allows the player to move the mermaid left and right at the bottom of the screen:
The mermaid won’t be able to move on the Y-axis (though it will look as though she is swimming forward), but she will be able to move back and forth along the X-axis with the script in figure 4.
The mermaid’s arms are down by her sides in figure 5, but that doesn’t look realistic if she’s swimming forward.
This script will make the mermaid sprite quickly switch between her two costumes, one with her arms up and one with her arms down, giving the illusion of swimming:
The small script in figure 6 animates the mermaid sprite.
Every sprite will be on the screen the whole time, though sometimes the sprite will be visible and sometimes it will be hidden. In the case of the mermaid sprite, you want her to be hidden (as she is in figure 7) when the retry button is on the screen at the end of the game.
This script hides the mermaid when the retry button comes on the screen at the end of the game, though the jellyfish and turtles will still be visible for a short period of time:
The condition is if currentlyPlaying is true, then show the mermaid—meaning, the game is on, so the mermaid should be visible. Or else, if the game is not on, the mermaid should be hidden. You can see the completed script in figure 8.
If you need a brief review of Booleans, turn back to chapter 3, or return to the game Dribble where you used zero (0) to indicate false/off/no and 1 to indicate true/on/yes.
It wouldn’t be an interesting game if you had no clue how well you did dodging the jellyfish and turtles. The score in this game, shown in figure 9, is tallied by the distance covered by the mermaid as she swims.
This script increases the player’s distance score by 10 every second, which means the player has been moving for 7.2 seconds and has gone 72 feet in the score above:
The completed script in figure 10 shows how the score is tallied in the game.
The mermaid is now done and it’s time to turn your attention toward her enemies: the jellyfish and turtles.
The jellyfish and turtles, the enemies who are trying to catch Octavia, are like the other cars in Spy Hunter. You’ve combined them into a single sprite, which means that you only need to write scripts in one place and they’re applied to both enemies. This is a trick that will add variety to your game while lessening the chance that the various sprites will have conflicting scripts. The animals have four scripts: a hiding script, cloning script, movement script, and zigzag script. Make sure the blue box is around the animals in the Sprite Zone and keep it on the animals sprite for all four scripts. It doesn’t matter which costume is showing in the Sprite Zone thumbnail.
It would be a quick game if the player could always predict where the enemy will spawn. It becomes a much more difficult game when the location of the enemy, as shown in figure 11, is hidden:
This script hides the location of the sprite and randomly generates a copy—switching between the two possible costumes—anywhere along the X-axis.
Figure 12 shows the whole script. The enemy goes to a random, hidden location at the top of the Stage and either wears the jellyfish or turtle costume.
How often should the enemies spawn? If they come too slowly, the game becomes boring, but if they spawn too quickly, the game becomes impossible to win. The solution is a script that has the rate in which the enemies clone increase over time, as shown in figure 13.
This script will set the initial values for a few variables at the start of the game—such as giving the player three chances—and then tells Scratch how frequently the enemies should spawn:
That final block, shown in figure 14, tells Scratch to reduce the time between new enemies spawning by a tenth of a second every five sprites. The game will subtly get harder the longer it goes on.
The animals are swimming through the water but not bothering the mermaid. What happens if the jellyfish or turtles touch the mermaid, as in figure 15, or the animal reaches the barrier line at the bottom?
This script will tell Scratch what to do if either animal touches the mermaid or the barrier line:
Figure 16 shows what will happen if the sprite touches the mermaid or the barrier line.
Currently, the animals are falling in a straight line. Even when they start coming quickly, it will still be a fairly easy game. Making them move in a random, zigzag line, as shown in figure 17, will make it harder to know where the jellyfish or turtle will go next.
The jellyfish and turtles will move randomly down the Stage, though it looks as if the mermaid is swimming past her enemies due to the top-down perspective:
The jellyfish and turtles will change their X-position by a random amount, between 10 spaces to the left or right. Scratch will keep randomly generating the amount, so it will be impossible to predict where the animal will go with the script shown in figure 18. From the top-down perspective, it will look as though Octavia is swimming past her unpredictable enemies, and the enemies will look as if they are lunging at the mermaid.
The retry button appears at the end of the game, and it allows the player to restart the game without clicking the green flag again. The barrier line serves the same purpose it always serves, to establish the bottom of the Stage.
When your game begins, you don’t want to see the retry button (figure 19).
This script will show or hide the button depending on whether the game is on or off. You don’t need to write a script; you can borrow and tweak a script you’ve already written.
Navigate to the mermaid and drag her hiding script (remember to click and drag the top block in the script to make all the blocks move together) over to the retry button’s sprite in the Sprite Zone, as shown in figure 20. Release the script, and then make sure the blue box is around the retry button in the Sprite Zone so you can tweak the script.
Instead of having the game “on,” which is indicated by the 1 in the If currentlyPlaying = 1 block, turn the game “off” by typing zero (0). Now the script (shown in figure 21) states that if the game is off, the retry button should show, and if it’s on, the retry button should hide.
There’s another option that invites the player to try again beyond leaving them a note to reclick the green flag each time they want to play. In figure 22, the cursor is about to click that retry button in the center of the screen, which will start the game over.
The retry button always pops up when the game ends. Make sure the blue box is still around the retry button in the Sprite Zone:
Figure 23 shows the whole script.
Right now the game will continue, even if the player goes into negative lives. You need to set a stopping point so the game will turn off. You also need to write a script to send the line into position, as shown in figure 24.
This last script applies to the line, so place the blue box around the barrier line in the Sprite Zone:
Figure 25 shows the whole script that turns off the game when the player runs out of lives.
It’s time to play your game. Ours worked perfectly on the first try, but you may run into some bugs with your game. Whenever you run into bugs, first check that your sprites are centered. Open each sprite in the Art Editor and center it using the tool in the top right corner of the canvas. Next, make sure the mermaid is on the top layer. Click the mermaid on the Stage and move her a tiny bit to ensure she is the top sprite.
You may notice the enemies still falling, even after Octavia disappears and the retry button appears on the screen. This is not a glitch: all enemies that already spawned will continue to move through the water after the game is over, though new ones won’t continue to spawn.
There was one Boolean that kept popping up over and over in this game, used with the currentlyPlaying variable. Sometimes the value of the variable was 1, which means true or on, and sometimes the value was zero (0), which means false or off. This is the black and white nature of programming: either the game is on or the game is off, which means either the Boolean is true or the Boolean is false. It can’t be a little bit of both at the same time.
Like School Escape, Octavia the mermaid can keep swimming indefinitely because she isn’t really swimming forward. You can make the game even harder by working in some of the following challenges.
Octavia can swim anywhere on the screen and still be in-bounds. What if you redesign the backdrop to create a watery road for Octavia to follow? You’ll have to determine the width of the lane and code in what will happen if Octavia leaves the boundaries and swims off the road.
Create a few more enemies and either combine them under the Animals sprite, tweaking the code, or give them their own separate scripts so they behave differently from the jellyfish and turtles. What if there is a fish that randomly appears, and if she grabs it, it shoots her forward an extra 20 feet? Or a seashell she can collect that protects her from the enemies for 30 seconds?
Play with the zigzagging range to make the game even harder. What if the jellyfish can unexpectedly zoom across the screen and touch the mermaid, even if she’s on the other side? It makes for a dangerous passage.
Make the mermaid tilt left and right as she moves to the left and right. Can you write a script that slightly rotates the mermaid as she moves, so the left arrow tilts the mermaid a few degrees and the right arrow tilts her in the opposite direction?
Before you return to helping Octavia make her escape, take a moment to reflect which common computer science ideas from chapter 3 were used in this game:
This final game uses seven out of eight common programming ideas! Additionally, in Mermaid Splash you learned
We hope you enjoyed this extra game, but it’s time to return to the book and catch up on how you can join the larger Scratch community.
3.143.4.181