Remember that before we can use any variable in Loop, we need to set its initial value in Start.
health = 5
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) destroy(self)
if collision_check(self, 'obj_asteroid'): asteroid = collision_check(self, 'obj_asteroid') destroy(asteroid) health = health - 1
That’s better. Now if our health ever gets to 0 or lower, our player will be destroyed.
if health ‹= 0: destroy(self)
sprite = sprite_new('spr_shield_pickup')
Test it out! You should now gain health and destroy the shield pickup whenever you collide with it.
if collision_check(self, 'obj_shield'): shield = collision_check(self, 'obj_shield') destroy(shield) health = health + 1
if health > 5: health = 5
shieldTimer = 0
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.
shieldTimer = shieldTimer + 1
It's almost exactly like our code for creating asteroids. The only difference is swapping out the word "asteroid" for "shield".
if shieldTimer == 240: shieldOffset = random.randint(-200, 200) shield = object_new('obj_shield') shield.x = self.x shield.y = self.y shieldTimer = 0
You can make it go faster or slower by changing that number to something bigger or smaller.
self.y = self.y - 1
Ready for more? Try the next lesson!