Skip to content

Instantly share code, notes, and snippets.

@gujc71
Created June 24, 2017 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gujc71/17a1e93226bbc7828497f7a586547096 to your computer and use it in GitHub Desktop.
Save gujc71/17a1e93226bbc7828497f7a586547096 to your computer and use it in GitHub Desktop.
Tetris
<canvas id="tetrisCanvas" width="200" height="400"></canvas>
<script>
// just one falling
var canvas = document.getElementById('tetrisCanvas');
var ctx = canvas.getContext('2d');
var shapes = [
[[1, 1, 0, 0], // 'O'
[1, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 1, 0], // 'I'
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0]],
[[1, 1, 1, 0], // 'T'
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 1, 1, 0], // 'S'
[1, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[1, 1, 0, 0], // 'Z'
[0, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 1, 0, 0], // 'J'
[0, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 0, 0]],
[[1, 0, 0, 0], // 'L'
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 0, 0, 0]]
];
var shapeSize = [2,4,3,3,3,3,3];
var curShape = shapes[0], curShapeType = 0;
var sPos = {x:0, y:0};
var intervalHandler = setInterval(
function () {
draw();
}, 400
);
function draw() {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 200, 400);
ctx.rect(0, 0, 200, 400);
ctx.strokeStyle="blue";
ctx.stroke();
ctx.fillStyle = 'black';
for (var y = 0; y < 4; y++) {
for (var x = 0; x < 4; x++) {
if (curShape[y][x]) {
ctx.fillRect((sPos.x+x) * 20, (sPos.y+y) * 20, 19, 19);
}
}
}
if (sPos.y++ === 18){
clearInterval(intervalHandler);
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment