About 'Adding Lives'!

In this video (objectives)…

1 Create a starting_lives variable and a lives variable 2 Update lives when Bunny is hurt 3 Make bunny jump up when hurt

After watching (learning outcomes)…

Add lives to our game!

(Unique Video Reference: 15_HD_GDT)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Do you plan to deal with the top_spikes, the ones that hang down, in a later lecture or should we create code to handle them in this lecture? Obviously we would need to handle the jump code differently as it wouldn’t make much sense for the player to react by jumping up if the hazard is above them.

If we do this do you think it is better to have a seperate script for each hazard or a common hazard script that handles exceptions according to the hazard type? If the first there would be more flexibility for handling the hazard but possibly code duplication. If the latter there is the risk that the code becomes monolithic with modifications each time you create a new hazard type, with the possibility of breaking the code for previous hazards when you add a new hazard type.

As top_spikes is an instance of bottom_spikes, they use the same code. Because the spike only hurts the player as Bunny enters it, it didn’t feel like there was any reason to change it yet. However, I did rename the script to hazards for that very reason!

I’m not too worried about the code becoming monolithic, as with one exception (the cloud npc) they’re all going to act like the spikes hazard with movement being handled by the animation player. In a larger project, or one where enemies have more elaborate behaviours, I’d probably organise things differently.

3 Likes

Question and Answer:
moving sideways into the spikes (walk) doesn’t always cause the bunny to jump, but it does reduce the lives.
can be fixed in Player.gd with
func fall(delta):
if motion.y > 0 and (is_on_floor() or is_on_ceiling()):

5 Likes

A check to see if the bunny has hit a ceiling will also work to stop the upward movement if you hit a downward spike. That will only work if there is something about the spike though.

func fall(delta):
	if is_on_floor():
	    motion.y = 0
	elif is_on_ceiling():
	    motion.y = GRAVITY * delta
	else:
	    motion.y += GRAVITY * delta
	
	if position.y > world_limit:
		Global.GameState.endGame()

To deal with ceiling spikes I’ve made it so that the player jumps away from the hazard with a speed of 1500.
I’ve modified it so that when the hazard detects a body collision, it will check if the body has “hurt” function and if it does, it will call it passing over itself (the hazard).

Hazards.gd

func _on_body_entered(body):
	if body.has_method("hurt"):
		body.hurt(self)

Then in the player’s hurt function we can use the hazard’s position to determine the direction we must jump (away from the hazard).

Player.gd

func hurt(hazard):
	var dir = Vector2(position.x - hazard.position.x, position.y - hazard.position.y).normalized()
	motion = 1500 * dir
	Global.GameState.hurt()

Then I call the GameState’s hurt function so that lives will be updated and the game ends if there are no more lives.

GameState.gd

func hurt():
	lives -= 1
	if lives < 0:
		end_game()

I think this order makes more sense and this makes it easier if in the future we want other entities to be hurt by hazards, without affecting player lives or game state.

EDIT:
I’ve almost forgot. For this to work nicely I had to move the origin for the spikes:

  • top spikes have origin bellow the bottom margin
  • bottom spikes have origin above top margin
1 Like

How come my bunny doesn’t jump every time even though hurt() is definitely executing. I pass a message to hurt and it prints every time, but the jump doesn’t always happen.

func hurt(mess): # make the bunny jump if hurt
motion.y = JUMPSPEED
print(mess)

I see there are a bunch of alternative fixes above, but seems odd how we’re not shown a reliable example in the tutorial.

Is it to do with physics? If bunny jumps onto it, he is coming down @5000 (Gravity) and then being told to go back up at -2500. Some kind of cancelling going on.

Realise I am rambling, but don’t just want to know how to ‘bodge’ a fix, I want to understand why it is happening in the first place.

Cheers

Privacy & Terms