Learn Create Your Own Split Screen

Next Page

Clearly we're going to check when time_remaining gets to zero and then do something. But because we're ticking down in 1/40's of a second, it's possible that it'll never be exactly equal to zero so we'll check for zero or less:
if (time_remaining <= 0) {
If it is, we'll display our game over message. There's no need to do that using an image (as we did in Scratch) - we can just write the words we want.

We'll select a large bold font, and we'll make it red. For the x and y position, we'll specify the exact middle of the canvas (half the height, half the width).

More interestingly, we'll introduce a new feature of the canvas called textAlign, which says that instead of displaying the text starting at the x-y specified, it will be centred at the x-y specified.

ctx.fillStyle= "red"; ctx.font = "bold 50px Arial"; ctx.textAlign="center"; ctx.fillText("Game Over", myCanvas.width / 2, myCanvas.height / 2); ctx.textAlign="left";


But how to stop the script? The magic in this lies in the else part of the if -- it includes all those things we don't want to do once the game is over, including changing the score, changing the time, and moving the melon.