Step 6¶
Let us round up this tutorial by adding in the UI for the Game Over state and adding a Timer for extra challenge.
We will be using a UI.Button
to help us handle the user input for restarting the game.
Game Over¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
UI.Button
is a UI element that appears as a clickable button with the text passed into itsLabel
parameter displayed on it.
When the button is pressed, we reset the game's variables, including .game-over
which will be used to decide what is drawn in our game's window.
Navigate to ui-loop
and add in .game-over
conditionals.
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
- Draw the Game Over UI if .game-over is true.
- Draw the Main Game UI if .game-over is false.
The game will now display the main game area or a game over screen depending on whether .game-over
is true.
Implementing a Timer¶
To add additional challenge to the game (for additional fun!), we can impose a time limit on each round.
Let us write a shard that decreases the .time-remaining
every time it is called.
1 2 3 4 5 6 7 8 9 |
|
- When the game is still running...
- ... decrease the time remaining by 1.
- The round is forced to end if the time remaining falls below 0.
We can have timer-tick
called every second consistently by using the Once
shard.
Place it within the logic-loop
where we are checking if a new round should be started.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
- Runs the timer-tick shard every 1 second.
Once
will run the code in itsAction
parameter every time the duration specified in itsEvery
parameter has passed.
Note
You can adjust the difficulty by changing the value of the max-timer
constant we defined at the start.
Outcome¶
Congratulations on making it to the end!
Your game now has 10 rounds, calculates the total score, shows a Game Over screen and lets you play it again at the end. It even has a timer to add a time constraint to each round.
Now that the tutorial is over, why not challenge yourself further by implementing a high-score system? You could also try adding support for a second player!
The possibilities are endless with the power of Shards in your hands.
The full game script can be found here.