JSFiddle - React, Tailwind, and code Playground
JavaScript
var Game = {
WIDTH: 9,
HEIGHT: 18,
PX: 20,
};
function Shape(x, shapeType) {
this.x = x;
this.y = 0;
this.shapeType = shapeType;
this.populate();
}
Shape.getRandomColor = function() {
return '#' + [Math.random(), Math.random(), Math.random()]
.map(function(r) {
return Math.floor(220 * r);
}).map(function(n) {
var s = n.toString(16);
return s.length == 2 ? s : '0' + s;
}).join('');
};
Shape.Shapes = {
TALL: 0,
SQUARE: 1,
L: 2,
THINGY: 3,
};
Shape.prototype.populate = function() {
var x = this.x;
var dir = Math.random() < .5 ? 1 : -1;
if (this.shapeType == Shape.Shapes.TALL) {
this.cells = [{x: x, y: 3}, {x: x, y: 2}, {x: x, y: 1}, {x: x, y: 0}, ];
} else if (this.shapeType == Shape.Shapes.SQUARE) {
this.cells = [{x: x, y: 1}, {x: x + 1, y: 1}, {x: x, y: 0}, {x: x + 1, y: 0}, ];
} else if (this.shapeType == Shape.Shapes.L) {
this.cells = [{x: x + dir, y: 2}, {x: x, y: 2}, {x: x, y: 1}, {x: x, y: 0}, ];
} else if (this.shapeType == Shape.Shapes.THINGY) {
this.cells = [{x: x, y: 2}, {x: x + dir, y: 1}, {x: x, y: 1}, {x: x, y: 0}, ];
}
this.color = Shape.getRandomColor();
};
Shape.prototype.rotate = function() {
};
function Board() {
this.shapes = [];
// In addition to shapes, keep an index of the board.
this.rows = [];
this.init();
this.addShape();
}
Board.prototype = {
landed: false,
init: function() {
document.body.innerHTML = '';
var outer = document.createElement('div');
for (var i = 0; i < Game.HEIGHT; i++) {
this.rows.push(new Array(Game.WIDTH));
var row = document.createElement('div');
for (var j = 0; j < Game.WIDTH; j++)
row.appendChild(document.createElement('div'));
outer.appendChild(row);
}
this.board = document.body.appendChild(outer);
},
addShape: function() {
var shapeNames = Object.getOwnPropertyNames(Shape.Shapes);
var name = shapeNames[Math.floor(Math.random() *...