Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created Nov 2, 2020
Embed
What would you like to do?
initControls() {
document.addEventListener("keydown", (e) => {
const {code} = e;
if (!this.canPlay && code !== "KeyP") {
return;
}
switch (code) {
case "ArrowRight":
this.attemptMoveRight();
break;
case "ArrowLeft":
this.attemptMoveLeft();
break;
case "ArrowDown":
this.attemptMoveDown();
break;
case "KeyR":
this.attemptRotate();
break;
case "KeyP":
this.pauseOrResumeGame();
break;
}
this.syncExistingPiecesWithBoard();
});
this.$btnDown.addEventListener("click", () => {
if (!this.canPlay) return;
this.attemptMoveDown();
});
this.$btnRight.addEventListener("click", () => {
if (!this.canPlay) return;
this.attemptMoveRight();
});
this.$btnLeft.addEventListener("click", () => {
if (!this.canPlay) return;
this.attemptMoveLeft();
});
this.$btnRotate.addEventListener("click", () => {
if (!this.canPlay) return;
this.attemptRotate();
});
[this.$btnPause, this.$btnResume].forEach($btn => $btn.addEventListener("click", () => {
this.pauseOrResumeGame();
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment