Game Maker Tutorial

Page 8 of 13
Enemies and Weaponry
We need to define two important collision events for the enemy plane: the collision event with the bullet, which should destroy the enemy plane, and the collision event with the main plane which should destroy the main plane and end the game.
We start with the collision event with the bullet. A number of actions are required here. But first of all we need a sound for a small explosion and a sprite that indicates the explosion. To create a sound, press Add sound and load some nice explosion sound (see the Sounds folder in Game Maker's directory for some example sounds). For the explosion we need a little sprite. Like for the enemy plane we set the origin to (16,16). We also create an explosion object and give it the explosion sprite. It does nothing except that once the complete explosion animation has played it must destroy itself. There is an event called Animation end for this under the other events.
When the sound and the explosion object are ready we can fill in the collision event for the enemy plane with the bullet. The following actions are required. First of all we play the explosion sound. Next we destroy the bullet. For this use the action to destroy an instance but, at the top, indicate that it should apply to the Other instance, which is the bullet in this case.
Next we create the explosion object, relative at position (0,0), that is, at the same place as the enemy plane. We do not destroy the enemy plane! Instead we move it to a random position above the room such that it looks as if a new enemy plane is coming. Finally, we set the score relative to 5. We need relative here because we want to add 5 points to the score; we do not want to set the score to 5. The evnt should look like this:
Next we need to handle the collision with the main plane. Again we will need an explosion sprite (a bit bigger this time) and an exploding sound (a bit louder this time). We add the sound to the game and we make a sprite for the explosion. Again we create an explosion object for it, but this object is a bit more elaborate than the other explosion object because it will also handle the ending of the game.
In its Animation end event we do a few things. We first destroy the instance to make it invisible. Next we sleep a while to make sure that the exploding sound is finished. The next action shows the high-score list, such that the player can type his or her name when reaching a high score. This goes all automatic in Game Maker. You can though indicate what the appearance should be of the high-score table. You can have a nice background image, choose the font, etc. You should experiment a bit with this. Finally we restart the game.
In the collision event of the enemy plane and the main plane, we change the main plane, that is, the other instance, into an explosion. Also we play the exploding sound and we destroy the enemy plane. What remains is to place an enemy plane in the room but we will do this slightly different. A good game becomes more difficult all the time. So we like to start with just one enemy plane and get more of them over time. To this end we create one more object, which we will call controller_enemy. It will control the creation of enemy planes. We make the object invisible during the game by unchecking the box labeled Visible. A sprite is not required for it.
In its creation event we create an enemy place at a random location just above the room. This will be the first enemy. We also set the alarm clock to 200. In the event for this alarm clock we create another enemy plane and set the alarm clock again, but this time to 500. That is all. The effect is that at the beginning of the game there is one enemy plane.
After 200 steps, that is, about seven seconds, a second enemy plane appears. After about 15 seconds, a third plane appears, etc. (The reason that we let the second plane appears faster than the other ones is because the game with just one enemy plane is too boring.) Place one instance of the controller object in the room and we are done.
This finishes the second version of 1945. We have a playable game with an enemy.