Mister_Stefan

Tetris Game

Aug 23rd, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Tetris Game
  3.     by kseny :D
  4. */
  5.  
  6. var screenSize = Global.GetScreenSize();
  7.  
  8. var boxWidth = 240;
  9. var boxHeight = 400;
  10.  
  11. function createGamePiece(type)
  12. {
  13.     switch(type)
  14.     {
  15.         case 't': return [[0,0,0], [5,5,5], [0,5,0]];
  16.         case 'o': return [[7,7], [7,7]];
  17.         case 'l': return [[0,4,0], [0,4,0], [0,4,4]];
  18.         case 'j': return [[0,1,0], [0,1,0], [1,1,0]];
  19.         case 'i': return [[0,2,0,0], [0,2,0,0], [0,2,0,0], [0,2,0,0]];
  20.         case 's': return [[0,3,3], [3,3,0], [0,0,0]];
  21.         case 'z': return [[6,6,0], [0,6,6], [0,0,0]];
  22.      }
  23. }
  24.  
  25. var tetrominoesColors = [null, [255, 0, 0, 255], [255, 20, 147, 255], [255, 69, 0, 255], [255, 255, 0, 255], [128, 0, 128, 255], [50, 205, 50, 255], [0, 139, 139, 255]];
  26.  
  27. function makeMatrix(w, h)
  28. {
  29.     var matrix = [];
  30.  
  31.     for(var i = 0; i < h; i++)
  32.     {
  33.         matrix[i] = [];
  34.         for(var j = 0; j < w; j++)
  35.             matrix[i][j] = 0;
  36.     }
  37.  
  38.     return matrix;
  39. }
  40.  
  41. var area = makeMatrix(12,20);
  42. var player = { pos: {x : 0, y : 0}, matrix: null, score: 0 };
  43. var move = 1;
  44. var pieces = "ijlostz";
  45. var gameRun = true;
  46.  
  47. function points()
  48. {
  49.     var rowCount = 1;
  50.  
  51.     outer:for(var y = area.length-1; y > 0; --y)
  52.     {
  53.         for(var x = 0; x < area[y].length; ++x)
  54.         {
  55.             if(area[y][x] === 0)
  56.             {
  57.                 continue outer;
  58.             }
  59.         }
  60.  
  61.         var row = area.splice(y, 1)[0];
  62.  
  63.         for(var k = 0; k < row.length; k++)
  64.             row[k] = 0;
  65.  
  66.         area.unshift(row);
  67.         ++y;
  68.  
  69.         player.score += rowCount*100;
  70.         rowCount *= 2;
  71.     }
  72. }
  73.  
  74. function collide(area, player)
  75. {
  76.     var m = player.matrix;
  77.     var o = player.pos;
  78.  
  79.     for(var y = 0; y < m.length; ++y)
  80.     {
  81.         for(var x = 0; x < m[y].length; ++x)
  82.         {
  83.             if(m[y][x] !== 0 && (area[y+o.y] && area[y+o.y][x+o.x]) !==0)
  84.             {
  85.                 return true;
  86.             }
  87.         }
  88.     }
  89.     return false;
  90. };
  91.  
  92.  
  93. function drawMatrix(matrix, offset)
  94. {
  95.     for(var i = 0; i < matrix.length; i++)
  96.     {
  97.         for(var j = 0; j < matrix[i].length; j ++)
  98.         {
  99.             if(matrix[i][j] != 0)
  100.             {
  101.                 Render.FilledRect(screenSize[0]/2 - boxWidth/2 + j * 20 + offset.x * 20, screenSize[1]/2 - boxHeight/2 + i *20 +offset.y * 20, 20, 20, tetrominoesColors[matrix[i][j]]);
  102.                 Render.Rect(screenSize[0]/2 - boxWidth/2 + j * 20 + offset.x * 20, screenSize[1]/2 - boxHeight/2 + i *20 +offset.y * 20, 20, 20, [0, 0, 0, 255]);
  103.             }
  104.         }
  105.     }
  106. };
  107.  
  108.  
  109. function merge(area, player)
  110. {
  111.     for(var y = 0; y < player.matrix.length; y++)  
  112.         for(var x = 0; x < player.matrix[y].length; x++)
  113.             if(player.matrix[y][x]) area[y+player.pos.y][x+player.pos.x] = player.matrix[y][x];
  114. }
  115.  
  116. function rotate(matrix, dir)
  117. {
  118.     for(var y = 0; y < matrix.length; ++y)
  119.     {
  120.         for(var x = 0; x < y; ++x)
  121.         {
  122.             var tmp = matrix[x][y];
  123.             matrix[x][y] = matrix[y][x];
  124.             matrix[y][x] = tmp;
  125.         }
  126.     }
  127.  
  128.     if(dir > 0)
  129.         for(var i = 0; i < matrix.length; i++)
  130.             matrix[i].reverse();
  131.     else
  132.         matrix.reverse();
  133. }
  134.  
  135. function playerReset()
  136. {
  137.     player.matrix = createGamePiece(pieces[Math.floor(Math.random()*pieces.length)]);
  138.     player.pos.y = 0;
  139.     player.pos.x = (Math.floor(area[0].length/2))-(Math.floor(player.matrix[0].length/2));
  140.  
  141.     if(collide(area, player))
  142.     {
  143.         for(var i = 0; i < area.length; i++)
  144.             for(var j = 0; j < area[i].length; j++)
  145.                 area[i][j] = 0;
  146.      
  147.         player.score = 0;
  148.         gameRun = false;
  149.     }
  150. }
  151.  
  152. function playerDrop()
  153. {
  154.     player.pos.y ++;
  155.  
  156.     if(collide(area, player))
  157.     {
  158.         player.pos.y --;
  159.      
  160.         merge(area, player);
  161.         points();
  162.         playerReset();
  163.     }
  164. }
  165.  
  166. function playerMove(dir)
  167. {
  168.     player.pos.x += dir;
  169.  
  170.     if(collide(area,player))
  171.     {
  172.         player.pos.x -= dir;
  173.     }
  174. }
  175.  
  176. function playerRotate(dir)
  177. {
  178.     var pos = player.pos.x;
  179.     var offset = 1;
  180.  
  181.     rotate(player.matrix, dir);
  182.  
  183.     while(collide(area, player))
  184.     {
  185.         player.pos.x += offset;
  186.         offset = -(offset+(offset > 0 ? 1 :-1));
  187.  
  188.         if(offset > player.matrix[0].length)
  189.         {
  190.             rotate(player.matrix,-dir);
  191.             player.pos.x=pos;
  192.             return;
  193.         }
  194.     }
  195. }
  196.  
  197. var lastUpdate = 0;
  198. var lastKeyPress = 0;
  199.  
  200. function onDrawEvent()
  201. {
  202.     if(!UI.GetValue("MISC", "JAVASCRIPT", "Script Items", "Play Tetris Game") || !UI.IsMenuOpen())
  203.         return;
  204.  
  205.     Render.GradientRect(screenSize[0]/2 - boxWidth/2 - 5, screenSize[1]/2 - boxHeight/2 - 34 - 2, boxWidth + 10, 4, 1, [217, 157, 86, 255], [223, 174, 97, 255]);
  206.     Render.FilledRect(screenSize[0]/2 - boxWidth/2 - 5, screenSize[1]/2 - boxHeight/2 - 30 - 2, boxWidth + 10, 25, [44, 48, 55, 255]);
  207.     Render.String(screenSize[0]/2, screenSize[1]/2 - boxHeight/2 - 25 - 2, 1, "Tetris Game" + " [score: "+ player.score +"]", [255, 255, 255, 255]);
  208.  
  209.  
  210.     Render.FilledRect(screenSize[0]/2 - boxWidth/2 - 5, screenSize[1]/2 - boxHeight/2 - 5, boxWidth + 10, boxHeight + 10, [44, 48, 55, 255]);
  211.     Render.Rect(screenSize[0]/2 - boxWidth/2, screenSize[1]/2 - boxHeight/2, boxWidth, boxHeight, [100, 100, 100, 150]);
  212.  
  213.     var gameIsPaused = UI.IsHotkeyActive("MISC", "JAVASCRIPT", "Script Items", "Pause Tetris Game");
  214.  
  215.     if(gameIsPaused)
  216.     {
  217.         Render.String(screenSize[0]/2, screenSize[1]/2 - 5, 1, "The game is paused", [255, 0, 0, 255]);
  218.         return;
  219.     }
  220.  
  221.     if(!gameRun)
  222.     {
  223.         Render.String(screenSize[0]/2, screenSize[1]/2 - 15, 1, "Game over!", [255, 0, 0, 255]);
  224.         Render.String(screenSize[0]/2, screenSize[1]/2, 1, "Press ENTER to start again", [255, 0, 0, 255]);
  225.  
  226.         if(Global.IsKeyPressed(0x0D))
  227.             gameRun = true;
  228.         return;
  229.     }
  230.  
  231.     var realTime = Global.Realtime();
  232.  
  233.     if(realTime - lastKeyPress > 0.1)
  234.     {
  235.         if(Global.IsKeyPressed(0x25))
  236.         {
  237.             playerMove(-move);
  238.         }
  239.         else if(Global.IsKeyPressed(0x27)) {
  240.             playerMove(+move);
  241.         }
  242.         else if(Global.IsKeyPressed(0x26)) {
  243.             playerRotate(-move);
  244.         }
  245.         else if(Global.IsKeyPressed(0x28)) {
  246.             playerDrop();
  247.         }
  248.  
  249.         lastKeyPress = realTime;
  250.     }
  251.  
  252.     if(realTime - lastUpdate > UI.GetValue("MISC", "JAVASCRIPT", "Script Items", "Tetris Speed"))
  253.     {
  254.         playerDrop();
  255.         lastUpdate = realTime;
  256.     }
  257.  
  258.     drawMatrix(area, {x : 0, y:0});
  259.     drawMatrix(player.matrix, player.pos);
  260. }
  261.  
  262. Global.RegisterCallback("Draw", "onDrawEvent");
  263.  
  264. UI.AddCheckbox("Play Tetris Game");
  265. UI.AddHotkey("Pause Tetris Game");
  266. UI.AddSliderFloat("Tetris Speed", 0.05, 0.5);
  267.  
  268. function onScriptInit()
  269. {
  270.     gameRun = true;
  271.     playerReset();
  272. }
  273.  
  274. onScriptInit();
Add Comment
Please, Sign In to add comment