Advertisement
Transformator

tetirs

Apr 21st, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML>
  2. <head>
  3.     <meta charset="utf-8">
  4.     <title>Tetris</title>
  5.  
  6.     <style>
  7.     canvas {
  8.         margin: auto;
  9.         position: absolute;
  10.  
  11.         top: 0;
  12.         bottom: 0;
  13.         left: 0;
  14.         right: 0;
  15.  
  16.         border: 2px solid #222;
  17.     }
  18.     </style>
  19. </head>
  20.  
  21. <body>
  22. <script>
  23.  
  24. // ^ y-achse
  25. // |
  26. // |
  27. // |
  28. // |
  29. // |
  30. // |
  31. // x--------------- >  x-achse
  32.  
  33. var WIDTH, HEIGHT;
  34.  
  35. var SECTION = 20;
  36. var SPACE = 2;
  37.  
  38. var gridpoints_x = 11;
  39. var gridpoints_y = 15;
  40.  
  41. var canvas, ctx, keystate, cooldown;
  42.  
  43. var DownArrow=40, RightArrow=39, LeftArrow=37, SKey=83, AKey=65, DKey=68;
  44.  
  45. var game = {
  46.     // int, int        boolean, string, array
  47.     // [[x, y] ...] = [used, color]
  48.     grid: null,
  49.  
  50.     object: {
  51.         x: null,
  52.         y: null,
  53.  
  54.         color: null,
  55.  
  56.         grid: null,
  57.  
  58.         draw: function() {
  59.             ctx.fillStyle = this.color;
  60.  
  61.             for(var i=0; i<gridpoints_x; i++) {
  62.                 for(var j=0; j<gridpoints_y; j++) {
  63.                     for(var h=0; h<this.grid.length; h++) {
  64.                         if(this.grid[h][0]+this.x == i && this.grid[h][1]+this.y == j) {
  65.                             ctx.fillRect(SECTION*i + SPACE*i, SECTION*j + SPACE*j,SECTION, SECTION);
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.         },
  71.         set_object: function() {
  72.             var obj = Math.floor((Math.random() * 6) + 1);
  73.  
  74.             this.x = Math.floor(gridpoints_x/2);
  75.             this.y = 1;
  76.  
  77.             switch(obj) {
  78.                 case(1):
  79.                     this.color = "#0F0";
  80.                     this.grid = [[0,  0], [0, -1], [-1, 0], [1,  0]];
  81.                     break;
  82.                 case(2):
  83.                     this.color = "#F00";
  84.                     this.grid = [[-1, -1], [-1, 0], [-1, 1], [0, 1]];
  85.                     break;
  86.                 case(3):
  87.                     this.color = "#00F";
  88.                     this.grid = [[-1, -1], [0, -1], [0, 0], [1, 0]];
  89.                     break;
  90.                 case(4):
  91.                     this.color = "#FF0";
  92.                     this.grid = [[-1, -1], [0, -1], [0, 0], [-1, 0]];
  93.                     break;
  94.                 case(5):
  95.                     this.color = "#0FF";
  96.                     this.grid = [[-1, -1], [0, -1], [-1, 0], [0, 0], [1, 0], [0, 1], [1, 1], [-1, 1], [1, -1]];
  97.                     break;
  98.                 case(6):
  99.                     this.color = "#DF7401";
  100.                     this.grid = [[1, -1], [1, 0], [1, 1], [0, 1]];
  101.             }
  102.         }
  103.     },
  104.     generate_grid: function() {
  105.         this.grid = [];
  106.  
  107.         for(var i=0; i<gridpoints_x; i++) {
  108.             this.grid[i] = [];
  109.         }
  110.  
  111.         for(var i=0; i<gridpoints_x; i++) {
  112.             for(var j=0; j<gridpoints_y; j++) {
  113.                 this.grid[i][j] = [false, "#444"];
  114.             }
  115.         }
  116.     },
  117.     draw: function() {
  118.         for(var i=0; i<gridpoints_x; i++) {
  119.             for(var j=0; j<gridpoints_y; j++) {
  120.                 ctx.fillStyle = this.grid[i][j][1];
  121.                 ctx.fillRect(SECTION*i + SPACE*i, SECTION*j + SPACE*j,SECTION, SECTION);
  122.             }
  123.         }
  124.     },
  125.     update: function() {
  126.         if(cooldown > 40) {
  127.             for(var i=0; i<this.object.grid.length; i++) {
  128.                 var x = this.object.grid[i][0]+this.object.x;
  129.                 var y = this.object.grid[i][1]+this.object.y;
  130.                
  131.                 if(typeof this.grid[x][y+1] === "undefined" || this.grid[x][y+1][0] == true) {
  132.                     this.grid[x][y] = [];
  133.                     this.grid[x][y][0] = true;
  134.                     this.grid[x][y][1] = this.object.color;
  135.  
  136.                     this.object.set_object();
  137.                 } else {
  138.                     this.object.y++;
  139.                 }
  140.             }
  141.             cooldown = 0;
  142.         } else {
  143.             cooldown++;
  144.         }
  145.     }
  146. }
  147.  
  148. function main() {
  149.     init();
  150.  
  151.     canvas = document.createElement("canvas");
  152.     canvas.width = WIDTH;
  153.     canvas.height = HEIGHT;
  154.     ctx = canvas.getContext("2d");
  155.     document.body.appendChild(canvas);
  156.  
  157.     document.addEventListener("keydown", function(evt) {
  158.         keystate[evt.keyCode] = true;
  159.     });
  160.  
  161.     var loop = function() {
  162.         update();
  163.         draw();
  164.  
  165.         requestAnimationFrame(loop);
  166.     }
  167.  
  168.     requestAnimationFrame(loop);
  169. }
  170.  
  171. function init() {
  172.     keystate = {};
  173.  
  174.     cooldown = 0;
  175.  
  176.     WIDTH  = gridpoints_x*SECTION + (gridpoints_x-2)*SPACE;
  177.     HEIGHT = gridpoints_y*SECTION + (gridpoints_y-2)*SPACE;
  178.  
  179.     game.generate_grid();
  180.  
  181.     game.object.set_object(5);
  182. }
  183.  
  184. function update() {
  185.     game.update();
  186. }
  187.  
  188. function draw() {
  189.     ctx.fillStyle = "#222";
  190.     ctx.fillRect(0, 0, WIDTH, HEIGHT);
  191.  
  192.     game.draw();
  193.     game.object.draw();
  194. }
  195.  
  196. main();
  197. </script>
  198. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement