Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let snake: ScriptDescription = {};
  2.  
  3. namespace Snake {
  4.     export let BASE_PATH: Array<string> = ['Boost','Fun','Выйди отсюда', 'Minigame\'s', 'Snake(1920x1080)'];
  5.     export let isEnabledOption = Menu.AddToggle(
  6.         BASE_PATH,
  7.         'Enable',
  8.         false,
  9.     ).SetNameLocale('ru', 'Включить');
  10.     export let isEnabledValue = isEnabledOption
  11.         .OnChange(state => {
  12.             isEnabledValue = state.newValue;
  13.         })
  14.         .GetValue();
  15.     export let speedSnake: number = 0.15;
  16.     export let speedMenu =
  17.         Menu.AddSlider(BASE_PATH, 'Speed', 0.05, 1, 0.15, 0.05)
  18.             .OnChange(state => speedSnake = state.newValue)
  19.             .GetValue();
  20.  
  21.     export let font: Font = Renderer.LoadFont('Arial', 100, Enum.FontWeight.NORMAL);
  22.  
  23.     export let directionDictionary = new Map();
  24.     directionDictionary.set("Down", [1, 0]);
  25.     directionDictionary.set("Up", [-1, 0]);
  26.     directionDictionary.set("Left", [0, -1]);
  27.     directionDictionary.set("Right", [0, 1]);
  28.  
  29.     export let direction = directionDictionary.get('Right');
  30.  
  31.     export let changeHeadOnetime: boolean = false;
  32.  
  33.     export let windowWidth: number = 0;
  34.     export let windowHeight: number = 0;
  35.  
  36.     export let clickedPlaySnake: boolean = false;
  37.     export let lose: boolean = false;
  38.  
  39.     export let snakeLen: number = 3;
  40.  
  41.     export let randomApplePos: number[] = [0, 0];
  42.  
  43.     export let windowPowerHeight: number = 0.0;
  44.     export let windowPowerWidth: number = 0.0;
  45.  
  46.     export let snakeMatrix: Array<Array<number>> = [ // 16 x 16
  47.         [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  48.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  49.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  50.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  51.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  52.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  53.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  54.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  55.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  56.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  57.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  58.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  59.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  60.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  61.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0],
  62.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  63.     ];
  64.  
  65.     export let lenMatrixW = 16;
  66.     export let lenMatrixH = 16;
  67.  
  68.     export let lastDirection = directionDictionary.get('Right');
  69.     export let secondSnakeCell = [0, 0];
  70.  
  71.     export function randomInteger(min, max) {
  72.         let rand = min + Math.random() * (max + 1 - min);
  73.         return Math.floor(rand);
  74.     }
  75. }
  76. snake.OnScriptLoad = () => {
  77.     [Snake.windowHeight, Snake.windowWidth] = Renderer.GetScreenSize();
  78.     Snake.windowPowerHeight = Snake.windowHeight / 1920;
  79.     Snake.windowPowerWidth = Snake.windowPowerWidth / 1080;
  80. };
  81.  
  82. snake.OnDraw = () => {
  83.     if (Snake.isEnabledValue) {
  84.         Renderer.SetTopMost(true);
  85.         if (!Snake.clickedPlaySnake && !Snake.lose) {
  86.             Renderer.SetDrawColor(125, 176, 97, 150);
  87.             Renderer.DrawFilledRect(Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 350, 625, 105);
  88.             Renderer.SetDrawColor(255, 255, 255, 255);
  89.             Renderer.SetDrawColor(125, 176, 97, 255);
  90.             for (let i = 0; i <= 3; i++) {
  91.                 Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 + i), Snake.windowWidth / 2 - (350 + i), 625, 105);
  92.                 Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 - i), Snake.windowWidth / 2 - (350 - i), 625, 105);
  93.             }
  94.             Renderer.SetDrawColor(255, 255, 255, 255);
  95.             Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 350, "Lets play snake");
  96.         } else if (Snake.lose) {
  97.             Renderer.SetDrawColor(125, 176, 97, 150);
  98.             Renderer.DrawFilledRect(Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 350, 625, Snake.windowWidth / 2 - 100);
  99.             Renderer.SetDrawColor(125, 176, 97, 255);
  100.             for (let i = 0; i <= 3; i++) {
  101.                 Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 + i), Snake.windowWidth / 2 - (350 + i), 625, Snake.windowWidth / 2 - 100);
  102.                 Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 - i), Snake.windowWidth / 2 - (350 - i), 625, Snake.windowWidth / 2 - 100);
  103.             }
  104.             Renderer.SetDrawColor(255, 255, 255, 255);
  105.             Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 340, Snake.windowWidth / 2 - 200, "You lose");
  106.             Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 300, "Lets play snake");
  107.             Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 340, Snake.windowWidth / 2 - 100, `Your Score: ${Snake.snakeLen - 3}`)
  108.         } else if (Snake.clickedPlaySnake && !Snake.lose) {
  109.             Renderer.SetDrawColor(255, 255, 255, 255);
  110.             Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 150, Snake.windowWidth / 2 - 500, `Score: ${Snake.snakeLen - 3}`);
  111.             Renderer.SetDrawColor(125, 176, 97, 255);
  112.             Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400, Snake.windowWidth / 2 - 400, 800, 800);
  113.             Renderer.SetDrawColor(86, 127, 64, 255);
  114.             for (let i = 0; i <= 3; i++) {
  115.                 Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (400 + i), Snake.windowWidth / 2 - (400 + i), 800, 800);
  116.                 Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (400 - i), Snake.windowWidth / 2 - (400 - i), 800, 800);
  117.             }
  118.             Renderer.SetDrawColor(255, 255, 255, 255);
  119.             for (let i = 0; i < Snake.lenMatrixH; i++) {
  120.                 for (let j = 0; j < Snake.lenMatrixW; j++) {
  121.                     if (Snake.snakeMatrix[i][j] === Snake.snakeLen) {
  122.                         Snake.snakeMatrix[i][j] = 0;
  123.                     }
  124.                     if (Snake.snakeMatrix[i][j] === 0) {
  125.                         Renderer.SetDrawColor(255, 255, 255, 0);
  126.                         Renderer.DrawOutlineRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
  127.                     } else if (Snake.snakeMatrix[i][j] == 1) {
  128.                         Renderer.SetDrawColor(72, 68, 73, 255);
  129.                         Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
  130.                         Renderer.SetDrawColor(255, 255, 255, 255);
  131.                         if (Snake.direction === Snake.directionDictionary.get('Up')) {
  132.                             for (let q = 0; q < 3; q++) {
  133.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) + (q * 10), Snake.windowWidth / 2 - 400 + (i * 50) + (q * 10), 10, 10);
  134.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) - (q * 10), Snake.windowWidth / 2 - 400 + (i * 50) + (q * 10), 10, 10);
  135.                             }
  136.                             Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 10, 50);
  137.                         }
  138.                         if (Snake.direction === Snake.directionDictionary.get('Down')) {
  139.                             for (let q = 0; q < 3; q++) {
  140.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) + (q * 10), Snake.windowWidth / 2 - 360 + (i * 50) - (q * 10), 10, 10);
  141.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) - (q * 10), Snake.windowWidth / 2 - 360 + (i * 50) - (q * 10), 10, 10);
  142.                             }
  143.                             Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 10, 50);
  144.                         }
  145.                         if (Snake.direction === Snake.directionDictionary.get('Left')) {
  146.                             for (let q = 0; q < 3; q++) {
  147.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - (400 - (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) + (q * 10), 10, 10);
  148.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - (400 - (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) - (q * 10), 10, 10);
  149.                             }
  150.                             Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50), 50, 10);
  151.                         }
  152.                         if (Snake.direction === Snake.directionDictionary.get('Right')) {
  153.                             for (let q = 0; q < 3; q++) {
  154.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - (360 + (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) - (q * 10), 10, 10);
  155.                                 Renderer.DrawFilledRect(Snake.windowHeight / 2 - (360 + (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) + (q * 10), 10, 10);
  156.                             }
  157.                             Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50), 50, 10);
  158.                         }
  159.                     } else if (Snake.snakeMatrix[i][j] > 0 && Snake.snakeMatrix[i][j] !== -1) {
  160.                         if (Snake.snakeMatrix[i][j] == 2) {
  161.                             Snake.secondSnakeCell[0] = i;
  162.                             Snake.secondSnakeCell[1] = j;
  163.                         }
  164.                         Renderer.SetDrawColor(54, 47, 52, 255 - (1 * Snake.snakeMatrix[i][j]));
  165.                         Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
  166.                     }
  167.                     if (Snake.snakeMatrix[i][j] === -1) {
  168.                         Renderer.SetDrawColor(255, 0, 0, 255);
  169.                         Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
  170.                         Renderer.SetDrawColor(255, 255, 255, 255);
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.     }
  176. };
  177. snake.OnUpdate = () => {
  178.     if(Snake.isEnabledValue) {
  179.         if (Input.IsKeyDown(Enum.ButtonCode.KEY_UP, false) && Snake.direction !== Snake.directionDictionary.get("Down")) {
  180.             Snake.direction = Snake.directionDictionary.get('Up');
  181.         } else if (Input.IsKeyDown(Enum.ButtonCode.KEY_DOWN, false) && Snake.direction !== Snake.directionDictionary.get("Up")) {
  182.             Snake.direction = Snake.directionDictionary.get('Down');
  183.         } else if (Input.IsKeyDown(Enum.ButtonCode.KEY_LEFT, false) && Snake.direction !== Snake.directionDictionary.get("Right")) {
  184.             Snake.direction = Snake.directionDictionary.get("Left");
  185.         } else if (Input.IsKeyDown(Enum.ButtonCode.KEY_RIGHT, false) && Snake.direction !== Snake.directionDictionary.get('Left')) {
  186.             Snake.direction = Snake.directionDictionary.get("Right");
  187.         }
  188.         if (Input.IsKeyDown(Enum.ButtonCode.MOUSE_LEFT) && Input.IsCursorInRect(Snake.windowHeight / 2 - 350, Snake.windowHeight / 2 - 800, 650, 150)) {
  189.             Snake.clickedPlaySnake = true;
  190.             Snake.lose = false;
  191.             Snake.snakeMatrix = [ // 16 x 16
  192.                 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  193.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  194.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  195.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  196.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  197.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  198.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  199.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  200.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  201.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  202.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  203.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  204.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  205.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0],
  206.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  207.                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  208.             ];
  209.             Snake.snakeLen = 3;
  210.             Snake.direction = Snake.directionDictionary.get("Right");
  211.         }
  212.         Snake.changeHeadOnetime = false;
  213.         if (Engine.OnceAt(Snake.speedSnake) && !Snake.lose && Snake.clickedPlaySnake) {
  214.             Snake.lastDirection = Snake.direction;
  215.             for (let i = 0; i < Snake.lenMatrixH; i++) {
  216.                 for (let j = 0; j < Snake.lenMatrixW; j++) {
  217.                     if (Snake.snakeMatrix[i][j] > 1) {
  218.                         Snake.snakeMatrix[i][j] += 1;
  219.                     }
  220.                     if (Snake.snakeMatrix[i][j] === 1 && !Snake.changeHeadOnetime) {
  221.                         Snake.snakeMatrix[i][j] += 1;
  222.                         let nextSnakePosI = (i + Snake.direction[0]) % Snake.lenMatrixH;
  223.                         let nextSnakePosJ = (j + Snake.direction[1]) % Snake.lenMatrixW;
  224.                         if (i + Snake.direction[0] < 0) {
  225.                             nextSnakePosI = Snake.lenMatrixH - 1;
  226.                         } else if (j + Snake.direction[1] < 0) {
  227.                             nextSnakePosJ = Snake.lenMatrixW - 1;
  228.                         }
  229.                         if (Snake.snakeMatrix[nextSnakePosI][nextSnakePosJ] == -1) {
  230.                             Snake.snakeLen += 1;
  231.                             Snake.randomApplePos[0] = Snake.randomInteger(0, Snake.lenMatrixH - 1);
  232.                             Snake.randomApplePos[1] = Snake.randomInteger(0, Snake.lenMatrixW - 1);
  233.                             while (Snake.snakeMatrix[Snake.randomApplePos[0]][Snake.randomApplePos[1]] !== 0) {
  234.                                 Snake.randomApplePos[0] = Snake.randomInteger(0, 15);
  235.                                 Snake.randomApplePos[0] = Snake.randomInteger(0, 15);
  236.                             }
  237.                             Snake.snakeMatrix[Snake.randomApplePos[0]][Snake.randomApplePos[1]] = -1;
  238.                         } else if (Snake.snakeMatrix[nextSnakePosI][nextSnakePosJ] !== 0) {
  239.                             Snake.clickedPlaySnake = false;
  240.                             Snake.lose = true;
  241.                         }
  242.                         Snake.snakeMatrix[nextSnakePosI][nextSnakePosJ] = 1;
  243.                         Snake.changeHeadOnetime = true;
  244.                     }
  245.                 }
  246.             }
  247.         }
  248.     }
  249. };
  250. RegisterScript(snake);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement