Dealing with the life and death of the player

As this is a game about survival, we will want the win/lose condition to be fairly simple. For the win condition, we will make the player survive for a set amount of time. The lose condition will be the player dying, but we don't want to come across as too hard to play, so we will give the player three lives. This means that we are going to need to respawn the player. Finally, to get this to work properly, we will need to give the Overlord some more duties.

Setting up the win condition

The win condition for this game is to survive for a set amount of time. We can achieve this through the use of an alarm and a variable to signal to the Overlord that the player has survived.

  1. We will need to set up some variables for the lives, win, and lose conditions. Reopen scr_Overlord_Create and add the following code at the bottom:
    lives = 3;
    isVictory = false;
    isDefeat = false;

    GameMaker: Studio has a few built-in global variables including lives. This variable is accessible by every instance in the game and never goes away. Here we have set it to 3 and will use that as our starting point. We also create two other variables, isVictory and isDefeat, which we have set to false. The reason we are using two variables to represent winning and losing the game instead of one is that we will want to check these during gameplay when they have neither won nor lost.

  2. We can also set our win condition in this script by setting an alarm for 90 seconds. To do this add the following line of code after the code in step 1:
    alarm[0] = 2700;

    The scr_Overlord_Create script should now look like the following in total:

    timeline_index = tm_Wave_Spawning;
    timeline_running = true;
    timeline_loop = true;
    
    lives = 3;
    isVictory = false;
    isDefeat = false;
    
    alarm[0] = 2700;
    
  3. Next, we have to set up a script for the alarm event for the victory condition. Create a new Script, name it scr_Overlord_Victory, and write the following code:
    timeline_running = false;
    with ( obj_Enemy_Parent )
    {
        instance_destroy();
    }
    alarm[1] = 90; 
    isVictory = true;

    The very first thing we do is stop the Time Line, as we don't want any more enemies to spawn. The next step is to remove all enemies still alive in the game. We do this by using a with statement that is going to execute code to all instances of obj_Enemy_Parent. As all the enemies are children of this object, they too will be destroyed. We will eventually want to restart the game, so we set another alarm for three seconds. Finally, we set the isVictory variable to true.

  4. In obj_Overlord, add an Alarm 0 event and apply the victory script.
  5. Let's wrap this up by creating the restart script. Create a new Script, name it scr_Overlord_GameRestart and write this code:
    game_restart();
  6. Add an Alarm 1 event and apply this restart script. The win condition is now working, so feel free to try it out.

Respawning with a Ghost object

We can now move on to the losing condition and respawning. When the player dies, we don't want to have the player respawn immediately, but instead have a smaller buffer of invulnerability. To do this, we will want to create a Ghost object that will temporarily stand in for the player.

  1. Create a new Sprite, name it spr_Ghost, and load Chapter 3/Sprites/Ghost.gif with Remove Background checked. It looks just like the plane, but is slightly transparent and flickers when animated.
  2. We need to set the origin to be exactly the same as the origin of spr_Player. Set Origin to X: 43, Y: 22, and then click on OK.
  3. Create a new Object, name it obj_Ghost, and apply spr_Ghost as the Sprite.
  4. When the player dies, we are going to have the Ghost appear offscreen to the left and then move into the gameplay area. Create a new Script, name it scr_Ghost_Create, and write the following code:
    x = -64;
    y = room_height * 0.5;
    hspeed = 4;

    We start by setting the x coordinate to be offscreen by 64 pixels. We then center the Ghost vertically by setting the y coordinate to half of the room height. Finally, we are applying a positive velocity to the Ghost, so that it starts moving on its own.

  5. Add a Create event to obj_Ghost and apply this script.
  6. The Ghost is going to move on the screen, and we will need to change it into the player at some point. In our case, we will make the switch once the Ghost has passed quarter way into the gameplay area. Create a new script, name it scr_Ghost_Step, and write the following code:
    if ( x >= 200 )
    {
        hspeed = 0;
        instance_change(obj_Player, true);
    }

    Here we check to see if the Ghost's x coordinate has crossed 200 pixels or not. If it has, we stop the forward velocity and then we transform into the player. The instance_change function asks for two arguments: what object to transform into and whether we want to run the Create event of this new object.

  7. Add a Step event to obj_Ghost and apply this script.
  8. One issue we will encounter with this setup is that the player has no control of the Ghost, and could end up in a dangerous position near an enemy when they transform. We don't want that, so let's give the player some limited controls. We can reuse the existing scr_Player_Key_Up and scr_Player_Key_Down scripts, so that the player has vertical movement. Add the appropriate Keyboard events and attach these scripts.

    The Ghost Object's properties should look like the following screenshot and is now ready to become a part of the game. We just need to change what happens when the player is hit.

    Respawning with a Ghost object
  9. Reopen scr_Enemy_Collision_Player.
  10. Currently, we are destroying both the bullet and the player. We need to change the with statement to allow for respawning. Remove line 3:
    instance_destroy();

    And replace it with:

    if ( lives > 0 )
    {
        instance_change(obj_Ghost, true);
    }
    else
    {
        instance_destroy();
    }
    lives -= 1;

    We only want to become a Ghost if we have lives available, so we start by checking that. If we do have at least one life, we transform the Player into a Ghost. Otherwise, we just destroy the Player and the Player will be permanently dead. Finally, we subtract a life every time, whether we have lives or not. The final code should look like the following:

    with ( other ) 
    {
        if ( lives > 0 )
        {
            instance_change(obj_Ghost, true);
        }
        else
        {
            instance_destroy();
        }
        lives -= 1;
    }
    instance_destroy();

    At this point we can play the game. Notice that when the player dies:

    • The player disappears
    • A Ghost is created and moves into the play area
    • The Ghost can move up and down
    • The Ghost turns back into the Player

    This of course happens three times and then the player disappears forever. The rest of the game, however, is continuing on as if nothing happened. We need to add in the defeat condition.

  11. Create a new Script, scr_Overlord_Step, and write the following code:
    if ( lives < 0 && isDefeat == false ) {
        alarm[1] = 90;    
        isDefeat = true;
    }

    Every step in this code will check to see if the player has any lives left. If the player has no lives left and the variable isDefeat is still false, it will set the Restart Game alarm for three seconds. Lastly, we set the isDefeat variable to true, so that we don't run this code again.

  12. In obj_Overlord, add a Step event and apply this script. The game will now restart after the player dies three times.

The core mechanics of the game are now complete, but it's not very clear to the player as to what is going on. The player can die and respawn a few times, but there is no indication of how many lives are left. Nor is there any information being displayed on whether the player has won or lost the game. Let's fix this!

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

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