© Lee Stemkoski and Evan Leider 2017

LEE STEMKOSKI and Evan Leider, Game Development with Construct 2, 10.1007/978-1-4842-2784-8_10

10. Airplane Assault

Lee Stemkoski and Evan Leider2

(1)DEPT OF MATH & CS, ADELPHI UNIVERSITY DEPT OF MATH & CS, Garden City, New York, USA

(2)NY, USA

In this chapter, you will be creating another top-down shooter game called Airplane Assault, shown in Figure 10-1, that was inspired by the classic survival-based game 1942.

A434545_1_En_10_Fig1_HTML.jpg
Figure 10-1. The Airplane Assault game

In Airplane Assault, the player controls a plane whose goal is to destroy as many enemy planes as possible for points. The player’s plane shoots small bullets , and if an enemy is hit, that enemy is destroyed. In contrast to Spell Shooter, the player will have a limited amount of health, which is reduced when hit by an enemy plane bullet. If the player’s health reaches zero, then the plane will be destroyed, and the game will be over. During gameplay, enemies will spawn every few seconds, move in a random pattern between waypoints, and face and attack the player with their own bullets. The player will need to move around the screen to line up shots and avoid enemy bullets at the same time. This game is endless; the implicit goal of the player is to earn a high score.

The player plane has eight-direction movement , controlled by the arrow keys. Shots are fired by pressing the spacebar. The user interface contains two Text objects that display the player’s current health points and score.

This chapter assumes you are familiar with the Sprite, TiledBackground, Text, and Keyboard objects; the 8-Direction, Bound to layout, Bullet, and Destroy outside layout behaviors; animations; global and instance variables; and the functions random, floor, and choose. The Flash behavior will be introduced to give the player a moment of invincibility after being hit. The Timer behavior will also be introduced to enable enemy planes to periodically fire at the player. You will learn about the angle function, which makes it easy to determine the angle between two objects or locations. Instance variables will be used in a new way: to add health points to objects.

To begin, download the zip file containing the graphics for this chapter from the book web site. In the layout properties, set the layout Name to Game, and set Size to 800, 800. As you have in previous projects, set up three layers named Background, Main, and UI. In the project properties, change the window Size to 800, 800. Add a TiledBackground object named Water using the image water.png, and resize it so it covers the entire layout area.

Player, Waypoint, and Enemy Setup

In this section, you will add the player plane object, the enemy planes that the player must shoot, and the Waypoint objects that determine the path that the enemy planes follow. In the layout, set the active layer to Main. Create a new sprite named Player, with the image player.png, and position it near the bottom of the layout. Change its Angle property to 270, and position it in the lower center of the layout. Add the behavior 8-Direction and change the property Set angle to No. (The player will face only upward during gameplay.) Also add the behavior Bound to layout.

Next, add a new sprite named Waypoint. This Waypoint object will be almost identical to the Vortex waypoint from the Spell Shooter game, except in Airplane Assault the waypoints will be invisible in the final version. Since a graphic isn’t needed, use the image editor tools such as the bucket or paintbrush to fill the image area with a solid color. Change the size of the sprite to 32-by-32 pixels. When you are finished, close the image editor windows. Create another new sprite named Enemy, with the image enemy-plane.png. Add the Bullet behavior, and change the properties Speed to 200 and Set angle to No, since the enemies will be facing the player rather than their direction of movement. Create five additional instances of the Waypoint sprite and two additional instances of the Enemy sprite, organizing them in the layout, as shown in Figure 10-2.

A434545_1_En_10_Fig2_HTML.jpg
Figure 10-2. The player, enemies, and waypoints with IDs in the layout

Add an instance variable named ID to the Waypoint object, set Type to Number, Initial value to 0, and Description to Unique identification number. Then, to the Enemy object, add an instance variable named Target, set Type to Number, set Initial value to 0, and set Description to ID of the Waypoint to move towards. In the layout editor, click a single waypoint in the layout, and in the Properties panel, set the value of ID to 0. Click another waypoint and set its ID value to 1. Repeat this for each waypoint in the layout until they each have one of the numbers from 0 to 5 (each number occurring exactly once), as shown in Figure 10-2.

Next, you will randomly set the Target values of the enemy airplanes at the beginning of the game as you did in Spell Shooter with the event condition On created. First, in the event sheet, create an event with the condition Enemy - On created, and set the action Enemy - Variables: Set value. Then set Instance variable to Target, and set Value to floor(random(Waypoint.Count)).

You will now create movement events that rotate and move enemies toward their intended target waypoints, with the For Each and ID comparison conditions. Create a new event with the condition System - Loops: For Each, and select Enemy. Add another condition called Waypoint - Compare instance variable, and check whether the ID is equal to Enemy.Target. In Spell Shooter, you used the Rotate towards position action to rotate sprite images toward vortex waypoints and also to move them toward these waypoints using the Bullet behavior (in that project, the Bullet property Set angle was set to Yes). In Airplane Assault, the enemy plane sprite images will rotate to face the player while they are moving in a different direction toward waypoints. To accomplish this, you will use two actions: Rotate towards position and Bullet: Set angle of motion. With Set angle of motion, you will use the angle function, which takes as input the X and Y coordinates of two locations (a total of four inputs altogether) and returns the angle between those locations. Add the action Enemy - Rotate toward position, set Degrees to 10, set X to Player.X, and set Y to Player.Y. Add another action Enemy - Set angle of motion, and set Angle to angle( Enemy.X, Enemy.Y, Waypoint.X, Waypoint.Y ).

Next, you will add the events for when an enemy reaches its destination, at which point they should select their next target (which should involve some randomness). Create a new event with the condition Enemy - On collision with another object, and select Waypoint. To verify that the enemy has collided with its actual target, add a second condition called Waypoint - Compare instance variable, and again check whether the ID is equal to Enemy.Target. Add the action Enemy - Variables: Add to. For Variable, select Target, and for Value enter choose(1, 2).

You also need to make sure that the value of the Target variable does not exceed the total number of waypoints (Waypoint.Count). Add a subevent with the condition Enemy - Compare instance variable, and check whether Target is greater than or equal to Waypoint.Count. Then, add the action Enemy - Variables: Set value, and set Target to choose(0,1).

When you are finished adding all the content described in this section, the corresponding events should appear as shown in Figure 10-3. Save and test your project. Make sure you can move your player with the arrow keys and that the enemies move randomly while facing the player.

A434545_1_En_10_Fig3_HTML.jpg
Figure 10-3. Events to set the enemies to rotate toward waypoints

Shooting and Spawning Enemies

In this section, you will add bullets for the player and enemies to shoot at each other and explosions that appear when they hit. You will also add random enemy spawning as well as configure the Timer behavior to set up a randomly periodic event that causes enemy planes to shoot.

First, in the layout, now that you have verified the enemy movement between waypoints works as expected, you no longer need to see the waypoints on the screen; click the Waypoint object in the object panel (so that all instances are selected) and set Initial visibility to Invisible. Next, create a Keyboard object. Then add two new sprites: one named PlayerBullet with the image bullet-gold.png and another named EnemyBullet with bullet-silver.png. Add the Bullet and Destroy Outside Layout behaviors to both. Position the bullets in the margins of the layout. Add another new sprite named Explosion; in the Animation frames window, import animation frames from the sprite strip named explode-animation.png, which consists of four rows and four columns. Set the animation properties Speed to 30, and set Loop to No. Close the image editor and position the explosion in the layout margins.

In the event sheet, create a new event with the condition Keyboard - On key pressed, and select the Space key. Add the action Player - Spawn another object, select PlayerBullet, and set Layer to "Main". Next, create a new event with the condition PlayerBullet - On collision with another object, and select Enemy. Add the following three actions: PlayerBullet - Destroy, Enemy - Spawn another object to spawn an Explosion object on the Main layer, and Enemy - Destroy. To remove explosions from the game once their animations are complete, create one more event with the condition Explosion - Animations: On any finished, and add the action Explosion - Destroy.

Since randomness adds to the challenge and replayability factor of this game, you will add periodically spawning enemies at random positions above the layout. In addition, you will implement a random shooting rate to prevent enemies from shooting at the same time; having enemies act differently from each other adds to the realism of the game.

To do this, you will use the Timer behavior, a new action named Start timer, and a new condition named On timer. The Start timer action determines when the corresponding On timer condition will register as true; both the condition and the action contain a Tag parameter, which is used to set up the association by giving these the same value. The Start time condition contains two additional parameters: Duration, which is used to specify how much time will pass until the On timer condition is first activated, and Type, which can be set to Once (which activates the condition one time) or Regular (will periodically activate the condition). In this game, every time an enemy spawns, it will start a timer called Shoot, which will repeat at a random rate, between 0.5 and 1.0 seconds. When an enemy’s On timer condition activates, it will shoot a bullet toward the player.

In the layout, select the Enemy object, and add the behavior Timer. Then, move the enemy objects into the margins above the layout. In the event sheet, create a new event with the condition System - Every X seconds, and set Interval to 1. Add the action System - Create object, set Object to Enemy, set Layer to "Main", set X to random(100, 700), and set Y to -100. In the event with the condition Enemy - On created, add the action Enemy - Start timer, set Duration to 0.5 + random( 0.5 ), set Type to Regular, and set Tag to "Shoot". Next, create a new event with the condition Enemy - On timer and set Tag to "Shoot". Then add the action Enemy - Spawn another object to spawn an EnemyBullet object on the Main layer.

When you are finished adding all the content described in this section, the corresponding events should appear as shown in Figure 10-4. Save and test your project. Make sure you can shoot bullets with the spacebar key to destroy enemies and that enemies are shooting toward you at random rates.

A434545_1_En_10_Fig4_HTML.jpg
Figure 10-4. Events for shooting and spawning more enemies

Score, Health, Invincibility, and Game Over

In this section, you will implement player health and invincibility game mechanics, create the scoring system and user interface, and add the “game over” message that appears when the player runs out of health. After the player is hit by an enemy bullet, the player will lose one health point, and a Text object will update to indicate the player’s current health.

First, add a new Text object named TextScore to the project. Set its Layer to UI, and position it in the bottom-left corner of the game window. Change the default text to Score: 0. To make the text easier to read, change the font to Arial, bold, size 24, and set the font color to black. Add another new Text object named TextHP to the project. Set its Layer to UI, and position it in the bottom-right corner of the game window. Change its default text to HP: 0, and change the font to Arial, bold, size 24. Also set the Horizontal alignment property to Right.

You will add health points as an instance variable to the player object since they are associated to a value intrinsic to the player, in contrast to the score, which is associated to the game as a whole. Add an instance variable named HP to the Player object, set Type to Number, Initial value to 10, and Description to Player health points. In the event sheet, create a new global variable named Score with the initial value 0. Then create an event with condition Enemy - On destroyed and action System - Variable: Add to, adding 100 to Score. Create another event with the condition System - Every tick and the two actions TextScore - Set text to "Score: " & Score and TextHP - Set text to "HP: " & Player.HP.

When an enemy bullet collides with the player and the player is not currently invincible (which occurs for a brief period after having been damaged), a small explosion should appear to indicate the damage, the player’s health should decrease by 1, and the player sprite should flash for 1 second to indicate invincibility. To implement the invincibility feature, you will use the Flash behavior, a new action called Flash, and a new condition called Is flashing. The action Flash will then cause the object associated to alternate between visible and invisible at a specified rate. The visibility duration is specified by the On time parameter, the invisibility duration by the Off time parameter, and the overall effect duration (how long the entire flash sequence should last) by the Duration parameter. You can use the Is flashing condition to check whether the player is flashing and thus whether the player should be able to take damage at that particular time.

To implement this, add the Flash behavior to the Player object. In the event sheet, create a new event with the condition EnemyBullet - On collision with another object, and select Player. Add another condition Player - Is flashing. When finished, right-click this condition in the event sheet, and select Invert from the menu that appears. Then add the following actions:

  • Add EnemyBullet - Destroy.

  • Add Player - Spawn another object, and spawn an Explosion object on the Main layer.

  • Add Player - Variables: Subtract from. For Variable, select HP, and for Value, enter 1.

  • Add Enemy - Flash. For On time and Off time , enter 0.1, and for Duration enter 1.

Finally, you will implement the “game over” functionality. In the layout, add a new sprite named GameOver with the image gameover.png. Positon it in the center of the layout, set its Layer to UI, and set Initial Visibility to Invisible. When the player’s health reaches 0 (or below), the “game over” message will be displayed, the player will be destroyed, and a giant explosion will appear (many times larger than the default size). In addition, enemies should stop spawning, and any remaining enemies will rotate toward and fly off the bottom of the layout (since there is no player object remaining to shoot at). For the enemies to rotate and move differently, the For each event that currently rotates and moves enemies should be restricted to work only while the player exists (or is onscreen); otherwise, they will interfere with the “game over” sequence of movements discussed earlier. In the event sheet, locate the For each event, and add the condition Player - Is on-screen. Also, add this condition to the Every 1 second event to stop enemy planes from spawning after the player is destroyed. Then create a new event with the condition Player - Compare instance variable, and check whether HP is less than or equal to 0. Then add the following actions:

  • Add GameOver - Set visible, and set it to Visible.

  • Add Player - Spawn another object, and spawn an Explosion object on the Main layer.

  • Add Explosion - Set scale, and set it to 8.

  • Add Player - Destroy.

  • Add Enemy - Angle: Set angle, and set Angle to 90 degrees.

  • Add Enemy - Bullet: Set angle of motion, and set Angle to 90 degrees.

When you are finished adding all the content described in this section, the corresponding events should appear as shown in Figure 10-5. Save and test your project. Make sure you receive points for destroying enemies and that you can lose health and flash upon taking damage. Also test that when you run out of health, the game ends properly.

A434545_1_En_10_Fig5_HTML.jpg
Figure 10-5. Events for score, player health, and game over

Congratulations! You have now finished implementing the core mechanics of the Airplane Assault game.

Side Quests

In this optional section, you will add endless vertical scrolling along the vertical axis to your water background, similar to the effect from the Plane Dodger game, with a few key differences. This will add a sense of global movement to the gameplay. A list of suggested additional features will also be presented for your consideration.

Endless Vertical Scrolling

First, in the layout, for the Background sprite, change the Position property to 0, 0, and set Size to 800, 800. Add the Bullet behavior, change Speed to 200, and change Set angle to No (because this object should not be rotated). Create another instance of Background, and set its Position to 0, -800. The layout should appear as shown in Figure 10-6.

A434545_1_En_10_Fig6_HTML.jpg
Figure 10-6. The layout adjusted for endless vertical scrolling

In the event sheet, create a new event with the condition System - On start of layout, add the action Water - Set angle of motion, and set Angle to 90. Next, you will create the event that shifts Background above the layout after it moves off-screen on the bottom. Create another event with the condition Water - Size & Position: Is on-screen. When finished, right-click this condition in the event sheet and select Invert from the menu that appears. Add the condition Water - Compare Y, and check whether the Y coordinate is greater than 800. Finally, add the action Water - Move at angle, with Angle set to 270 and Distance set to 2 * Water.Height. When you are finished, these events should appear as in Figure 10-7. Save and test your project; the background image should appear to scroll forever, with no noticeable gap between the two images.

A434545_1_En_10_Fig7_HTML.jpg
Figure 10-7. Events for endless vertical scrolling

On Your Own

Airplane Assault is a great starting point for applying many features and items you have learned so far. Adding menus, audio, and pause functionality would certainly give this game a more polished presentation. To make the game a bit more difficult, you could add enemy health points. To do this, you could add a health point instance variable to the enemy and, in the Enemy - On collision with PlayerBullet event, replace the Destroy action with one that subtracts a health point instead. You could then add an event for when the Enemy’s health is less than or equal to 0 and proceed to destroy them as you used to do. You could also add falling obstacles and items that positively or negatively affect the player’s characteristics such as speed.

Here, we list some other ideas of varying difficulty to implement:

  • Adjusting the player’s collision polygon to be smaller (thus increasing their chances of survival)

  • Adding analog gamepad support (discussed in Chapter 5)

  • Gaining additional points from a “Bonus Points” item

  • Adding an item to increase the player’s health points

  • Adding a dangerous, instant destruction item that would destroy the player on contact

  • Adding an explosive bullet to shoot and explode after a delay, with range damage

  • Implementing SpawnRate and EnemySpeed variables from Plane Dodger for difficulty ramp

Summary

In this chapter, you created the game Airplane Assault. You used the Timer behavior to implement time-based, repeated enemy shooting with a random interval. To give the player multiple chances to battle, you learned how to add health points and how to use the Flash behavior to add invincibility. In the “Side Quests” section, you added endless vertical scrolling to the background for a more immersive experience. The “Side Quests” section also discussed other additions including enemy health points, analog gamepad support, items, and a difficulty ramp.

In the next chapter, you will continue fighting a stream of enemies, this time via ground-based turrets, as you create the game Tower Defenders.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset