JSFiddle - React, Tailwind, and code Playground
by ilyautkin
HTML
<div id="tetris">
<span id="start-btn">START</span>
</div>
CSS
#tetris {
display: inline-block;
width: 264px;
height: 440px;
border: 2px solid #000;
line-height: 0px;
background: #000;
}
#tetris b, #tetris i {
display: inline-block;
width: 20px;
height: 20px;
background: #000;
border: 1px solid #333;
}
#tetris i {
background: #fff;
border-color: #999;
}
#start-btn {
display: block;
width: 30%;
margin: 70% auto 0;
padding: 20px;
background: #eee;
text-align: center;
cursor: pointer;
}
#start-btn:hover {
background: #fff;
}
JavaScript
var Tetris = {
config: {
pitchID: "tetris",
freeBrick: "<b></b>",
filledBrick: "<i></i>"
},
startBtn: document.getElementById('start-btn'),
pitch: {
width: 12,
height: 20,
bricks: [],
getDom: function() {
return document.getElementById(Tetris.config.pitchID);
}
},
figure: {
coords: [],
go: function() {
if (this.coords.length == 0) {
this.create();
} else {
this.process();
}
},
create: function() {
this.coords = [
[[-1,5],[-1,6]],
[[0,5], [0,6]]
];
},
process: function() {
if (this.touched()) {
this.joinToBricks();
if (!Tetris.checkGameOver()) {
this.destroy();
}
} else {
this.makeStep();
}
},
touched: function() {
var contact = false;
Tetris.each(this.coords, function(i,j){
var figureRow = Tetris.figure.coords[i][j][0];
if (Tetris.pitch.bricks[figureRow + 1] == undefined) {
contact = true;
}
});
if (contact) {
return contact;
}
Tetris.each(this.coords, function(i,j){
var figureRow = Tetris.figure.coords[i][j][0];
var figureCol = Tetris.figure.coords[i][j][1];
if (Tetris.pitch.bricks[figureRow + 1][figureCol]) {
contact = true;
}
});
if (contact) {
return contact;
}
return false;
},
joinToBricks: function() {
Tetris.each(this.coords, function(i,j){
var figureRow = Tetris.figure.coords[i][j][0];
var figureCol = Tetris.figure.coords[i][j][1];
if (figureRow >= 0) {
Tetris.pitch.bricks[figureRow][figureCol] = 1;
}
});
},
destroy: function() {
this.coords = [];
},
makeStep: function() {
Tetris.each(this.coords, function(i,j){
Tetris.figure.coords[i][j][0]++;
});
},
checkCoords: function(row, col) {
var...