Learn Create Your Own Split Screen

Next Page

Our goal is that the game cannot restart until a second after the game finishes.

We'll achieve this by keeping track of when the game was most recently running.

We'll declare a global variable:

var time_game_last_running; // when did the game finish?
The easiest thing is just to continually set the variable every time we render a frame with the game_mode set to 'running'.

When the game stops running, the variable will be left set at the last time it was running.

function Do_a_Frame () { ctx.clearRect(0, 0, myCanvas.width, myCanvas.height); bird.Do_Frame_Things(); switch (game_mode) { case 'prestart': { display_intro_instructions(); break; } case 'running': { time_game_last_running = new Date(); // Game was running at this time show_the_pipes(); // etc
Date() is a built-in Javascript function which returns the current date and time.

We haven't discussed dates and times before, but here's some good information available here and here.

The important thing for us right now is to know that date/times are internally stored as the number of milliseconds (thousandths of a second) since January 1st, 1970.

That might seem odd to us humans, but computers often represent date/times like that. The beauty of the system is you can easily subtract two dates and find the number of milliseconds between them.