-
-
Save melcor76/c40a65a336ad03783be4a611dbf0234f to your computer and use it in GitHub Desktop.
js-tetris - event listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('keydown', event => { | |
if (moves[event.keyCode]) { | |
// Stop the event from bubbling. | |
event.preventDefault(); | |
// Get new state of piece | |
let p = moves[event.keyCode](board.piece); | |
if (board.valid(p)) { | |
// If the move is valid, move the piece. | |
board.piece.move(p); | |
// Clear old position before drawing. | |
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); | |
board.piece.draw(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment