Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kinson completed, prior to SERIOUS refactoring #2

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# project_tetris_js
block based gaming
AJK's Tetris!
===========

Do you really need a description of Tetris?

[Play!][1]


[1]: https://htmlpreview.github.io/?https://github.com/kinsona/project_tetris_js/blob/master/tetris.html
59 changes: 59 additions & 0 deletions board.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var TETRIS = TETRIS || {};


TETRIS.Board = (function() {

var width,
height,
rows;

// row look like [,,,,,]
// board look like [row,row,row,row]
// 0,0 is top-left

function init() {
width = 10;
height = 24;
rows = _buildRows();
};


function _buildRows() {
var tempRows = []

for (var i = 0; i < height; i++) {
tempRows.push(_buildSingleRow(i));
};

return tempRows;
};


function _buildSingleRow(rowNumber) {
var row = [];

for (var colNumber = 0; colNumber < width; colNumber++) {
var newUnit = new GridUnit(rowNumber, colNumber);
row.push(newUnit);
};

return row;
};


function GridUnit(row, col) {
this.row = row;
this.col = col;
};




return {
init: init,
getHeight: function() { return height },
getWidth: function() { return width },
getRows: function() { return rows }
};

})();
83 changes: 83 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var TETRIS = TETRIS || {};


TETRIS.MainModule = (function() {

var _gameloop;
var score;


function init() {
TETRIS.Board.init();
TETRIS.Piece.init();
TETRIS.View.init();

var rows = TETRIS.Board.getRows();
TETRIS.View.drawBoard(rows);
};


function start() {
score = 0;
TETRIS.View.enableControls();
_gameloop = setInterval(_tick, 750);
};

function _tick() {

if ( TETRIS.Piece.hasActivePiece() ) {
TETRIS.Piece.stepDown();
}
else {
TETRIS.Piece.spawnShape(TETRIS.ShapeModule);
}

TETRIS.Piece.checkCompleteRows();

var pieces = TETRIS.Piece.getPieces();
TETRIS.View.render(pieces, score);

score += TETRIS.Piece.removeClearedRows();
_checkGameOver();
};


function keydown() {
var input = event.which;
switch (input) {
case 37:
TETRIS.Piece.slideAllLeft();
break;
case 39:
TETRIS.Piece.slideAllRight();
break;
case 40:
TETRIS.Piece.forceAllDown();
break;
};

var pieces = TETRIS.Piece.getPieces();
TETRIS.View.render(pieces, score);;
};


function _checkGameOver() {
if (TETRIS.Piece.checkGameOver()) {
_endGame();
}
};


function _endGame() {
clearInterval(_gameloop);
TETRIS.View.renderEndGame();
};


return {
init: init,
start: start,
keydown: keydown
};

})();
226 changes: 226 additions & 0 deletions piece.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
var TETRIS = TETRIS || {};


TETRIS.Piece = (function() {

var pieces;
var _activePiece;
var _rowsToClear;
var _shapes;
var gameover;


function init() {
pieces = [];
_rowsToClear = 0;
gameover = false;
_shapes = ['2x2','4x1','L-left','L-right','S-left','S-right'];
};


function _PieceConstructor(row, col) {
this.row = row;
this.col = col;
this.active = true;
this.set = false;
this.clearing = false;
};




_PieceConstructor.prototype.detectCollision = function() {
var maxRow = this.findMaxRow();
return (this.row === maxRow);
};


_PieceConstructor.prototype.findDownDistance = function() {
return (this.findMaxRow() - this.row);
};


_PieceConstructor.prototype.findMaxRow = function() {
var self = this;

var piecesInColumn = $(pieces).filter( function( index, object) {
return (object.col === self.col && !object.active)
});

if (piecesInColumn.length > 0) {
return piecesInColumn.last()[0].row - 1;
}
else {
return TETRIS.Board.getHeight() - 1;
};
};


_PieceConstructor.prototype.stop = function() {
this.active = false;
this.set = true;
if (this.row < 4) {
gameover = true;
};
};


_PieceConstructor.prototype.slideLeft = function() {
this.col = Math.max(this.col - 1, 0);
};


_PieceConstructor.prototype.slideRight = function() {
this.col = Math.min(this.col + 1, TETRIS.Board.getWidth() - 1 );
};


_PieceConstructor.prototype.forceDown = function(distance) {
this.row += (distance - 1);
};


_PieceConstructor.prototype.validMove = function(columnMove) {
var self = this;
var targetColumn = self.col + columnMove;

var piecesInRow = $(pieces).filter( function( index, object) {
return (object.row === self.row && object.col === targetColumn && !object.active)
});

return (_columnInBounds(targetColumn) && piecesInRow.length === 0);
};


function _columnInBounds(colNumber) {
return (colNumber >= 0 && colNumber < TETRIS.Board.getWidth() );
};


function spawnShape(ShapeModule) {
_activePiece = []

var shapeOffsets = ShapeModule.getShapeOffsets();
var baseColumn = _randomColumn(shapeOffsets[0]);

shapeOffsets.slice(1,5).forEach( function(offset, index) {
spawnPiece(offset.row, baseColumn + offset.col);
})

};


function spawnPiece(row, col) {
newPiece = new _PieceConstructor(row,col);
_activePiece.push(newPiece);
pieces.push(newPiece);
};


function _randomColumn(rangeOffset) {
return Math.floor( Math.random() * (rangeOffset.high - rangeOffset.low) );
};


function hasActivePiece() {
return !!_activePiece;
};


function stepDown() {
_activePiece.forEach(function(piece) { piece.row++; } );
_stopPiece();
};


function _stopPiece() {
if (_activePiece.some(function(piece) { return piece.detectCollision() } ) ) {
_activePiece.forEach(function(piece) { piece.stop() } );
_activePiece = undefined;
};
};


function slideAllLeft() {
if (_activePiece.every(function(piece) { return piece.validMove(-1) } ) ) {
_activePiece.forEach(function(piece) {
piece.slideLeft();
});
_stopPiece();
};
};


function slideAllRight() {
if (_activePiece.every(function(piece) { return piece.validMove(1) } ) ) {
_activePiece.forEach(function(piece) {
piece.slideRight();
});
_stopPiece();
};
};


function forceAllDown() {
var minDownDistance = _activePiece.reduce(function(previous, current) {
return Math.min(previous, current.findDownDistance() )
}, _activePiece[0].findDownDistance() );

_activePiece.forEach(function(piece) { piece.forceDown(minDownDistance) } );
};


function checkCompleteRows() {
for(var i = 0; i < TETRIS.Board.getHeight(); i++) {
var piecesInRow = _findPiecesInRow(i);

if (piecesInRow.length === 10) {
_rowsToClear++;
piecesInRow.each(_flagForClear);
};
};
};


function _findPiecesInRow(rowNumber) {
var piecesInRow = $(pieces).filter( function( index, piece) {
return (piece.row === rowNumber && piece.set)
});
return piecesInRow;
};


function _flagForClear(index, piece) {
piece.clearing = true;
};


function removeClearedRows() {
var rowsCleared = _rowsToClear;
_rowsToClear = 0;

pieces.forEach(function(piece, index) {
piece.row += rowsCleared;
})

pieces = pieces.filter( function(el, i) { return (el.clearing === false) } )

return rowsCleared;
};


return {
init: init,
spawnShape: spawnShape,
hasActivePiece: hasActivePiece,
getPieces: function() { return pieces },
stepDown: stepDown,
slideAllLeft: slideAllLeft,
slideAllRight: slideAllRight,
forceAllDown: forceAllDown,
checkCompleteRows: checkCompleteRows,
removeClearedRows: removeClearedRows,
checkGameOver: function() { return gameover }
};

})();
3 changes: 3 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(document).ready(function() {
TETRIS.MainModule.init();
});
Loading