You can now actually lose the game.

This commit is contained in:
Robert Kolner committed 2012-03-15 17:24:42 +01:00
1 parent 27b7ba6311
commit 18df86378c
2 files changed
+26 -21

No files matched your search

+6 -3
View File
@@ -2,8 +2,11 @@ var game = null;
function doTurn() {
document.onkeypress = onKeyPressed;
game.doTurn();
window.setTimeout(doTurn, 333);
if (game.doTurn()) {
window.setTimeout(doTurn, 333);
} else {
alert('You lost!');
}
}
function onKeyPressed(e) {
@@ -31,4 +34,4 @@ function onKeyPressed(e) {
$(document).ready(function() {
game = new TetrisGame("tetris-board");
doTurn();
});
});
+20 -18
View File
@@ -47,11 +47,8 @@ function TetrisGame(id) {
this.getRandomBrick = function() {
return 1 + Math.floor(Math.random() * 7);
}
this.resetCurrentBrick = function() {
this.currentBrickId = this.getRandomBrick();
this.currentBrickRot = 0;
this.getBrickStartingLocation = function(brickId) {
var shape = this.brickShape(this.currentBrickId, this.currentBrickRot);
var lowestPoint = 0;
@@ -59,7 +56,14 @@ function TetrisGame(id) {
lowestPoint = Math.min(lowestPoint, shape[i][1]);
}
this.currentBrickLoc = [Math.floor(this.width/2),this.height-lowestPoint-1];
return [Math.floor(this.width/2),this.height-lowestPoint-1];
}
this.resetCurrentBrick = function() {
this.currentBrickId = this.getRandomBrick();
this.currentBrickRot = 0;
this.currentBrickLoc = this.getBrickStartingLocation(this.currentBrickId);
this.redraw();
}
@@ -149,7 +153,7 @@ function TetrisGame(id) {
}
return false;
}
this.moveCurrentBrickDown = function() {
if (this.canCurrentBrickMove(0, -1)) {
this.currentBrickLoc[1] -= 1;
@@ -163,11 +167,15 @@ function TetrisGame(id) {
var x = this.currentBrickLoc[0]+shape[i][0];
var y = this.currentBrickLoc[1]+shape[i][1];
if (y >= this.height) {
return false;
}
this.board[x][y] = this.bricks[this.currentBrickId];
}
this.resetCurrentBrick();
return false;
return true;
}
this.rotateCurrentBrick = function() {
@@ -212,25 +220,19 @@ function TetrisGame(id) {
}
}
}
}
this.isGameLost = function() {
return false;
}
}
this.doTurn = function() {
this.moveCurrentBrickDown();
if (! this.moveCurrentBrickDown()) {
return false;
}
var filledLines = this.getFilledLines();
for (var i = 0; i < filledLines.length; i++) {
this.removeLine(filledLines[i]-1);
this.removeLine(filledLines[i]-i);
}
this.redraw();
if (this.isGameLost) {
return false;
}
return true;
}