May 27, 2017
146 Views
3 0

Make a 2D Science Video Game with Construct3 (Part 2 of 3: Adding Enemies and Events)

Written by
Let’s continue our White Blood Cells Attack! tutorial and add other elements of the game. Here are the links if you need to revisit part 1 or jump to part 3. Also, if you want to play the finished product just go here. In part one we finished the background,inserted our player, and the controls for our player. Now we will develop other parts of our game.

 

Creating Enemies and other Objects

 

13. Now lets add some enemy bacteria to fight against! Double click on the middle layout window and insert a “Sprite” object. Insert the new Sprite object anywhere on the layout and then load the bacteria image below (Make sure you download it first and save it to your computer). Close the Animations Editor window and you should now have a bacteria enemy like this.

 

While you’re on the Bacteria object, let’s add a behavior to it. Let’s say they will move around randomly so click on the bacteria object, then just like we did before, go to Properties >> Behaviors >> Add new behaviors >> Sine. Now the bacteria moves back-and-forth. You can adjust this as you wish in the Properties bar. In fact, I added a second Sine behavior so that my bacteria moves in vertical and horizontal planes. Here is a snapshot of my settings in case you are interested.

 

14. Create more bacteria –  Holding control, click and drag the Bacteria object. You’ll notice it spawns another instance. This is simply another object of the Bacteria object type.

 

15. Explosions – For an effect when bacteria are destroyed in the game, let’s add an explosion sprite that we will use later. Download the image below and once you’ve inserted it, move it off of the background image to the gray margin.  Add the Fade behavior to the Explosion object (so it gradually disappears after appearing). By default the Fade behavior also destroys the object after it has faded out, which also saves us having to worry about invisible Explosion objects clogging up the game.

 

Creating Events: The Inner Mechanics of the Game

 

16. First, click the Event sheet 1 tab at the top to switch to the Event sheet editor. A list of events is called an Event sheet, and you can have different event sheets for different parts of your game, or for organisation. Event sheets can also “include” other event sheets, allowing you to reuse events on multiple levels for example, but we won’t need that right now. More details can be found here if you’re interested. 

 

In short, an event basically runs like this:Are all conditions met?
—> Yes: run all the event’s actions.
—> No: go to next event (not including any sub-events).

 

    In general, making events can be described by these steps:

  1. Double-click to insert a new event, or click an Add action link to add an action.
  2. Double-click the object the condition/action is in.
  3. Double-click the condition/action you want.
  4. Enter parameters, if any are needed.
Our first event will be that when the player collides with a bacteria, the bacteria is destroyed. To do this, first click on “Add event”. Select the “Player” as the focus of the condition.

 

Next, we want the On collision with another object condition.

 

Next, choose the Bacteria object to be the object with which to test a collision.

 

Now you will need to add an action that happens if the condition is met. So click on the “Add action” words in your event and choose the Player. Scroll down to select Spawn another object.

 

The Object you will spawn is the Explosion, it will be in Layer 1.

 

You also want the bacteria to be destroyed when your player collides with it. So add another action to the same event. Choose the Bacteria as your focus for the action. Then scroll down to select Destroy.

 

Your event should look like this.

 

Miscellaneous Details

 

17. Adjusting colliders – If you play the game you should notice that the collisions are a little premature and the explosion looks weird. Go to the Project panel and right-click the Bacteria. Choose Edit Animations. Then right-click on the image of the bacteria and select Guess the polygon shape. This will give you better bounds for the shape of your object and allow you to adjust them as you like. Mine looks like this.

 

Close the Animation Editor and do the same procedure for the Player object. Mine looked like this.

 

Close the Animation Editor and try playing the game. Better, right?

 

18. The Explosion effect – The explosion still has that weird black background. Let’s get rid of that.Click the Explosion object in either the Object bar in the bottom right, or the Project bar (which was tabbed with the layers bar). Its properties appear in the properties bar on the left. At the bottom, set its Blend mode property to Additive. Now try the game again.

 

Why does this work? Without going in to the nuts and bolts, ordinary images are pasted on top of the screen. With the additive effect, each pixel is instead added (as in, summed) with the background pixel behind it. Black is a zero pixel value, so nothing gets added – you don’t see the black background. Brighter colors add more, so appear more strongly. It’s great for explosions and lighting effects.

 

19. Spawning more enemies – Once you kill bacteria you’ll want more to kill! So let’s go back to the Events Sheet to do this. Choose Add event >> System >> Every X seconds. Let’s say a new bacteria is going to be created every 3 seconds. Now go to Add action >> System >> Create object. Choose Bacteria as your “Object to create”, Layer = 1, X = random(3840), Y = random(2160). These values allow bacteria to be randomly spawned anywhere in our layout. Your event should look like this:

 

20. Keeping Score –  Let’s have a score so the player knows how well they’ve done. We can use a global variable. A global variable (or just “global”) can store text or a number. Each variable can store a single number or a single piece of text. Global variables are also available to the entire game across all layouts – convenient if we were to add other levels.

 

Right-click the space at the bottom of the event sheet, and select Add global variable.

 

Enter Score as the name. The other field defaults are OK, it’ll make it a number starting at 0.
You should see this at the top of your Event sheet if you did it correctly.

 

Let’s give the player a point for killing a bacteria. In our “On collision with Bacteria” event, click Add action, and select System >> Add to (under Global & local variables) >> Score, value 1. Now the event should look like this.

 

Great! We’ve created enemies, explosions, and interactions between our player and game objects. We’ve also fine-tuned a few of the details to make the game run more smoothly. In part 3, you’ll create a heads-up display (HUD) to show the score as well as a timer to give motivation to the player.

 

Article Categories:
Game Design · Game Development · Tutorials


Leave a Comment

Your email address will not be published. Required fields are marked *