From 790f9ed9653148864b8b01bd38a7c9c77b3d91cc Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Thu, 15 Mar 2012 17:51:57 +0100 Subject: [PATCH] Added better controls (hard drop, rotate only once) and short text on the page about how to control the game. --- index.html | 10 +++++++++- tetris-control.js | 26 ++++++++++++++++++++++---- tetris.js | 7 +++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 4d5d18f..e753dd5 100755 --- a/index.html +++ b/index.html @@ -16,6 +16,14 @@ - +
+ +
+
    Controls: +
  • a,d - left/right
  • +
  • w - rotate
  • +
  • s - down
  • +
+
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()) {