Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
JS game framework 2020
<body style=margin:0>
<canvas id=a>
// initialize 2D canvas (c)
// initialize game state (s)
// initialize keys states (u,r,d,l for directions, k for all the keyboard)
// (initialize your global variables here)
// update u,l,d,r globals when an arrow key/wasd/zqsd is pressed or released
// update k[keyCode] if any other key is pressed/released
// start game loop (60fps)
// the canvas is cleared and adjusted to fullscreen at each frame
// draw each screen in the switch's cases
// in each screen, you can make key presses update the game state
// ex: "press enter to open the menu" => `if(k[13])s=1;`
case 0: // ex: draw title screen
case 1: // ex: draw menu screen
case 2: // ex: draw game board
case 3: // ex: draw game over screen
// handle click/touch events
// globals x and y contain the pointer's coordinates
// in each screen, you can make a click update the game state
// ex: "game over if we click on the bottom half of the screen" => `if(y>h/2)s=3;`
case 0: // react to clicks on screen 0
case 1: // react to clicks on screen 1
case 2: // react to clicks on screen 2
case 3: // react to clicks on screen 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.