From 59a7eb083ff1f8f0d6f1e4eb4cd4637e3fc7a1da Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Fri, 16 Mar 2012 14:59:30 +0100 Subject: [PATCH 1/2] Added score. --- tetris.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tetris.js b/tetris.js index d2d72bf..8e70129 100644 --- a/tetris.js +++ b/tetris.js @@ -28,7 +28,9 @@ function TetrisGame(id) { /* Variables: */ /**************/ this.gameIsOver = false; - + this.gameLinesCleared = 0; + this.gameScore = 0; + this.width = 10; this.height = 15; this.canvas = new TetrisCanvas(id, this.width, this.height); @@ -54,6 +56,17 @@ function TetrisGame(id) { this.drawCurrentBrick(); } + this.xyToTileIndex = function(x,y) { + return y * this.width + x; + } + + this.tileIndexToXY = function(tileIndex) { + var y = Math.floor(tileIndex / this.width); + var x = tileIndex % y; + + return [x,y]; + } + this.getRandomBrick = function() { return 1 + Math.floor(Math.random() * 7); } @@ -253,6 +266,9 @@ function TetrisGame(id) { for (var i = 0; i < filledLines.length; i++) { this.removeLine(filledLines[i]-i); } + + this.gameScore += filledLines.length * 100; + this.gameLinesCleared += filledLines.length; this.drawUnlock(); this.redraw(); From 3e4b7f46bd624fa7f1b23108c8d35686645e93bb Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Fri, 16 Mar 2012 14:59:54 +0100 Subject: [PATCH 2/2] Added util function. --- utils.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/utils.js b/utils.js index 01c8f5b..d8771b3 100644 --- a/utils.js +++ b/utils.js @@ -10,3 +10,13 @@ function createArray(length) { return a; } + +function arrayContains(array, element) { + for (var i = 0; i < array.length; i++) { + if (array[i] == element) { + return true; + } + } + + return false; +}