Welcome back students! Like the title suggests, today we are going to be adding health to our player object.
Right now, it’s pretty brutal. If we get hit just one time, we are destroyed. Let’s give ourselves a little more wiggle room than that.
That way this won't happen!
To start, go to your obj_player script / Start:
health = 5
Remember that before we can use any variable in Loop, we need to set its initial value in Start.
Now let’s look at some code in the Loop Tab. Remember this code?
if collision_check(self, 'obj_asteroid'):
asteroid = collision_check(self, 'obj_asteroid')
destroy(asteroid)
destroy(self)
Well let’s make a few changes. Instead of destroying ourselves completely, let’s just subtract 1 health!
if collision_check(self, 'obj_asteroid'):
asteroid = collision_check(self, 'obj_asteroid')
destroy(asteroid)
health = health - 1
Perfect! Now try getting hit by 5 asteroids and see what happens. Hmm… That’s weird, when we get hit by 5 we can still move around normally.
That’s because we didn’t set up any code to say we are destroyed when health is 0! Back in obj_player / Loop, make a new if statement:
if health ‹= 0:
destroy(self)
That’s better. Now if our health ever gets to 0 or lower, our player will be destroyed.
Now that we have a health value to play with, let’s add a new object which restores our health when we collect it!
Create a new object called obj_shield. Remember, just click on the "+" sign next to "Object Scripts" to create an object.
Then add the shield sprite to it!
In case you forgot, use this code to load the sprite. In obj_shield / Start:
sprite = sprite_new('spr_shield_pickup')
We now want to set up code similar to our collision check with the asteroid only the health should be increasing instead of decreasing.
Watch, it’s almost the exact same! In obj_player / Loop:
if collision_check(self, 'obj_shield'):
shield = collision_check(self, 'obj_shield')
destroy(shield)
health = health + 1
Test it out! You should now gain health and destroy the shield pickup whenever you collide with it.
We want to keep our player’s health capped at a maximum value. In my case, my maximum health is 5. However, currently if we are at full health, and pick up another shield, it will go above the max!
We can fix this with another simple if statement in obj_player / Loop:
if health > 5:
health = 5
This simple 2 lines of code means that if our health is ever more than whatever our maximum value is, we bring it down to the maximum to keep it from being imbalanced.
If you want your maximum health to be 10, or 20, or any number, just change both 5s for whatever you want the max to be.
Ok, we have one last issue with our health pickups. We can only place so many and then we run out. What if there was a way to make one appear every so often and come down towards us? Wait a minute… Our spawner object!
We can make our spawner object create shield objects just like they create asteroids!
Let’s make a new timer variable in obj_spawner / Start:
shieldTimer = 0
Now in obj_spawner / Loop:
shieldTimer = shieldTimer + 1
Just like the asteroidTimer, we need it to count up every frame, then we can make it create a new object every few seconds. We just need a new if statement which spawns a shield every few seconds instead of an asteroid.
Let’s make it every 4 seconds (so when shieldTimer equals 240). Feel free to make it shorter or longer. In obj_spawner / Loop: