First commit
This commit is contained in:
		
							
								
								
									
										9
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
			
		||||
<html>
 | 
			
		||||
    <body>
 | 
			
		||||
        <title>JDG :: Tetris JS</title>
 | 
			
		||||
        <script src="tetris.js"></script>
 | 
			
		||||
    </body>
 | 
			
		||||
    <head>
 | 
			
		||||
        <div id="app"></div>
 | 
			
		||||
    </head>
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										126
									
								
								tetris.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								tetris.js
									
									
									
									
									
										Normal file
									
								
							@ -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<p.t[p.r])
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function getNextShape() {
 | 
			
		||||
        return Math.floor(Math.random() * tetrimonios.length);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function setShape(p) {
 | 
			
		||||
        let tNum = Math.floor(Math.random() * tetrimonios.length);
 | 
			
		||||
        if ( !this.next_t ) {
 | 
			
		||||
            this.next_t = tetrimonios[ tNum ];
 | 
			
		||||
            setShape(p);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        p.t = this.next_t;
 | 
			
		||||
        p.x = 0;
 | 
			
		||||
        p.y = grid_x/2 - 1;
 | 
			
		||||
        p.r = 0;
 | 
			
		||||
        
 | 
			
		||||
        this.next_t = tetrimonios[ tNum ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function newGame() {
 | 
			
		||||
        this.score = 0;
 | 
			
		||||
        this.speed = 1;
 | 
			
		||||
        this.next_t = null;
 | 
			
		||||
        setShape( this.t );
 | 
			
		||||
        this.timer = setInterval(moveDown, 1000);
 | 
			
		||||
    }
 | 
			
		||||
    newGame();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
document.addEventListener('DOMContentLoaded', tetris );
 | 
			
		||||
		Reference in New Issue
	
	Block a user