Creating a game loop
The core of this game, and virtually every game ever, is just an infinite loop. You can boil them all down to something like this:
That means, theoretically, these are trivial to implement, as shown here:
while(!quit) { handleInput() updateGame() drawGame() }
At its core, that's what we're going to write, but as you probably guessed, if it was that simple, I wouldn't have devoted an entire chapter to it. No, there are two problems we are going to be dealing with as we write it:
- The browser: If we were writing this game as a command-line program, we'd be able to use the preceding loop, but not in the browser. Any program running in the browser must give up control to the browser itself so that it can do whatever a browser does when it's not showing cat videos, and this kind of loop would...