Created
June 24, 2017 06:36
-
-
Save gujc71/17a1e93226bbc7828497f7a586547096 to your computer and use it in GitHub Desktop.
Tetris
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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