commit 7228798f0e00860040191b31cb551768f6075bea Author: JDG Date: Mon Oct 18 23:49:13 2021 +0200 First commit diff --git a/index.html b/index.html new file mode 100644 index 0000000..9cd78f2 --- /dev/null +++ b/index.html @@ -0,0 +1,9 @@ + + + JDG :: Tetris JS + + + +
+ + \ No newline at end of file diff --git a/tetris.js b/tetris.js new file mode 100644 index 0000000..57cd4e5 --- /dev/null +++ b/tetris.js @@ -0,0 +1,126 @@ +"use strict"; + +let tetris = function () { + const grid_x = 10; + const grid_y = 30; + const tetrimonios = [ + /*'I'*/{ mR:2, + r:[ + [ [0,0],[0,1],[0,2],[0,3] ], + [ [0,0],[1,0],[2,0],[3,0] ] + ] + }, + /*'O':*/{ mR:1, + r:[ + [ [0,0],[0,1],[1,0],[1,1] ] + ] + }, + /*'L':*/{ mR:4, + r:[ + [ [0,0],[0,1],[0,2],[1,2] ], + [ [0,0],[1,0],[2,0],[0,1] ], + [ [0,0],[0,1],[1,1],[1,2] ], + [ [0,2],[1,2],[1,1],[1,0] ] + ] + }, + /*'L2':*/{ mR:4, + r:[ + [ [1,0],[1,1],[1,2],[0,2] ], + [ [0,0],[0,1],[1,1],[1,2] ], + [ [1,0],[0,0],[0,1],[0,2] ], + [ [0,0],[1,0],[2,0],[2,1] ] + ] + }, + /*'Z':*/{ mR:2, + r:[ + [ [0,0],[1,0],[1,1],[2,1] ], + [ [1,0],[1,1],[0,1],[0,2] ] + ] + }, + /*'Z2':*/{ mR:2, + r:[ + [ [0,1],[1,1],[1,0],[2,0] ], + [ [0,0],[0,1],[1,1],[1,2] ] + ] + } + ]; + + let timer; + let score, speed; + let next_t; + + let b=[grid_x][grid_y]; + let t = {t:null,x:0,y:0,r:0}; + + + function control(e) { + clean(); + switch(e.key) { + case 'ArrowUp': moveUp(this.t); break; + case 'ArrowDown': moveDown(this.t); break; + case 'ArrowLeft': moveLeft(this.t); break; + case 'ArrowRight': moveRight(this.t);break; + } + draw(); + } + document.addEventListener('keydown', control); + + function moveUp(p) { + let r = p.r; + p.r = (p.r++)% p.t.mR; + if ( collision(p) ) p.r = r; + } + + function moveDown(p) { + p.y++; + if ( collision(p) ) glue(p); + } + + function moveLeft(p) { + p.x--; + if ( collision(p) ) p.x++; + } + + function moveRight(p) { + p.x++; + if ( collision(p) ) p.x--; + } + + function collision(p) { + let x,y; + for(x=0;x