Improve key input

This commit is contained in:
Marcel
2019-04-08 21:59:43 +02:00
parent 9dc90621ab
commit e4d127936f

23
game.js
View File

@@ -45,7 +45,7 @@ class Game {
this.restartButton = document.getElementById('restart-btn'); this.restartButton = document.getElementById('restart-btn');
this.fieldSize = {x: 16, y: 12}; this.fieldSize = {x: 16, y: 12};
this.bombCount = this.fieldSize.x * this.fieldSize.y * .25; this.bombCount = this.fieldSize.x * this.fieldSize.y * .2;
this.field = []; this.field = [];
this.gameOver = false; this.gameOver = false;
this.scaleFactor = .5; this.scaleFactor = .5;
@@ -323,7 +323,7 @@ class Game {
this.rightClickEventHandler(e); this.rightClickEventHandler(e);
}); });
window.addEventListener("keyup", (e) => { window.addEventListener("keypress", (e) => {
e.preventDefault(); e.preventDefault();
const changeRate = .05; const changeRate = .05;
@@ -332,20 +332,23 @@ class Game {
let newWindowX = this.windowX; let newWindowX = this.windowX;
let newWindowY = this.windowY; let newWindowY = this.windowY;
if (e.code === "BracketRight") { if (e.code === "BracketRight" || e.key === "e") {
newZoomFactor -= changeRate; newZoomFactor -= changeRate;
} else if (e.code === "Slash") { }
if (e.code === "Slash" || e.key === "q") {
newZoomFactor += changeRate; newZoomFactor += changeRate;
} else if (e.code === "ArrowLeft") { }
if (e.code === "ArrowLeft" || e.key === "a") {
newWindowX -= changeRate; newWindowX -= changeRate;
} else if (e.code === "ArrowRight") { }
if (e.code === "ArrowRight" || e.key === "d") {
newWindowX += changeRate; newWindowX += changeRate;
} else if (e.code === "ArrowUp") { }
if (e.code === "ArrowUp" || e.key === "w") {
newWindowY -= changeRate; newWindowY -= changeRate;
} else if (e.code === "ArrowDown") { }
if (e.code === "ArrowDown" || e.key === "s") {
newWindowY += changeRate; newWindowY += changeRate;
} else {
return;
} }
newZoomFactor = Math.min(newZoomFactor, 1); newZoomFactor = Math.min(newZoomFactor, 1);