How to code a game where you have life's

#1

Hello!
I would like to create a game where you have three lives. And after the third life, you die and the game restarts. But I did not find any kind of instruction to this topic and I am not even sure if this is possible to do in cospaces. So, if you are able to do this can somebody please help me. Also, I am a beginner, so if possible I need a detailed and “easy” instruction how to code.
Thank you!

#2

Hi @Charly_book,

To keep track of lives you use a variable value that decreases when a condition is met. Below is step-by-step guide, but feel free to check out the CoBlocks Basic Tutorial, too :slight_smile:

https://forum.edu.cospaces.io/t/coblocks-the-basics/1203/14


Step-by-step guide

In this space we want our character to lose a live when colliding against a cactus

First, we create the cactus - items and add them to CoBlocks. Then we open the Code Editor and select CoBlocks

Let’s create a variable called myLives. We set its value to 3

Now we need to create an event handler that does something whenever the cactus collides with the camera (make sure to add the camera to CoBlocks, too!). In this case we change (or subtract) -1 from our myLives variable.

Finally, we add an if-statement to check when the myLives variable reached 0. If it does, we switch to another scene called “Restart”. In that “Restart” - scene, you can place a single block to move back to the starting scene.

Now we can test our code! Press Play and open the CoBlocks debugger to see your code in action!

That’s it! I added a remixable space below, if you want to check out how to continue from here :slight_smile:

#3

Thank you very much. I hope this will help me. :slight_smile: