Controlling the game with the Overlord

In this game, we will be using the Overlord, the master controller of the game, to control the spawning of enemies, monitor the player's lives, and deal with the win/lose condition. The win condition will simply be to survive for two minutes against waves of enemies. The lose condition will be that the player runs out of lives.

Spawning waves of enemies

We need to start by creating the wave of enemies, so that the game is playable. For this, we will utilize a looping timeline to spawn the various enemies. We are going to have three different waves, each one spawning a different enemy every two seconds.

  1. Create three new scripts, and name them: scr_Wave_Strafer, scr_Wave_SpaceMine, and scr_Wave_FloatBot.
  2. We will start with the wave of Strafer, as it will be the simplest wave. Write the following code in scr_Wave_Strafer:
    instance_create(room_width - 64, room_height/2 - 64, obj_Strafer);
    instance_create(room_width - 64, room_height/2 + 64, obj_Strafer);

    Here we spawn two instances of the Strafer located 64 pixels off the right-hand side of the screen. This will ensure that we don't see them pop into existence. We have also offset them by 64 pixels from the vertical center of the room.

  3. For the SpaceMine, we will want them vertically placed in random positions to keep things interesting. Write the following code in scr_Wave_SpaceMine:
    placeY = irandom_range(64, room_height - 64);
    instance_create(room_width - 64, placeY, obj_SpaceMine);

    We are creating a variable called placeY to hold a value for the vertical position. GameMaker: Studio has a special function, irandom_range, which will return a whole number between any two numbers passed to it. The numbers we used will ensure that the SpaceMine will remain at least 64 pixels away from the top and bottom of the screen. We then use the placeY variable when we create the instance.

  4. The FloatBot is going to use a similar setup for placement on the vertical axis, but we will want three instances flying in a "V" formation. Write the following code in scr_Wave_FloatBot:
    placeY = irandom_range(80, room_height - 80);
    instance_create(room_width - 32, placeY, obj_FloatBot);
    instance_create(room_width - 64,placeY - 32, obj_FloatBot);
    instance_create(room_width - 64, placeY + 32, obj_FloatBot);

    Here we are using the placeY variable again, but the range of numbers is narrower. We need some extra padding, so that all three planes stay onscreen. The first instance created is the front unit of the formation. The next two instances spawn 32 pixels behind and offset 32 pixels above and below the first.

  5. All the waves are scripted, so we can now implement them in a Time Line. When first implementing a Time Line, it is useful to keep the numbers simple, such as two seconds apart. Properly balancing the timing comes during the polish phase of a game's development, and spending too much time trying to get this right before all content is in is most likely wasted time. Create a new Time Line and name it tm_Wave_Spawning.
  6. Click on Add, set Indicate the Moment as 60, and apply the scr_Wave_FloatBot script. This will spawn the first enemy into the game for two seconds.
  7. We will want to add the SpaceMines two seconds later. Click on Add, set Indicate the Moment as 120, and apply the scr_Wave_SpaceMine script.
  8. Finally we bring in the Strafer after six seconds. Click on Add, set Indicate the Moment as 180, and apply the scr_Wave_Strafer script. The Time Line is now ready to be used and should look like the following screenshot:
    Spawning waves of enemies

Building the Overlord

We are now ready to start building the Overlord and apply our spawning system.

  1. Create a new Object and name it obj_Overlord.
  2. There is no Sprite needed, so set Sprite to no sprite.
  3. We will start the Time Line immediately upon creation of the Overlord. Create a new Script, name it scr_Overlord_Create, and write the following code:
    timeline_index = tm_Wave_Spawning;
    timeline_running = true;
    timeline_loop = true;

    The first line of code defines what Time Line we want to run, in our case we only have one: tm_Wave_Spawning. Next, we start the Time Line and then tell it to loop. These last two are Boolean variables, which means that they can only be turned on and off.

  4. In the Overlord, add a Create event and apply this script.
  5. Open TheGame and place a single instance of the Overlord anywhere in the room. The location does not matter, but the upper-left corner is a common place to put it.
  6. Remove any instances of the enemies that remain in the room. There should be only one instance of the Player and one of the Overlord as can be seen in the following screenshot:
    Building the Overlord
  7. Run the game.
    Building the Overlord

The game now has enemies! It will take a couple of seconds for the first enemy, the FloatBots, to appear, but after that the enemies will continue to spawn forever. At this point we have most of the core gameplay implemented as follows:

  • We can move the player around the screen, but not out of it
  • We can shoot and destroy enemies
  • Enemies can shoot and destroy the player
  • Enemies will spawn continuously

The only element remaining is very obvious when playing the game at this stage; the player can die, but the game doesn't stop. We need to implement the win/lose condition.

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

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