Project 11
UFO Pilot is a side-scrolling game set in an asteroid field surrounding a distant planet. You’ll use real space photos from the web to create your graphics. Then you’ll write the code to let a player take on the role of a little alien piloting his UFO!
But now that you’re becoming a master coder, you’re not going to make this an easy game. You’ll write commands to simulate gravity so that the UFO constantly falls towards the planet. And you’ll build in single-key keyboard control so that the player can make his alien fire thrusters to counteract gravity. New coding concepts, including keyboard input, scoring, and mirrored motion apply here and in future programming!
UFO Pilot is a side-scrolling style video game in the tradition of Flappy Bird. It can be adapted to a wide variety of formats including Defender (which includes shooting). Think about how you want to create your game:
Start a new project for your UFO Pilot game as follows:
A new project opens.
Add a background for your UFO Pilot game that conveys a space theme. A great way to do this is to paste actual space and planet photos on your game background.
Follow these steps to create a space-themed background with images from the web:
An easy alternative to using real space images for your game background is to use one of the many space backgrounds provided by MicroWorlds EX. Open the Hide/Show Painting/Clipart palette and click the Backgrounds button to show the backgrounds. Scroll down to locate the three space backgrounds, shown in Figure 11-2. Click the background image you want to apply to your game, and then click in the workspace to apply the background image. Right-click (Windows) or Ctrl-click (Mac) the background image and select Stamp Full Page. The image is stamped to fill the entire workspace.
The image is stamped to fill the entire workspace.
The moon or planet image is pasted into the shape spot, as shown in Figure 11-3.
The Shape Editor opens.
The wedge is now copied. (Note that MicroWorlds EX may change the lassoed shape to a rectangular shape, even though only the lassoed region is actually copied.)
The wedge now occupies a shape spot, as shown in Figure 11-5.
The wedge appears on the background.
The Painting/Clipart palette opens.
For example, use the Spray Can tool to spray additional white or blue stars across the background. Your completed background looks similar to Figure 11-6.
The UFO character consists of a ship and an alien pilot. In this example, the alien ship is facing east. Create a UFO character for your game as follows:
You may want to use the vertical mirror image buttons near the zoom selector in the upper-left corner to flip the ship in the direction of flight. You can use the paint tools to recolor the ship or draw a little alien pilot at the helm.
The edited alien spaceship shape now appears on the project Shapes tab, as shown in Figure 11-10.
The ufo turtle now wears the alien spaceship shape.
If desired, change the size of the ufo turtle. The default size is 40, but you can make the UFO bigger or smaller by typing a new number in the Size field of the backpack State tab. When finished, close the ufo turtle backpack by clicking its X button.
Don’t worry about the heading of the UFO — this will be set in other places in the game.
Asteroid clusters are the obstacles through which the player’s UFO must navigate. Crash into an asteroid cluster and the mission is done! In this example, asteroid clusters appear to be moving west.
Create six asteroid cluster characters for your game as follows:
Note that the background in the figure has been made mostly transparent so that you can see the turtles more easily. The turtles should not be perfectly lined up — variation in position makes the game more interesting.
Click OK to close the Name dialog box for each turtle.
The default value in the Size field is 40. Making turtles slightly larger or smaller will make the asteroid field look more varied and realistic.
Now, you need to create a cluster of asteroids in a column shape for each turtle. To do so, follow these steps:
The Shape Editor opens.
Now there is sufficient room to paint!
The asteroid column shape now appears on the Shapes tab, as shown in Figure 11-12.
The topcol1 turtle now wears the cluster of asteroids column.
All asteroid turtles now wear the same shape (refer to the title figure at the beginning of the project).
A score variable allows someone playing your game to keep track of how many asteroid columns they pass without crashing. Follow these steps to create a score variable for the game:
This text box will be used to show the value of a variable.
A text box used as a variable must remain opaque (not transparent) for the variable value to change.
A reset procedure sets the initial value of the score to zero and places the ufo at its starting position in the center of the workspace.
Create a reset procedure as follows:
to reset
setscore 0
ufo, home
end
Here’s how the reset procedure works:
During the game, you will change the heading of the ufo, so don’t worry about its heading right now. The shape of the UFO appears to be facing east, although the heading of its turtle will always be either north (seth 0) or south (seth 180).
Also, if desired, you can set the starting position of every asteroid cluster, although this is not necessary.
In a side-scrolling game, the main character never actually moves side-to-side — it only moves up and down (north and south). The other objects in the game move from side to side, creating the appearance that the main character is actually moving horizontally, even though it isn’t.
After you have written the reset procedure, MicroWorlds EX recognizes it as a new command that you can use. Create a button to run the procedure by following these steps:
The Reset button is added to the workspace.
See that moon at the bottom of the game? The gravitational pull of that celestial body on the UFO is making it extra difficult for the alien to pilot his way through the asteroid field. If the alien fails to navigate safely, he crashes into the asteroids!
Follow these steps to code the constant fall of the UFO towards the moon and code the crash between the UFO and asteroids:
When the ufo is clicked on via the clickon command (in the Go! button that you will create later), this command sequence causes the ufo to point south (seth 180) and move in that direction at a moderate speed (fd 5 wait 1) forever. This simulates the gravitational pull of the moon. In the next section, you code a keyboard command that lets the player counteract this gravitational pull by moving north.
Leave the backpack open for the next step.
Here’s how this code works: If the UFO touches any asteroid turtle, the announce command shows whatever text you type between the square brackets. Then, stopall ceases program execution — the entire game stops.
This is your first project that includes a keyboard-controlled interface. It uses a single key on the keyboard — the up arrow key — to execute the fly procedure by the UFO. The fly procedure lets a player fire the thrusters on the UFO to oppose the pull of gravity from the moon.
For a Windows game:
to fly
let [key readchar]
if (ascii :key) = 38 [seth 0 fd 20 wait 1]
end
For a Mac game:
to fly
let [key readchar]
if (ascii :key) = 30 [seth 0 fd 20 wait 1]
end
Simply put, the fly procedure reads the character of the key pressed by a player. If the key is the up arrow (ASCII code 38 in Windows, or ASCII code 30 on a Mac), the commands inside the square brackets are executed. Here, the example UFO has its heading set to north (seth 0) and then it moves in that direction 20 pixels (fd 20) with a wait of 1. You may need to tinker with the code to create the upward thrust you want.
Note that when the UFO turtle is clicked on, its OnClick commands constantly point and move it south. This fly procedure will be put into the Go! button, which you create later in this project.
See Project 12 for a more detailed explanation of how a keyboard-controlled procedure works.
As the player successfully navigates the UFO past each column of asteroids, a point is scored. Write a checkpass procedure to check for this successful passage.
to checkpass
if xcor = 0 [setscore score + 1]
end
Think about what is happening in checkpass. Because the UFO never moves horizontally, its x-coordinate is always 0. So if a moving asteroid column has an xcor of 0 and hasn’t collided with the UFO, the score increases by 1 (setscore score + 1). Because checkpass is based on the motion of the asteroids relative to the UFO, it will be placed in the asteroid turtles.
The asteroids will move from east to west across the screen, barraging the little UFO and leaving only a small gap for the alien pilot to fly through. There are three columns of asteroids. Each column of asteroids has a top turtle and a bottom turtle; that’s why you named them topcol1 and botcol1; topcol2 and botcol2; and topcol3 and botcol3.
In the following steps, you code the top asteroid turtles to move across the screen and include the scoring procedure, and code the bottom asteroid turtles to move along with their partners at the top:
Note that all top asteroid turtles are already pointed in the direction they will travel and this direction never changes.
After the top asteroid turtle is clicked on, this command sequence causes it to move forever one pixel at a time at a medium speed (fd 1 wait 0.1), and execute the checkpass procedure after each step. checkpass checks to see whether the asteroid turtle has an x-coordinate of 0, meaning if it is moving past the UFO; if it does, the player scores a point.
Each top asteroid turtle will now move according to the same commands.
Because each top asteroid turtle is only moving one pixel at a time, at some point it will have xcor = 0. If you adjust the speed of the asteroid, change only the wait time, not the forward distance. Otherwise, the asteroid may not have an xcor = 0 (for example, it may go from xcor = 1 to xcor = -1 or similar); if this happens, the score does not increase as it should.
Again, note that all bottom asteroid turtles are already pointed in the direction they will travel and this direction never changes.
After the bottom asteroid turtle is clicked on via the clickon command (in the Go! button, which you will create in the next section), this command sequence causes it to forever set its x-coordinate to match the x-coordinate of its partner top asteroid. This mirrored motion has the effect of making the top and bottom asteroid pair move together — they stay vertically aligned.
This is it, the final step! A Go! button starts the game. Create a Go! button as follows:
Here’s how the commands in the Instruction work. The everyone [clickon] command clicks all turtles on. For the asteroids, this means putting them in motion and checking to determine whether the score needs to be increased. For the UFO, that means making it constantly fall. Then, ufo, fly commands the ufo to execute the fly procedure — keyboard-controlled thrusters.
Because Go! is set to Forever, the execution of the clickon and the fly commands continues over and over until a collision occurs — at which point stopall ends the game.
The Go! button is added to the workspace.
Choose File⇒Save Project from the menu bar to save your game. Your finished game should look similar to Figure 11-19. (Note: Some of the commands in the procedures wrap onto the following line. This is normal for long procedures. It does not indicate a line break, so be sure to type the code as shown in the project text.)
Test your game by clicking Reset and then Go! to start the action. Use the arrow key to pilot the UFO through the asteroid field. Invite friends to play and see who can earn the highest score! If you notice any bugs, go back and revise your code, and then retest the gameplay.