The third part of this challenge series finalizes the project you created in the first two parts. In this part we’ll implement a score, lives and do some polishing. The final version will look like this:
Let’s start with the greeting, because that’s also one of the first things in our game:
- After
Game.ShowSceneAndAddTurtle();
greet the user, wait until the space key is pressed, then make the turtle shut up.
Your fingers should be warmed up by now, so we continue with the score:
- Before the endless loop, define a new variable
score
and initialize it with0
. - Inside the loop, tell the user her score. This must be done for every loop iteration because the score is going to change.
- Before saying the score, but after moving the turtle as well as the food, check if the turtle and the food touch each other.
While you could find that out using both coordinates and sizes,
Turtle.TouchesPlayer
might be handier. - If so, increase the score by 1 because that means that the turtle caught the food and move the food to a random position at the top.
The last part is the lives, which is quite similar to the score:
- Define a variable
lives
and initialize it with3
. - Tell the user her lives additionally to the score.
- Decrease
lives
by one if the food hits the bottom (we already check that). - Change the endless loop so that it continues only if there are lives left.
After the loop - when the game is over because there are no lives left - tell the user her score.
Now reward yourself by playing your first self-written C# game. Congratulations!