You can now actually lose the game.
This commit is contained in:
1 parent
27b7ba6311
commit
18df86378c
2 files changed
+26
-21
No files matched your search
+6
-3
@@ -2,8 +2,11 @@ var game = null;
|
|||||||
|
|
||||||
function doTurn() {
|
function doTurn() {
|
||||||
document.onkeypress = onKeyPressed;
|
document.onkeypress = onKeyPressed;
|
||||||
game.doTurn();
|
if (game.doTurn()) {
|
||||||
window.setTimeout(doTurn, 333);
|
window.setTimeout(doTurn, 333);
|
||||||
|
} else {
|
||||||
|
alert('You lost!');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onKeyPressed(e) {
|
function onKeyPressed(e) {
|
||||||
@@ -31,4 +34,4 @@ function onKeyPressed(e) {
|
|||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
game = new TetrisGame("tetris-board");
|
game = new TetrisGame("tetris-board");
|
||||||
doTurn();
|
doTurn();
|
||||||
});
|
});
|
||||||
@@ -47,11 +47,8 @@ function TetrisGame(id) {
|
|||||||
this.getRandomBrick = function() {
|
this.getRandomBrick = function() {
|
||||||
return 1 + Math.floor(Math.random() * 7);
|
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 shape = this.brickShape(this.currentBrickId, this.currentBrickRot);
|
||||||
var lowestPoint = 0;
|
var lowestPoint = 0;
|
||||||
|
|
||||||
@@ -59,7 +56,14 @@ function TetrisGame(id) {
|
|||||||
lowestPoint = Math.min(lowestPoint, shape[i][1]);
|
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();
|
this.redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +153,7 @@ function TetrisGame(id) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.moveCurrentBrickDown = function() {
|
this.moveCurrentBrickDown = function() {
|
||||||
if (this.canCurrentBrickMove(0, -1)) {
|
if (this.canCurrentBrickMove(0, -1)) {
|
||||||
this.currentBrickLoc[1] -= 1;
|
this.currentBrickLoc[1] -= 1;
|
||||||
@@ -163,11 +167,15 @@ function TetrisGame(id) {
|
|||||||
var x = this.currentBrickLoc[0]+shape[i][0];
|
var x = this.currentBrickLoc[0]+shape[i][0];
|
||||||
var y = this.currentBrickLoc[1]+shape[i][1];
|
var y = this.currentBrickLoc[1]+shape[i][1];
|
||||||
|
|
||||||
|
if (y >= this.height) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
this.board[x][y] = this.bricks[this.currentBrickId];
|
this.board[x][y] = this.bricks[this.currentBrickId];
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resetCurrentBrick();
|
this.resetCurrentBrick();
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.rotateCurrentBrick = function() {
|
this.rotateCurrentBrick = function() {
|
||||||
@@ -212,25 +220,19 @@ function TetrisGame(id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.isGameLost = function() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.doTurn = function() {
|
this.doTurn = function() {
|
||||||
this.moveCurrentBrickDown();
|
if (! this.moveCurrentBrickDown()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var filledLines = this.getFilledLines();
|
var filledLines = this.getFilledLines();
|
||||||
for (var i = 0; i < filledLines.length; i++) {
|
for (var i = 0; i < filledLines.length; i++) {
|
||||||
this.removeLine(filledLines[i]-1);
|
this.removeLine(filledLines[i]-i);
|
||||||
}
|
}
|
||||||
this.redraw();
|
this.redraw();
|
||||||
|
|
||||||
if (this.isGameLost) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user