diff --git a/index.html b/index.html index 4d5d18f..e753dd5 100755 --- a/index.html +++ b/index.html @@ -16,6 +16,14 @@ - +
+ +
+ +
diff --git a/tetris-control.js b/tetris-control.js index 3d3d431..5877c3d 100644 --- a/tetris-control.js +++ b/tetris-control.js @@ -1,7 +1,10 @@ var game = null; +var wasRecentlyRotated = false; function doTurn() { document.onkeypress = onKeyPressed; + document.onkeydown = onKeyDown; + document.onkeyup = onKeyUp; if (game.doTurn()) { window.setTimeout(doTurn, 333); } else { @@ -9,6 +12,14 @@ function doTurn() { } } +function onKeyDown(e) { + var ascii_space = 32; + + if (e.keyCode == ascii_space) { + game.hardDrop(); + } +} + function onKeyPressed(e) { if (String.fromCharCode(e.charCode) == 'a') { game.moveCurrentBrickHorizontally(-1); @@ -23,14 +34,21 @@ function onKeyPressed(e) { if (String.fromCharCode(e.charCode) == 's') { game.moveCurrentBrickDown(); return; - } - - if (String.fromCharCode(e.charCode) == 'w') { - game.rotateCurrentBrick(); + } + + if (String.fromCharCode(e.keyCode) == 'w') { + if (! wasRecentlyRotated) { + game.rotateCurrentBrick(); + wasRecentlyRotated = true; + } return; } } +function onKeyUp(e) { + wasRecentlyRotated = false; +} + $(document).ready(function() { game = new TetrisGame("tetris-board"); doTurn(); diff --git a/tetris.js b/tetris.js index 4965db1..eb24349 100644 --- a/tetris.js +++ b/tetris.js @@ -177,6 +177,13 @@ function TetrisGame(id) { this.resetCurrentBrick(); return true; } + + this.hardDrop = function() { + while (this.canCurrentBrickMove(0,-1)) { + this.moveCurrentBrickDown(); + } + return this.moveCurrentBrickDown(); + } this.rotateCurrentBrick = function() { if (this.canCurrentBrickRotate()) {