diff --git a/tetris-canvas.js b/tetris-canvas.js index 20b2ad7..7112282 100644 --- a/tetris-canvas.js +++ b/tetris-canvas.js @@ -1,4 +1,4 @@ -function TetrisCanvas(id, width, height, linesId) { +function TetrisCanvas(canvasId, width, height) { /***********/ /* Colors: */ /***********/ @@ -28,9 +28,6 @@ function TetrisCanvas(id, width, height, linesId) { this.sWidth = width; this.sHeight = height; - /* Score box: */ - this.linesElement = 0; - /**************/ /* Functions: */ /**************/ @@ -54,27 +51,19 @@ function TetrisCanvas(id, width, height, linesId) { } } - this.updateLines = function(newLines) { - if (this.linesElement != 0) { - this.linesElement.html(newLines); - } - } + /***************/ /* Constructor */ /***************/ - this.TetrisCanvas = function(id, linesId) { - var canvas = document.getElementById(id); - this.pxHeight = $("#"+id).height(); - this.pxWidth = $("#"+id).width(); + this.TetrisCanvas = function(canvasId) { + var canvas = document.getElementById(canvasId); + this.pxHeight = $("#"+canvasId).height(); + this.pxWidth = $("#"+canvasId).width(); this.pxRectSize = (this.pxWidth - (this.pxSeparatorSize * this.sWidth)) / (this.sWidth); this.context = canvas.getContext("2d"); - - if (linesId) { - this.linesElement = $("#"+linesId); - } } - this.TetrisCanvas(id, linesId); + this.TetrisCanvas(canvasId); } diff --git a/tetris.js b/tetris.js index f3d6db0..41f73ba 100644 --- a/tetris.js +++ b/tetris.js @@ -1,4 +1,4 @@ -function TetrisGame(id, linesId) { +function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { /*********/ /* Enum: */ /*********/ @@ -32,7 +32,8 @@ function TetrisGame(id, linesId) { this.width = 10; this.height = 15; - this.canvas = new TetrisCanvas(id, this.width, this.height, linesId); + this.canvas = new TetrisCanvas(canvasId, this.width, this.height, linesCounterId, nextBricksCanvasId); + this.lineCounterElement = 0; this.board = createArray(this.width, this.height); this.currentBrickId = 0; @@ -265,6 +266,12 @@ function TetrisGame(id, linesId) { } } } + + this.updateLineCounter = function() { + if (this.lineCounterElement != 0) { + this.lineCounterElement.html('' +this.linesCleared); + } + } this.doTurn = function() { this.drawLock(); @@ -279,7 +286,7 @@ function TetrisGame(id, linesId) { } this.linesCleared += filledLines.length; - this.canvas.updateLines(this.linesCleared); + this.updateLineCounter(); this.drawUnlock(); this.redraw(); @@ -303,7 +310,10 @@ function TetrisGame(id, linesId) { /****************/ /* Constructor: */ /****************/ - this.TetrisGame = function(id) { + this.TetrisGame = function() { + // Find the line counter element if defined: + this.lineCounterElement = $('#' +linesCounterId); + // Initialize the board: for (var y = 0; y < 15; y++) { for (var x = 0; x < 10; x++) { @@ -318,5 +328,5 @@ function TetrisGame(id, linesId) { this.getNextCurrentBrick(); }; - this.TetrisGame(id); + this.TetrisGame(); }