JavaScript How to pause simple canvas game, made with js and html5?

I created a simple snake game after following some simple tutorials on YouTube.

The problem is that the game does not have a pause function (e.g. when pressing P the game should pause/resume) and when the snake hits the border of the canvas the game restarts itself (but that is another problem).

Here is the complete code I have of the game: https://pastebin.com/URaDxSvF

The pause-related functions I've created:

function gamePaused{ /**i need help on this**/ }

function keyDown(e) {
  if (e.keyCode == 80) pauseGame();
}

function pauseGame() {
  if (!gamePaused) {
    game = clearTimeout(game);
    gamePaused = true;
  } else if (gamePaused) {
    game = setTimeout(loop, 1000 / 30);
    gamePaused = false;
  }
}
Answer:1

Create a Boolean variable called paused and set it to true if the player presses p, Then put an if statement around the loop that runs your game. and say if (!paused){run loop}

You can create a toggle pause function for when p is pressed.

function togglePause()
{
    if (!paused)
    {
        paused = true;
    } else if (paused)
    {
       paused= false;
    }

}

You also need to create an event listener for when p is pressed Like this

window.addEventListener('keydown', function (e) {
var key = e.keyCode;
if (key === 80)// p key
{
    togglePause();
}
});

up the top where you have Game objects and constants put in paused = false, and in your loop function do this

 draw(); 
if(!paused)
{ 
update(); 
}
Answer:2

Game state managment

Games will usually have various game states. Pause, End game, Press key to start, etc... As the optimal way to run a game is via a single main loop the easiest way to manage game states is to have variable hold the current state function and just assign that variable the appropriate function to handle the current state.

BTW you should use requestAnimationFrame instead of setTimeout or setinterval and keyCode should not be used as it is depreciated use keyEvent.code (see example for details)

requestAnimationFrame(mainLoop);  // start when code below done.
// set up keyboard IO
const keys = {
    KeyP : false,
    Enter : false,
    listener(e){
       if(keys[e.code] !== undefined){
           keys[e.code] = e.type === "keydown";
           e.preventDefault();
        }
    }
}
addEventListener("keydown",keys.listener);
addEventListener("keyup",keys.listener);

// the current game state
var currentState = startGame;

function startGame (){
    // code to do a single frame of start game
   // display press enter to start
   if(keys.Enter){
      keys.Enter = false;
      currentState = game;  // start the game
   }
}
function pause(){
    // code to do a single frame of pause
   // display pause
    if(keys.KeyP){
       keys.KeyP = false; // turn off key
       currentState = game;   // resume game
    }

}
function game(){
    // code to do a single frame of game
    if(keys.KeyP){
       keys.KeyP = false; // turn off key
       currentState = pause;  // pause game
    }
}
function mainLoop(time){
    currentState(); // call the current game state
    requestAnimationFrame(mainLoop);
}

Reference: https://developer.mozilla.org/es/docs/Web/API/KeyboardEvent

Answer:3

Im trying to update a video's metadata using the Youtube API V3 in my AngularJS app. I am able to upload the video using insert. I had unlimited problems trying to set the video metadata at the same ...

Im trying to update a video's metadata using the Youtube API V3 in my AngularJS app. I am able to upload the video using insert. I had unlimited problems trying to set the video metadata at the same ...

I'm working on making a wysiwyg editor using slate.js I'm in a situation where I'm trying to find the first node with text. This picture below shows what I'm talking about: Slate.js find first text ...

I'm working on making a wysiwyg editor using slate.js I'm in a situation where I'm trying to find the first node with text. This picture below shows what I'm talking about: Slate.js find first text ...

I have a function: function getNumber(number, array){ for(var i=0; i<array.length; i++){ if (array[i] == number) { return true; } else { return false; } } } console....

I have a function: function getNumber(number, array){ for(var i=0; i<array.length; i++){ if (array[i] == number) { return true; } else { return false; } } } console....

  1. check array length
  2. check array for value
  3. check array empty
  4. check array for duplicates
  5. check array empty javascript
  6. check array equality javascript
  7. check array of objects for value
  8. check array length python
  9. check array length php
  10. check array for duplicates javascript
  11. check array size
  12. check array is empty java
  13. check array length java
  14. check array size python
  15. check array empty python
  16. check array type python
  17. check arraylist is empty
  18. check array size java
  19. check array for string
  20. check array length bash

I would like to have a property dynamically accessed with a variable. For example, I have : data(){ redsection : '', bluesection : '', }, methods(){ changeColor(color, val){ ...

I would like to have a property dynamically accessed with a variable. For example, I have : data(){ redsection : '', bluesection : '', }, methods(){ changeColor(color, val){ ...

  1. vuejs pass variable to child component
  2. vuejs pass variable to component
  3. vuejs pass variable to css
  4. vuejs pass variable to parent
  5. vuejs pass variable to function
  6. vuejs pass variable to computed
  7. vuejs pass variable to method
  8. vuejs pass variable
  9. vuejs pass variable to child
  10. vuejs pass parameters to component
  11. vuejs router pass variable