Pausing and changing game speeds

Altering game timing

Now we're looking at the speed and timing of things, it's time to overhaul the way we manage game speed. In doing so, we'll not only be able to increase or decrease the game speed, but also pause the game entirely.

View example

Thankfully, because of the way in which we've already set up our code, we'll only need to make some minor additions and changes. Let's get started!

We'll begin by creating some new global variables: gameTime, which keeps track of the time which has currently elapsed in the game, and currentSpeed, which is the index of the current speed modifier in the gameSpeeds array.

We're using gameSpeeds to provide a list of speeds at which our game can run. Each entry in the array consists of two parameters: the name of the speed, for reference purposes, and mult, which is the multiplication factor of normal game speeds at which this entry causes the game to run:

var gameTime = 0;
var gameSpeeds = [
	{name:"Normal", mult:1},
	{name:"Slow", mult:0.3},
	{name:"Fast", mult:3},
	{name:"Paused", mult:0}
var currentSpeed = 0;

As you can see, "Normal" (index 0) just runs the game at 1x normal game speed, ie; does not alter the speed at all. "Fast" runs the game at 3x normal game speeds, and "Paused" runs at 0x; nothing progresses.

Next, in our windows onload method, we're going to add another key we want to listen for in the "keyup" event listener. After the if line that checks if an arrow key (keyCodes 37 to 40) has been pressed, we'll add the following:

			currentSpeed = (currentSpeed>=(gameSpeeds.length-1) ? 0 : currentSpeed+1);

This will listen for the letter "s" on the keyboard being pressed and released. When this key is released, it will select the next gameSpeeds entry (or loop back to the first, or 0 index if the currentSpeed as at the end of the list), and set the currentSpeed value to the index of that speed.

Modifying the drawGame function

Finally, we'll make some changes to the drawGame function. After we've calculated the timeElapsed variable value, we'll add a line to increase the gameTime by the amount of time elapsed since the last frame, multiplied by the current gameSpeeds entry at index currentSpeed's mult property.

	gameTime+= Math.floor(timeElapsed * gameSpeeds[currentSpeed].mult);

We'll also add a check to the players movement input if block - we'll not only check that processMovement returns false, but also that the game isn't currently paused, by checking that the current gameSpeeds mult property value isn't 0. We'll also change all of the movement processing methods time arguments from currentFrameTime to gameTime:

	if(!player.processMovement(gameTime) && gameSpeeds[currentSpeed].mult!=0)
		if(keysDown[38] && player.canMoveUp())			{ player.moveUp(gameTime); }
		else if(keysDown[40] && player.canMoveDown())	{ player.moveDown(gameTime); }
		else if(keysDown[37] && player.canMoveLeft())	{ player.moveLeft(gameTime); }
		else if(keysDown[39] && player.canMoveRight())	{ player.moveRight(gameTime); }

Also, for our information, we'll render another bit of text after we've shown the FPS; this will show us the currently selected game speed:

	ctx.fillText("Game speed: " + gameSpeeds[currentSpeed].name, 10, 40);

Finally, we'll change the time used for finding the current frame for animated tiles inside the y,x nested loops for tile rendering from currentFrameTime to gameTime:

			var sprite = getFrame(tile.sprite, tile.spriteDuration,
				gameTime, tile.animated);

And that's it. Any more game speeds we want to add, we can add to the gameSpeeds list. Managing time in this method allows us to easily manipulate game speed for both rendering (animated effects) and logic (movement, etc.).

Save your HTML document and load it up, or try our online example!

Page loaded in 0.008 second(s).