From 9ec86ee63e9e70115d59040c2da3ee6505b089ac Mon Sep 17 00:00:00 2001 From: Abdelrahman Tarek Date: Sat, 19 Oct 2019 20:43:12 +0200 Subject: [PATCH] Add key up / w / space --- game.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/game.js b/game.js index 9ba0b09..01133b7 100644 --- a/game.js +++ b/game.js @@ -1,6 +1,7 @@ const RAD = Math.PI/180; const scrn = document.getElementById('canvas'); const sctx = scrn.getContext("2d"); + scrn.tabIndex = 1; scrn.addEventListener("click",()=>{ switch (state.curr) { case state.getReady : @@ -21,6 +22,31 @@ } }) + scrn.onkeydown = function keyDown(e) { + if (e.keyCode == 32 || e.keyCode == 87 || e.keyCode == 38) // Space Key or W key or arrow up + { + switch (state.curr) { + case state.getReady : + state.curr = state.Play; + SFX.start.play(); + break; + case state.Play : + bird.flap(); + break; + case state.gameOver : + state.curr = state.getReady; + bird.speed = 0; + bird.y = 100; + pipe.pipes=[]; + UI.score.curr = 0; + SFX.played=false; + break; + } + } +} + + + let frames = 0; let dx = 2; const state = {