Skip to content

⬅️ Previous part: 10. More lives ➡️ Next part: 12. Main menu

11. Game over

With the Monster having lives, collision detection in place, and bombs falling down along the shapes, we get to a point where the player can lose all the lives, and what comes then? Game over!

Let's add a new object, which will again be a sprite.

Call it "GameOver", add the respective image, and save it.

Drag it from the objects panel onto the scene.

Add another sprite, which will be our button to try playing the game again. Name it "ButtonTryAgain", and set the animation name to "TryAgainNormal" for the default state. Import the image.

Add the second animation called "TryAgainHover" which will be used to indicate user has hovered the mouse cursor over the button.

The third and the last animation is "TryAgainPressed", apply the changes.

Drag the button onto the scene.

Repeat this for the "ButtonMainMenu" that will take the player to the main menu we're gonna create soon as the separate scene.

After "MainMenuNormal", the second animation name is "MainMenuHover"...

...and the third one "MainMenuPressed", apply the changes.

Drag the main menu button onto the scene.

Double click on the Monster, and add a second animation for him.

"MonsterDead" will be used when the Monster will run out of all the available lives.

Create the "Health" comment in "Level1 (Events)".

Then add a new condition.

"Monster", then "Is just damaged", and "Ok".

Note

This condition "Is just damaged" is appearing there as we previously added the "Health" behavior to the object.

Then "Life", "Change the animation (by name)", and enter "Life" +, and then click on the "ABC" button on the right. Chose "Conversion", and then "Number > Text" (this is because we want to transform the health of the player, which is a number, to a text that will be used to choose the animation to display).

In the popup, click "123" button...

...and then chose "Health".

After that you'll be asked to enter object's name and the behavior (that is automatically filled for you after selecting the object).

Go for "Monster". "Health" will be automatically written in the second field. Click Apply.

Note

It's possible to have multiple behaviors of the same kind on an object! For example, a huge spaceship in a space shooter game might have multiple Health behaviors, corresponding to different parts of the spaceship. In general though, a single behavior is enough!

Then you'll be presented with the Monster.Health::Health() function which will return the health variable - this expression will be converted to text.

Note

Confused by the notation Monster.Health::Health()? The first Health is the name of the behavior, while the second Health correspond to the function to be called on the behavior.

The final value of the "Animation name" will be "Life" + ToString(Monster.Health::Health()).

Note

The ToString function is important, as it's converting the health from a number to a text. Otherwise, GDevelop will try to add a text and a number, which does not make sense.

Back to "Monster", look for the "Flash (blink)" action, and then set the duration of the blinking to 1.5 seconds, and "Ok".

Under the "Health" comment, add another condition.

Choose "Monster", then "Is dead", "Ok".

Note

This is again a condition provided by the "Health" behavior.

The action for this: "Life", "Change the animation (by name)", and then "Life0", so when the Monster dies, the lives image will show no more available hearts.

Another action: "Monster", then "Change the animation (by name)", and "MonsterDead". This will show the different image for the Monster when he runs out of lives.

The next action we need is to remove the "Shapes" group by using "Delete an object" on it...

...and doing the same with the "Obstacle".

Next up, we pick the "GameOver" sprite and "Show" it on the screen.

Do the same with the "ButtonTryAgain"...

...and the "ButtonMainMenu".

All those sprites will be showed on game over, but we need to hide them at the beginning of the game.

Create a new "Game Over" comment, and add a new condition.

Search for the "At the beginning of the scene".

Then make an action where "Hide" is applied to the "GameOver" object.

Do the same for the "ButtonTryAgain"...

...adn the "ButtonMainMenu".

This way, when you launch the game and lose all your lives, the hearts will be almost fully transparent, shapes and bombs will disappear, Monster's animation will change to the dead one, and the Game Over message will be showed along with the Home and Try Again buttons.

The gameplay itself is finished, but we'd like to add more to the game - it would be nice to introduce more scenes into the game by having a Main Menu at the beginning, and allow player to start the gameplay on their own.

⬅️ Previous part: 10. More lives ➡️ Next part: 12. Main menu