Hero's Trail: Player Abilities & Power Ups


Hero's Trail: Player Abilities & Power Ups

Overview

This tutorial expands on the Hero’s Trail project provided with GameMaker. We’ll give the player the ability to attack in all four directions and use a shield, as well as add heart pickups and speed/invincibility power ups!

undefined

This tutorial has the following sections:

  1. Attack in 4 Directions

  2. Heart Pickup

  3. Speed Power Up

  4. Invincibility/Star Power Up

    1. Becoming Invincible

  5. Energy Shield

    1. Depleting Energy

    2. Recharging Energy

    3. Drawing the Energy Meter

Attack in 4 Directions

Currently the player can only attack left or right, so we’ll now allow it to attack in all 4 directions. For this we’ll create a variable to store the direction that the player is facing in.

Let’s do the following:

  1. Go to obj_player and open its Create event.

  2. Add the “Assign Variable” action and create a variable called input_direction.

undefined

This variable can have any one of the following values indicating the input direction:

  • 0: Right

  • 90: Up

  • 180: Left

  • 270: Down

This is how the player will know where it's facing, and will be able to rotate the sword to face in the same direction!

Setting the Input Direction

This variable will be set in each arrow key event, so the following events will need the actions shown next to them:

Add the following action to the Key Down - Left event:

undefined

Add the following action to the Key Down - Up event:

undefined

Add the following action to the Key Down - Right event:

undefined

Add the following action to the Key Down - Down event:

undefined

Now when the player presses an arrow key, the input direction will be set accordingly. We can use this value to swing our sword in the correct direction!

Sword Rotation

When the sword is created, we’ll change its rotation so it uses the input_direction value as its image angle. Let’s do the following:

  1. Open obj_player’s Key Press - Space event.

  2. This event will already have an “Assign Variable” action which changes the image_xscale of the sword. Remove this action.

    undefined

  3. Add a new “Assign Variable” action. Make sure to place it after the “Create Instance” action.

  4. We’ll use this action to change the image_angle of the sword, which stores the rotation angle of its sprite.

  5. Set its value to input_direction so the sword faces in the direction stored in that variable.

undefined

Run the game now and you will be able to attack in all four directions. We just made the game a lot more fun and easier!

undefined

Refer to this tutorial for adding bat enemies.

Heart Pickup

The player can lose hearts, so we need to resupply them with heart pickups throughout the level so they can keep fighting.

Follow this tutorial to add a hearts system to your game if you don't already have one.

Let’s start by creating a heart pickup object:undefined

  1. Go to "Objects" -> "Game".


  2. Here, create a new object called obj_heart_pickup.


  3. Assign the spr_heart_pickup sprite to it, which can be found in the “Items” group under “Sprites”.




You can now go ahead and place these hearts in the room, inside the “Instances” layer.

undefined

Collecting Hearts

We’ll now make the player able to collect these hearts by colliding with them.

How Will This Work?

The player will use a collision event to check if it has collided with a heart pickup.

In that case, it will then check if the player has fewer hearts than it should.

If that is true, the player's hearts will go up by 1.

The colliding heart pickup will then be destroyed.

Let’s do the following:

  1. Open obj_player and add a collision event with the new obj_heart_pickup object.

  2. Add the “If Variable” action in this event. This condition should check if the hearts variable is less than max_hearts, meaning that the player is missing some hearts.

  3. Add an “Assign Variable” action and attach it to the condition. Use this to increase the value of the hearts variable by 1.

  4. Attach an “Apply To” action, click on the little arrow next to the X button and select “Other”.

    undefined

  5. Any actions attached to this event will now be applied to the “other” instance in the collision, which is the heart pickup (as the player is the “self”).

  6. Attach the “Destroy Instance” and “Do Effect” actions to “Apply To” so the heart is destroyed with a particle effect.

Use the following settings for your actions:

undefined

You can now run the game and pick up hearts! You’ll notice that you can’t collect them when your hearts are full. This makes sure that any excess heart pickups are saved for later.

undefined


Speed Power Up

undefined

We’ll now create a power up that makes the player super fast. Let’s start by setting up the power up object: undefined

  1. Create a new object called obj_powerup_speed.

  2. Assign the spr_powerup_speed sprite to it, found in the “Items” folder under “Sprites”.


Now go ahead and place this power up anywhere in your room. For now try to place it near the player’s starting position so you can test it quickly.

undefined


Power Up System

We’ll create a power up system in the player object, so it can equip powers that last for a certain amount of seconds (for as long as you like!).

Let’s do the following to create our power up system:

  1. Go into obj_player and open the Create event.

  2. Use the “Assign Variable” action to create a variable called powerup_active with a value of false.

  3. Create another variable called default_move_speed and set its value to move_speed.

undefined

Collecting a Power Up

The player should be able to collect a power up and have its properties changed based on the power up.

How Will This Work?

When the player collides with a power up, some of its properties will change.

In case of the speed power up, we'll simply increase its movement speed and animation speed.

We'll then use an Alarm event to disable the power up and reset these properties back to their default values.

Let’s implement the effects of the speed power up by doing the following:

  1. In the player object, add a collision event with obj_powerup_speed.

  2. We should only be able to collect this power up if the player does not already have one, so we’ll add an “If Variable” condition to make sure that powerup_active is equal to false.

  3. Now we’re going to attach some actions to this condition:

    1. Assign Variable” - Set powerup_active to true as a power up is now active.

    2. Assign Variable” - Set the move_speed to twice the default_move_speed, so the player moves twice as fast.

    3. Set Animation Speed” - Double the animation speed too as the player is now moving faster

    4. Set Alarm Countdown” - Tell Alarm 1 to run after a few seconds. Alarm 1 will reset the effects of the active power up.

undefined

Tip: You can resize an action box by dragging it from its sides. This makes it easier to see fields that are too long.

Since we’re setting Alarm 1 to 300, it means our speed effect will last for 5 seconds.

We also need to destroy the speed power up instance, so let’s do the following:

  1. In the same chain of actions, add the “Destroy Instance” and “Do Effect” actions.

  2. Apply them both to the “Other” instance, which in this case is the speed power up:

    undefined

Use the following settings for the particle effect:

undefined

Disabling a Power Up

Picking up the speed power up will tell Alarm 1 to run after 300 steps. That event should then disable the power up, so let’s implement that:

  1. Add the Alarm 1 event to obj_player.

    undefined

  2. Use the “Assign Variable” action to reset the powerup_active variable to false.

  3. Use another “Assign Variable” action to reset the move_speed back to the default_move_speed.

  4. Then reset the animation speed to 1.

undefined

This will now handle disabling the power up a few seconds after it has been collected.

You can now run the game, collect the power up and run around faster than ever before!

undefined

The power up works well, however we can make it better by adding a “trailing frame” behind the player.

Trailing Frame

While the speed power up is active, we’ll draw a transparent image of the player one frame behind it so it looks like the player is going fast.

We’ll use the Draw event for this, which allows us to draw something over the instance. Let’s begin by adding the event:

  1. Add the Draw event to obj_player.

  2. Use “Draw Self” to draw the player instance.

  3. Use “If Variable” to check if the player’s current move speed is greater than the default move speed. This means that the player is using a power up to speed itself up.

  4. If that is true, we want to draw the player sprite with extended options so we can control its scale and alpha. Add the “Draw Sprite Transformed” action and attach it to the condition.

Use the following settings for the new actions:

undefined

For the “Sprite” and “Frame” options we’re simply passing in the sprite_index and the image_index, which store the current sprite and frame of the instance respectively.

Using these options, the player’s sprite will be drawn at its previous position, so it will appear “behind” the player.

Now run the game, collect the speed power up and you will see the trailing frame behind the player, giving a nice “boost” effect!

undefined

Invincibility/Star Power Up

We’ll now add an invincibility power up that makes the player able to defeat any enemy with just a single touch. This pickup will be useful as a reward for the player discovering a hidden area!

undefined

Let’s start by adding the power up object: undefined

  1. Create a new object called obj_powerup_invinc.

  2. Assign the spr_powerup_invinc sprite to it, found in the “Items” folder under “Sprites”.

You can now go ahead and place this somewhere in the room.

Activating Invincibility

The star/invincibility power up will be activated in the same way as the speed power up: in a collision event. However before we do that, we need to create a new variable.

  1. Go to obj_player and open its Create event.

  2. Create a new variable called star_powerup_active and set it to false. This tells us whether the star/invincibility power up is currently active or not.

  3. We’ll later use this variable to immediately defeat enemies on contact.

undefined

Let’s now make the player able to collect a star power up:

  1. In the player object, add a collision event with obj_powerup_invinc.

    1. The contents of this event will be similar to the collision event with obj_powerup_speed.

  2. Add a condition ("If Variable") to make sure that no power up is active.

  3. In that case, we will set the powerup_active variable to true.

  4. We’ll also set star_powerup_active to true since this enables the star power up specifically.

  5. This power up will make the player fast too, but not as much as the speed power up. We’ll increase the player’s move speed and animation speed by 1.5x.

  6. We’ll also change the instance's colour to yellow to indicate that it’s invincible.

  7. Finally, we'll set Alarm 1 to 600 steps (so the power up is disabled after 10 seconds).

Here are all the new actions:

undefined

As previously mentioned, this power up will last 10 seconds. Of course, feel free to change this to your liking!

After these actions we also need to destroy the power up itself and create a particle effect; for this, we’ll use the same actions we used for the speed power up (make sure to apply them both to “Other”). Also make sure to attach them to the main condition.

undefined

Disabling Invincibility

We used Alarm 1 to disable the speed power up, and we’ll use the same event to disable the star power up too:

  1. Open the Alarm 1 event in obj_player.

  2. This event already resets the player’s speed back to normal, so we don’t need to worry about that.

  3. We only need to reset the star power up variable and the player's colour.

  4. Add "Assign Variable" and set the star_powerup_active variable to false, as the star power up should not be active anymore.

  5. Add a “Set Instance Colour” action to reset the player’s colour to white.

undefined

You can now run the game, pick up a star and roam around for some seconds with a changed colour, however it doesn’t make you invincible yet. Let’s work on that!

Becoming Invincible

While the star power up is active, the player should be able to defeat all enemies it touches. We’ll achieve that by creating an invisible sword instance when the player touches an enemy, which will defeat the enemy without the user seeing the sword at all!

Let’s do the following:

  1. Go to obj_player and open its collision event with obj_enemy_parent.

  2. At the top of the event, add a condition to check if star_powerup_active is true.

  3. Attach an "Apply To" action to this condition and apply that to the other instance (which is the enemy).

  4. Attach a "Create Instance" action to "Apply To", and use it to create an instance of the obj_sword_attack object at the relative position of the instance. Store its ID in a temporary variable called sword.

  5. Make this sword invisible by setting its visible variable to false.

  6. After that, exit the event by adding the “Exit” action. This stops the event, making sure that the rest of the actions do not run and the player doesn’t get hurt!

    1. Place the "Exit" action below "Apply To", not inside it.

Here are the actions:

undefined

You can now run the game, pick up an invincibility power up and go on a rampage!

undefined

Energy Shield

We’ll now give the player an energy shield so they can protect themselves, and add a limited “energy” meter that drains while that shield is active.

undefined

Let’s start by creating the shield object -- this will be the actual shield that spawns on the player to protect them, not a pickup. undefined

  1. Under "Objects" -> "Game", create a new object called obj_energy_shield.

  2. Assign the spr_energy_shield sprite to it, which can be found in the “Player” group under “Sprites”.



We want this shield to always move with the player. Our player moves in the Step event, so we’ll use the End Step event to move the shield after the player has moved.

  1. Add the “End Step” event to the shield object.

  2. In this event, add the “Jump to Point” action. This action teleports the instance to the given point.

  3. Pass in the X and Y position of the player so that the shield moves to the player’s position.

    1. To pass in the position of the player, use obj_player.x and obj_player.y.

undefined

This makes sure that the shield always sticks to the player, by jumping to its position every frame.

Activating the Shield

Before the player can activate its shield, it needs to have a variable to store the shield energy. The shield can only be activated if this energy level is above 0, so let’s add it:

  1. Go to obj_player and open the Create event.

  2. Create a new variable called shield_energy and set its value to 100 -- this is the maximum shield energy the player can have.

undefined

We’ll now make the player able to activate the shield by pressing the Shift key. Here’s how we’ll do it:

  1. Go to obj_player and add the “Key Pressed - Shift” event. This event will run when the Shift key is hit (not held).

    undefined

  2. Use a condition to check if the shield energy is greater than 0.

    1. This is done by adding the "If Variable" action, using shield_energy as the "Variable", "Greater" as "Is" and 0 as the "Value".

  3. If it is, create an instance of the shield at the player’s position.

    1. This is done by attaching the "Create Instance" action to the condition and using obj_energy_shield as the "Object".

    2. Make sure to create it at the player's relative position, as shown in the image below.

undefined

This will now create a shield instance whenever the Shift key is hit!

Deactivating the Shield

When the Shift key is released, the shield should be destroyed. We’ll do the following to make that work:

  1. Add the “Key Up - Shift” event in the player object. This event runs when the Shift key is released.

    undefined

  2. Here we’ll destroy the shield instance, however we will first make sure that it exists in the room, as later we'll make it so it can be destroyed when the player runs out of energy.

  3. Add the “If Instance Exists” action and check if the obj_energy_shield object has any instances in the room.

  4. This is a condition, so you can attach instances to it. Add a “Destroy Instance” action and attach that to the condition.

  5. Apply this action to obj_energy_shield so the shield is destroyed. If you don't do this, the player instance will be destroyed instead!

    undefined

Here is what your event should look like:

undefined

You can now run the game, press Shift and the shield will appear. It also moves with the player so they're protected at all times; then release Shift and the shield will disappear!

undefined

We’ve got a basic shield system working, however it can’t block enemy attacks yet. Let’s work on that!

Shield Blocking

While the player’s shield is up, all enemy attacks should be ignored. Here’s how we’ll do that:

  1. Open obj_player’s collision event with obj_enemy_parent.

  2. At the top, add "If Instance Exists" to check if a shield exists in the room. This means that the player’s shield is active.

  3. If the condition is true, use "Exit" to exit the event.

undefined

Now, when an enemy collides with the player, it will check if a shield exists in the room, and in that case it will exit the collision event. This means the player will not be hurt.

You can now run the game, activate the shield, go near an enemy and see that it won’t hurt you at all!

undefined

Disabling the Sword

Now this does make the player a little too powerful, as you’re able to attack enemies while holding the shield up. This shouldn’t be allowed, so we’ll disable the player’s sword while the shield is up.

Let’s do the following:

  1. Open the “Key Press - Space” event in obj_player. This is the event that creates the sword.

  2. At the top of the event, add “If Instance Exists” to check if the shield exists.

  3. If it does, exit the event.

This is the exact same thing we did in the player's collision event with the enemy parent.
undefined

This will now stop the event if the shield is being used, preventing the player from attacking.

So we have added a mechanic (shield) that blocks two other mechanics:

  • Enemies attacking the player

  • The player using the sword

This is why conditions are powerful, as they allow you to change how your game functions depending on what the player has done, resulting in more interesting game mechanics!


Drawing the Energy Meter

We have a shield_energy variable in the player that stores the player's shield energy (up to 100). Let's draw this as part of the game's HUD so the player can see how much energy they have left!

  1. Go to obj_manager and open the Draw GUI event. This is where we draw the player hearts, so we’ll use it to draw the energy meter as well.

  2. Add the “Draw Healthbar” action to this event. As the name suggests, it’s used for drawing a “health” bar, but it can be used to display any value as a meter.

  3. Make sure this action is attached to the existing condition in the event.

  4. Attach a "Draw Sprite" action into the same chain, and use it to draw spr_hud_shield.

Use the following settings for drawing the energy meter:

undefined

The “Left” and “Top” properties define the top-left corner of the bar (the point where it starts), and the “Right” and “Bottom” properties define its bottom-right corner (where it ends). You can modify these values to move the bar.

The “Min Colour” is the bar’s colour when it’s empty, and the “Max Colour” is its colour when it’s full. The colour is then mixed based on the bar’s value - for example, at 50% energy the bar’s colour will be a mix of both colours.

You can now run the game and see the bar on the HUD!

undefined

Let's now make it go up and down depending on the player's usage of the shield.

Depleting Energy

The player’s shield energy should deplete while the shield is being used, so let’s implement that:

  1. Open obj_player and add the Step event.

  2. Here, add "If Instance Exists" to again check if the shield exists.

  3. If it does, reduce the shield_energy variable by 0.4 (by adding -0.4 to it). This is how much energy the shield uses up every frame.

  4. Inside the same chain, add a condition to check if the shield_energy is Less or Equal to 0.

  5. In that case, destroy the shield instance. Make sure to apply the “Destroy Instance” action to obj_energy_shield.

Your event should look like this:

undefined

The energy will now go down while the shield is out, and once it runs out, the shield will be destroyed. Try it out!

Recharging Energy

The energy should recharge after it has run out. We’ll do this in the same event (Step):

  1. Add an “Else” block and place it below the “If Instance Exists” action (at the root of the event).

    undefined

    This "Else" will run when the shield does not exist.

  2. Attach an “If Variable” condition to the “Else” action. Use this to check if the shield_energy is less than 100 (meaning it's not full).

  3. If that condition is true, increase shield_energy by 0.1.

undefined

Basically, when the shield is not activated and the energy meter is unfilled, it will fill up by 0.1 every frame until it reaches 100.

Try it out: see how the shield automatically disappears when you run out of energy, and how it starts filling again automatically!

undefined

You can also modify the depletion and recharge values of the shield energy in this event, customising the difficulty of your own game!

Summary

Our player is now more powerful than ever, with collectible power ups that give them an edge over all enemies and an energy shield that can be used for strategic combat.

In the next part, we’ll finish up our game and work on game essentials such as music, sound effects, pausing and a help window.

undefined

-> Continue to next part

-> Go back to index page