Skip to content

Commit

Permalink
added queue and lock delay
Browse files Browse the repository at this point in the history
  • Loading branch information
tonytanger committed Sep 14, 2014
1 parent b3c0fcd commit 0da4bb6
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 132 deletions.
26 changes: 13 additions & 13 deletions board.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,6 @@ Board.prototype.getBlock = function(row, col) {
Board.prototype.addTetromino = function(tetromino) {
// get x and y of the blocks, and add it to the board.
tetromino.board = this;
// var trow = tetromino.row;
// var tcol = tetromino.col;
// var b0row = tetromino.blocks[0].row;
// var b0col = tetromino.blocks[0].col;

// var b1row = tetromino.blocks[1].row;
// var b1col = tetromino.blocks[1].col;

// var b2row = tetromino.blocks[2].row;
// var b2col = tetromino.blocks[2].col;

// var b3row = tetromino.blocks[3].row;
// var b3col = tetromino.blocks[3].col;

// check if any of the blocks are already occupied.
for(var i = 0; i < 4; i++) {
Expand All @@ -95,6 +82,15 @@ Board.prototype.addTetromino = function(tetromino) {
return true;
};

Board.prototype.removeTetromino = function(tetromino) {
tetromino.setGhostDead();
for(var i = 0; i < 4; i++) {
this.setDead(tetromino.blocks[i].row, tetromino.blocks[i].col, tetromino.blocks[i].blockType);
}
tetromino.reset();
return true;
};

Board.prototype.setDead = function(row, col) {
this.theBoard[row][col].setDead()
};
Expand All @@ -103,13 +99,17 @@ Board.prototype.setAlive = function(row, col, blockType) {
this.theBoard[row][col].setAlive(blockType);
};

// findFullLines () -> int
// loop through theBoard array and return the number of rows are full

Board.prototype.findFullLine = function() {
var isFull = true;
var count = 0;
for(var i = 0; i < this.height; i++) {
isFull = true;
for(var j = 0; j < this.width; j++) {
if(this.theBoard[i][j].isDead()) {
// as soon as one block is dead, no need to see the rest
isFull = false;
break;
}
Expand Down
160 changes: 160 additions & 0 deletions game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
function Game() {
this.board = new Board();
this.keyboard = new Keyboard();
this.level = new Level();
this.score = new Score(this.level);
this.nextTetromino = 0;
this.tetrominoInQueue = 0;
this.tetrominoInHold = 0;
this.scoreDOM = 0;
this.boardDOM = 0;
this.keyHandler = 0;
this.timeout = 0;
this.pause = false;
this.hold = false;
this.lastKeyPress = new Date().getTime();
this.gameOver =false;
}

Game.prototype.start = function() {
this.scoreDOM = "score";
this.boardDOM = "board_wrapper";
this.nextTetromino = this.level.getNextTetromino();
this.tetrominoInQueue = this.level.getNextTetromino();
this.board.addTetromino(this.nextTetromino);
_this = this;
this.keyHandler = function(event) {
var val = _this.keyboard.onKeyDown(event.keyCode, _this.nextTetromino);
_this.lastKeyPress = new Date().getTime();
switch(val) {
case 0:
// rotate, left, right, down
_this.updateBoard();
break;
case 1:
// drop
if(_this.onDrop()) {
clearTimeout(_this.timeout);
_this.downLoop();
_this.updateBoard();
}
break;
case 2:
// pause/unpause
if(_this.pause) {
_this.downLoop();
_this.pause = false;
} else {
clearTimeout(_this.timeout);
// disable keyboard???
_this.pause = true;
}
break;
case 3:
//hold
// if something in hold
// remove nextTetromino from the board
// add tetrominoInHold to board
// swap nextTetromino with tetrominoInHold
// set flag so pressing "c" doesn't let you swap again.
// else nothing in hold
// set tetrominoInhold as nextTetromino
// remove nextTetromino from board
// set nextTetromino as tetrominoInQueue
// set tetrominoInQueue to getNextTetromino
// add nextTetromino
// set flag so pressing "c" doesn't let you swap again.

if(_this.tetrominoInHold) {
// check flag, can I swap again?
if(!_this.hold) {
_this.board.removeTetromino(_this.nextTetromino);
var temp = _this.tetrominoInHold;
_this.tetrominoInHold = _this.nextTetromino;
_this.nextTetromino = temp;
_this.board.addTetromino(_this.nextTetromino);
//set flag
_this.hold = true;
}
} else {
// check flag, can I swap again?
if(!_this.hold) {
_this.tetrominoInHold = _this.nextTetromino;
_this.board.removeTetromino(_this.nextTetromino);
_this.nextTetromino = _this.tetrominoInQueue;
_this.tetrominoInQueue = _this.level.getNextTetromino();
_this.board.addTetromino(_this.nextTetromino);
// set flag
_this.hold = true;
}
}
_this.updateBoard();
break;
default:
// something else
break;
}
};
this.keyboard.init(this.keyHandler);
this.downLoop();
this.updateBoard();
};

/**
* Update Display
*/
Game.prototype.updateBoard = function() {
document.getElementById(this.scoreDOM).innerHTML = this.score.print();
document.getElementById(this.scoreDOM).innerHTML += "<br />";
document.getElementById(this.scoreDOM).innerHTML += "QUEUE: " + this.tetrominoInQueue.print();
document.getElementById(this.scoreDOM).innerHTML += "<br />";
document.getElementById(this.scoreDOM).innerHTML += "HOLD: " + (this.tetrominoInHold? this.tetrominoInHold.print(): "NULL");
document.getElementById(this.boardDOM).innerHTML = this.board.print();
};

Game.prototype.downLoop = function() {
var _this = this;
this.timeout = setTimeout(function() {
if(!_this.nextTetromino.down()) {
// tetromino cannot go down anymore

//check lockDelay. Was last keypress less than lockDelay? If so. don't onDrop yet. call this function and return.
if(new Date().getTime() - _this.lastKeyPress >= _this.level.lockDelay) {
_this.onDrop();
}
}
_this.updateBoard();
if(!_this.gameOver) _this.downLoop();
console.log(_this.level.lockDelay);
}, _this.level.lockDelay);
};

Game.prototype.onDrop = function() {
// check if row clear?
// add appropriate score
this.score.addScore(this.board.findFullLine());

// stop current tetromino
// get next tetromino
this.nextTetromino = this.tetrominoInQueue;

// add new tetromino
if(this.board.addTetromino(this.nextTetromino)) {
this.tetrominoInQueue = this.level.getNextTetromino();
} else {
this.over();
return false;
}
this.hold = false;
return true;
}

Game.prototype.over = function() {
// stop down loop on current tertromino
// deactivate keyboard
this.keyboard.remove();
this.gameOver = true;
// print appropriate message
console.log("GAME OVER!");
document.getElementById("game_over").style.zIndex = "150";
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<div id="game_over">
GAME OVER!
</div>
<script src="game.js"></script>
<script src="block.js"></script>
<script src="board.js"></script>
<script src="tetromino.js"></script>
Expand Down
62 changes: 36 additions & 26 deletions keyboard.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
function Keyboard() {
this.handler = 0;
}

var handler = function(event) {
switch(event.keyCode) {
Keyboard.prototype.onKeyDown = function(key, tetromino) {
switch(key) {
case 38:
//up
nextBlock.rotate();
break;
tetromino.rotate();
console.log("rotate");
return 0;
case 37:
//left
nextBlock.left();
break;
tetromino.left();
console.log("left");
return 0;
case 39:
// right
nextBlock.right();
break;
tetromino.right();
console.log("right");
return 0;
case 40:
//down
nextBlock.down();
break;
tetromino.down();
console.log("down");
return 0;
case 32:
// space
nextBlock.drop();
deactivateKeyboard();
gameLoop(downInterval);
break;
case 88:
// special
clearInterval(downInterval);
break;
// space: drop
tetromino.drop();
console.log("drop");
return 1;
case 80:
// p: pause
console.log("pause");
return 2;
case 67:
// c: hold
console.log("hold");
return 3;
}
updateBoard();
};

function activateKeyboard() {
document.addEventListener('keydown', handler);
}
Keyboard.prototype.init = function(handler) {
this.handler = handler;
document.addEventListener('keydown', this.handler);
};

function deactivateKeyboard() {
document.removeEventListener('keydown', handler);
}
Keyboard.prototype.remove = function(){
document.removeEventListener('keydown', this.handler);
};
45 changes: 12 additions & 33 deletions level.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function Level() {
this.level = 1;
this.rand = 0;
this.blockInQueue = 0;
this.nextBlock = 0;
this.tetrominoInQueue = 0;
this.nextTetromino = 0;
this.lockDelay = 1000;
}

Level.prototype.getLevel = function() {
return this.level;
};

Level.prototype.getNextBlock = function() {
Level.prototype.getNextTetromino = function() {
var new_rand = Math.floor(Math.random() * 7) + 1;
while(new_rand === this.rand) {
new_rand = Math.floor(Math.random() * 7) + 1;
Expand All @@ -19,56 +19,35 @@ Level.prototype.getNextBlock = function() {
console.log("Random Nunber: " + this.rand);
switch(this.rand) {
case 1:
this.nextBlock = new IBlock();
this.nextTetromino = new IBlock();
break;
case 2:
this.nextBlock = new JBlock();
this.nextTetromino = new JBlock();
break;
case 3:
this.nextBlock = new SBlock();
this.nextTetromino = new SBlock();
break;
case 4:
this.nextBlock = new ZBlock();
this.nextTetromino = new ZBlock();
break;
case 5:
this.nextBlock = new OBlock();
this.nextTetromino = new OBlock();
break;
case 6:
this.nextBlock = new TBlock();
this.nextTetromino = new TBlock();
break;
case 7:
this.nextBlock = new LBlock();
this.nextTetromino = new LBlock();
break;
}
// if(this.nextBlock instanceof IBlock) {
// this.nextBlock = new JBlock();
// } else if(this.nextBlock instanceof JBlock) {
// this.nextBlock = new LBlock();
// } else if(this.nextBlock instanceof LBlock) {
// this.nextBlock = new SBlock();
// } else if(this.nextBlock instanceof SBlock) {
// this.nextBlock = new ZBlock();
// } else if(this.nextBlock instanceof ZBlock) {
// this.nextBlock = new OBlock();
// } else if(this.nextBlock instanceof OBlock) {
// this.nextBlock = new TBlock();
// } else if(this.nextBlock instanceof TBlock) {
// this.nextBlock = new IBlock();
// } else {
// this.nextBlock = new IBlock();
// }
return this.nextBlock;
return this.nextTetromino;
};

Level.prototype.levelUp = function() {
if(this.lockDelay >= 400) {
if(this.lockDelay >= 500) {
this.lockDelay -= 100;
clearInterval(downInterval);
setInterval(function() {gameLoop(downInterval)}, this.lockDelay);
} else {
this.lockDelay -= 50;
clearInterval(downInterval);
setInterval(function() {gameLoop(downInterval)}, this.lockDelay);
}
if(this.level < 15) {
this.level++;
Expand Down
Loading

0 comments on commit 0da4bb6

Please sign in to comment.