Project 12
When you’re a simple, nonviolent squid, it can be hard to defend your ocean home. But armed with ink sacs, you can swim around safely, releasing a disorienting, black cloud whenever a nasty shark gets too close!
In Squid Ink, you build a shooter-style game. The squid’s motion is keyboard controlled, and the shark glides constantly in a direction towards the squid. Additionally, the squid carries hidden cargo — his ink cloud — that matches the squid’s x and y coordinates until it is shot at the shark. The touchedturtle commands in IF-THEN conditionals check whether the ink touches the shark, as well as whether the shark touches the squid. Recorded audio plays if either touching condition is met.
Shooter games can be adapted to a wide variety of themes, including
Begin creating your Squid Ink game by starting a new project as follows:
A new project opens.
Follow these steps to add a background for your Squid Ink game that conveys an ocean theme:
The Painting/Clipart palette opens.
The image is stamped to fill the entire workspace.
The completed background is shown in Figure 12-1.
Leave the Painting/Clip Art palette open.
Create a squid character for your game as follows:
All the squid shapes are selected, as shown in Figure 12-3.
The turtle now wears the animated sequence of squid, as shown in Figure 12-4.
Create an ink cloud character for your squid to shoot as follows:
Keep the ink turtle backpack open, but move it out of the way.
The Shape Editor opens.
To increase the size of the drawing space, decrease the zoom percentage and then pull the sizing dots.
The completed ink cloud shape now appears on the project Shapes pane.
The turtle now wears the shape, as shown in Figure 12-7.
If you accidentally click somewhere other than the turtle, the ink cloud shape will appear on the background — simply right-click (Windows) or Ctrl-click (Mac) the shape and select Remove from the pop-up menu to get rid of it.
A shark character roams the ocean scene and serves as a target where the squid shoots its ink. Create an animated shark character as follows:
The Shape Editor opens.
The Shape Editor opens.
Now, all three shark shapes appear in spots on the project Shapes pane, as shown in Figure 12-10.
All three shark shapes are selected.
The turtle now wears the animated sequence of sharks, as shown in Figure 12-11.
Your game will use a go procedure to start the game action. Write the go procedure and then make an associated Go button.
Follow these steps to write the go procedure:
to go
everyone [setx random 740 clickon]
tto [squid shark] st
ink, ht
end
The go procedure instructs everyone — the squid turtle, the ink turtle, and the shark turtle — to set its x-coordinate to a random number up to 740, which is the width of the workspace. This has the effect of placing the turtles in random positions horizontally across the screen, with the objective being to separate the squid from the shark at the start of the game. (See Project 10 for an explanation regarding how this command scatters turtle objects into random positions.) The clickon portion of the everyone command then clicks on all three turtles. You will write instructions in the OnClick fields of each turtle so that they know what commands to perform when clicked on.
The procedure then talks to (tto) the squid and shark, instructing them to show turtle (st).
The command ink, ht tells the ink turtle to hide because the squid hasn’t shot the ink cloud at the shark — yet!
After you have written the go procedure, MicroWorlds EX recognizes it as a new command that you can use. Follow these steps to create a Go button to run the procedure:
The Go button is added to the workspace.
A swim and shoot (swimshoot) procedure lets a player move the squid around the ocean and shoot ink by using the keyboard. Follow these steps to write the procedure and add it to the squid:
For a Windows game:
to swimshoot
let [key readchar]
if (ascii :key) = 37 [seth 270 fd 10]
if (ascii :key) = 38 [seth 0 fd 10]
if (ascii :key) = 39 [seth 90 fd 10]
if (ascii :key) = 40 [seth 180 fd 10]
if (ascii :key) = 32 [shoot]
end
For a Mac game:
to swimshoot
let [key readchar]
if (ascii :key) = 28 [seth 270 fd 10]
if (ascii :key) = 30 [seth 0 fd 10]
if (ascii :key) = 29 [seth 90 fd 10]
if (ascii :key) = 31 [seth 180 fd 10]
if (ascii :key) = 32 [shoot]
end
swimshoot begins with a basic keyboard-control procedure that allows the player to use the keyboard to move the squid around the ocean in four directions. It then extends the procedure by adding the execution of a shoot procedure whenever the spacebar is pressed: if (ascii :key) = 32 [shoot]. Don’t worry about the shoot procedure yet; you will write it in the next section.
After players click the Go button — a process that clicks on the squid — they can use the keyboard arrows to move the squid around the ocean, and press the spacebar to fire ink.
If you plan to distribute your game on both Windows and Mac platforms, you can put both sets of ASCII keyboard commands into the swimshoot procedure so that the game operates everywhere. Just follow the “if” line of code for Windows with the “if” line of code for Mac on the next line.
A shoot procedure allows the squid to suddenly reveal a secret weapon — a puff of ink! When shoot executes, the ink cloud changes from hidden to showing, and moves at a target in the direction the squid is headed.
Continue working on the project Procedures pane and write a shoot procedure as follows:
to shoot
tto [ink]
setx squid's "xcor
sety squid's "ycor
seth squid's "heading
st
glide 200 0.4
ht
tto [squid]
end
When a player is driving the squid around the ocean using the swimshoot procedure, the shark approaches. The player presses the spacebar to shoot at the squid. This action executes the shoot procedure, which works as follows:
glide is an alternative motion command for forward, and it is useful whenever you want to create smoother movement. To use the glide command, indicate both a distance and a speed, as in glide 200 0.4, which means move 200 pixels at a speed of 0.4. Tinker with the distance and speed to create the motion you desire.
Although squid and sharks don’t actually talk, you can guess what they might say if they did! If you have a microphone available on your computer, the audio recording feature of MicroWorlds EX allows you to record voices and sounds for inclusion in your programs. Create new recordings as follows:
The Record dialog box opens.
You want to see the name of the recorded sound when you look for it on the Project tab, but you don’t want the sound icon visible in the workspace. Also, embedding the sound into the project means you won’t have to worry about the location of the sound if you want to share your game file — the sound will be part of the file — although it does cause your file size to increase quite a bit.
A Media dialog box appears, as shown in Figure 12-15.
(The check boxes will already be checked according to the preferences you set previously.)
The argh sound is added to your project. The icon representing the sound does not appear in the workspace because the Visible check box was not selected.
The Project tab opens and shows all pages and elements in your project.
You can edit or show a project element that is hidden by right-clicking (Windows) or Ctrl-clicking (Mac) the element at the Project tab, and then selecting Edit or Show from the pop-up menu that appears.
If the shark touches the squid, it’s calamari time! A geteaten procedure is executed when the squid and shark are touching. Follow these steps to write the procedure and add it to the squid:
to geteaten
if touchedturtle = "shark [squid, ht chomp stopall]
end
Because this procedure will be placed in the squid backpack, the touchedturtle command evaluates whether the squid is touching the shark. If they do touch, the consequence of the IF-THEN statement is executed: [squid, ht chomp stopall]. The squid is instructed to hide (because he has been eaten!), and the chomp recorded sound plays. The game then halts via the stopall primitive.
If the squid is touching the shark, the geteaten procedure is executed.
In MicroWorlds EX, the touchedturtle command allows you to program a turtle object to respond only to specific turtles it bumps into, while ignoring others. All programming languages allow you to specify the parameters of a collision and how participants will react in a collision.
Freeze your squid and shark turtles to prevent the player from clicking and moving them during the game.
If the ink turtle touches the shark, he’s been inked! A hit procedure is executed to indicate a successful hit. The hit procedure will be placed inside the ink turtle’s backpack. Follow these steps to write the procedure and add it to the ink turtle:
to hit
if touchedturtle = "shark [ink, ht launch [argh]
shark, bk 10]
end
Because this procedure will be placed in the ink backpack, the touchedturtle command evaluates whether the ink is touching the shark. If they do touch, the consequence of the IF-THEN statement is executed: [ink, ht launch [argh] shark, bk 10]. The ink is instructed to hide (because it hit the shark), the argh recorded sound is launched for play (see the nearby “Launching a parallel process in a procedure” sidebar), and then the shark backs up (bk 10) as if recoiling from being hit.
If you closed the backpack earlier and the ink turtle is hidden, access it by opening the Project pane, expanding Page 1, and double-clicking on the ink turtle icon.
If the ink touches the shark, the hit procedure is executed.
Like all sharks, your game shark moves around the ocean looking for a tasty treat. A float procedure is executed so that the shark can move around. Float includes not only the speed at which the shark moves, but also the direction: He should always be headed towards the squid!
Follow these steps to write the float procedure and then place it inside the shark turtle’s backpack:
to float
glide 3 0.01
towards "squid
end
float consists of a simple glide command, followed by towards "squid. The towards command sets the heading of the shark to constantly point towards the squid. Feel free to change the parameters of the glide command to adjust the speed of the shark.
Choose File⇒Save from the menu bar to save your game. Your finished game should look similar to Figure 12-20. (Note: I’ve moved the divider between the workspace and the project Procedures pane so that you can see all the procedures.)
Test your game by clicking the Go button to start the action. Use the arrow keys to swim the squid around the ocean, and press the spacebar to ink the shark. Play it repeatedly and ask friends to help test it as well. Remember to click the Stop All button if the game is glitchy and you want to stop and examine your code. This will help you work out the bugs and obtain a polished, playable game!
3.15.156.140