Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let snake: ScriptDescription = {};
- namespace Snake {
- export let BASE_PATH: Array<string> = ['Boost','Fun','Выйди отсюда', 'Minigame\'s', 'Snake(1920x1080)'];
- export let isEnabledOption = Menu.AddToggle(
- BASE_PATH,
- 'Enable',
- false,
- ).SetNameLocale('ru', 'Включить');
- export let isEnabledValue = isEnabledOption
- .OnChange(state => {
- isEnabledValue = state.newValue;
- })
- .GetValue();
- export let speedSnake: number = 0.15;
- export let speedMenu =
- Menu.AddSlider(BASE_PATH, 'Speed', 0.05, 1, 0.15, 0.05)
- .OnChange(state => speedSnake = state.newValue)
- .GetValue();
- export let font: Font = Renderer.LoadFont('Arial', 100, Enum.FontWeight.NORMAL);
- export let directionDictionary = new Map();
- directionDictionary.set("Down", [1, 0]);
- directionDictionary.set("Up", [-1, 0]);
- directionDictionary.set("Left", [0, -1]);
- directionDictionary.set("Right", [0, 1]);
- export let direction = directionDictionary.get('Right');
- export let changeHeadOnetime: boolean = false;
- export let windowWidth: number = 0;
- export let windowHeight: number = 0;
- export let clickedPlaySnake: boolean = false;
- export let lose: boolean = false;
- export let snakeLen: number = 3;
- export let randomApplePos: number[] = [0, 0];
- export let windowPowerHeight: number = 0.0;
- export let windowPowerWidth: number = 0.0;
- export let snakeMatrix: Array<Array<number>> = [ // 16 x 16
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- ];
- export let lenMatrixW = 16;
- export let lenMatrixH = 16;
- export let lastDirection = directionDictionary.get('Right');
- export let secondSnakeCell = [0, 0];
- export function randomInteger(min, max) {
- let rand = min + Math.random() * (max + 1 - min);
- return Math.floor(rand);
- }
- }
- snake.OnScriptLoad = () => {
- [Snake.windowHeight, Snake.windowWidth] = Renderer.GetScreenSize();
- Snake.windowPowerHeight = Snake.windowHeight / 1920;
- Snake.windowPowerWidth = Snake.windowPowerWidth / 1080;
- };
- snake.OnDraw = () => {
- if (Snake.isEnabledValue) {
- Renderer.SetTopMost(true);
- if (!Snake.clickedPlaySnake && !Snake.lose) {
- Renderer.SetDrawColor(125, 176, 97, 150);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 350, 625, 105);
- Renderer.SetDrawColor(255, 255, 255, 255);
- Renderer.SetDrawColor(125, 176, 97, 255);
- for (let i = 0; i <= 3; i++) {
- Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 + i), Snake.windowWidth / 2 - (350 + i), 625, 105);
- Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 - i), Snake.windowWidth / 2 - (350 - i), 625, 105);
- }
- Renderer.SetDrawColor(255, 255, 255, 255);
- Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 350, "Lets play snake");
- } else if (Snake.lose) {
- Renderer.SetDrawColor(125, 176, 97, 150);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 350, 625, Snake.windowWidth / 2 - 100);
- Renderer.SetDrawColor(125, 176, 97, 255);
- for (let i = 0; i <= 3; i++) {
- Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 + i), Snake.windowWidth / 2 - (350 + i), 625, Snake.windowWidth / 2 - 100);
- Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (350 - i), Snake.windowWidth / 2 - (350 - i), 625, Snake.windowWidth / 2 - 100);
- }
- Renderer.SetDrawColor(255, 255, 255, 255);
- Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 340, Snake.windowWidth / 2 - 200, "You lose");
- Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 350, Snake.windowWidth / 2 - 300, "Lets play snake");
- Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 340, Snake.windowWidth / 2 - 100, `Your Score: ${Snake.snakeLen - 3}`)
- } else if (Snake.clickedPlaySnake && !Snake.lose) {
- Renderer.SetDrawColor(255, 255, 255, 255);
- Renderer.DrawText(Snake.font, Snake.windowHeight / 2 - 150, Snake.windowWidth / 2 - 500, `Score: ${Snake.snakeLen - 3}`);
- Renderer.SetDrawColor(125, 176, 97, 255);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400, Snake.windowWidth / 2 - 400, 800, 800);
- Renderer.SetDrawColor(86, 127, 64, 255);
- for (let i = 0; i <= 3; i++) {
- Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (400 + i), Snake.windowWidth / 2 - (400 + i), 800, 800);
- Renderer.DrawOutlineRect(Snake.windowHeight / 2 - (400 - i), Snake.windowWidth / 2 - (400 - i), 800, 800);
- }
- Renderer.SetDrawColor(255, 255, 255, 255);
- for (let i = 0; i < Snake.lenMatrixH; i++) {
- for (let j = 0; j < Snake.lenMatrixW; j++) {
- if (Snake.snakeMatrix[i][j] === Snake.snakeLen) {
- Snake.snakeMatrix[i][j] = 0;
- }
- if (Snake.snakeMatrix[i][j] === 0) {
- Renderer.SetDrawColor(255, 255, 255, 0);
- Renderer.DrawOutlineRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
- } else if (Snake.snakeMatrix[i][j] == 1) {
- Renderer.SetDrawColor(72, 68, 73, 255);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
- Renderer.SetDrawColor(255, 255, 255, 255);
- if (Snake.direction === Snake.directionDictionary.get('Up')) {
- for (let q = 0; q < 3; q++) {
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) + (q * 10), Snake.windowWidth / 2 - 400 + (i * 50) + (q * 10), 10, 10);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) - (q * 10), Snake.windowWidth / 2 - 400 + (i * 50) + (q * 10), 10, 10);
- }
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 10, 50);
- }
- if (Snake.direction === Snake.directionDictionary.get('Down')) {
- for (let q = 0; q < 3; q++) {
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) + (q * 10), Snake.windowWidth / 2 - 360 + (i * 50) - (q * 10), 10, 10);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50) - (q * 10), Snake.windowWidth / 2 - 360 + (i * 50) - (q * 10), 10, 10);
- }
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 380 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 10, 50);
- }
- if (Snake.direction === Snake.directionDictionary.get('Left')) {
- for (let q = 0; q < 3; q++) {
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - (400 - (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) + (q * 10), 10, 10);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - (400 - (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) - (q * 10), 10, 10);
- }
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50), 50, 10);
- }
- if (Snake.direction === Snake.directionDictionary.get('Right')) {
- for (let q = 0; q < 3; q++) {
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - (360 + (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) - (q * 10), 10, 10);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - (360 + (q * 10)) + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50) + (q * 10), 10, 10);
- }
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 380 + (i * 50), 50, 10);
- }
- } else if (Snake.snakeMatrix[i][j] > 0 && Snake.snakeMatrix[i][j] !== -1) {
- if (Snake.snakeMatrix[i][j] == 2) {
- Snake.secondSnakeCell[0] = i;
- Snake.secondSnakeCell[1] = j;
- }
- Renderer.SetDrawColor(54, 47, 52, 255 - (1 * Snake.snakeMatrix[i][j]));
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
- }
- if (Snake.snakeMatrix[i][j] === -1) {
- Renderer.SetDrawColor(255, 0, 0, 255);
- Renderer.DrawFilledRect(Snake.windowHeight / 2 - 400 + (j * 50), Snake.windowWidth / 2 - 400 + (i * 50), 50, 50);
- Renderer.SetDrawColor(255, 255, 255, 255);
- }
- }
- }
- }
- }
- };
- snake.OnUpdate = () => {
- if(Snake.isEnabledValue) {
- if (Input.IsKeyDown(Enum.ButtonCode.KEY_UP, false) && Snake.direction !== Snake.directionDictionary.get("Down")) {
- Snake.direction = Snake.directionDictionary.get('Up');
- } else if (Input.IsKeyDown(Enum.ButtonCode.KEY_DOWN, false) && Snake.direction !== Snake.directionDictionary.get("Up")) {
- Snake.direction = Snake.directionDictionary.get('Down');
- } else if (Input.IsKeyDown(Enum.ButtonCode.KEY_LEFT, false) && Snake.direction !== Snake.directionDictionary.get("Right")) {
- Snake.direction = Snake.directionDictionary.get("Left");
- } else if (Input.IsKeyDown(Enum.ButtonCode.KEY_RIGHT, false) && Snake.direction !== Snake.directionDictionary.get('Left')) {
- Snake.direction = Snake.directionDictionary.get("Right");
- }
- if (Input.IsKeyDown(Enum.ButtonCode.MOUSE_LEFT) && Input.IsCursorInRect(Snake.windowHeight / 2 - 350, Snake.windowHeight / 2 - 800, 650, 150)) {
- Snake.clickedPlaySnake = true;
- Snake.lose = false;
- Snake.snakeMatrix = [ // 16 x 16
- [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- ];
- Snake.snakeLen = 3;
- Snake.direction = Snake.directionDictionary.get("Right");
- }
- Snake.changeHeadOnetime = false;
- if (Engine.OnceAt(Snake.speedSnake) && !Snake.lose && Snake.clickedPlaySnake) {
- Snake.lastDirection = Snake.direction;
- for (let i = 0; i < Snake.lenMatrixH; i++) {
- for (let j = 0; j < Snake.lenMatrixW; j++) {
- if (Snake.snakeMatrix[i][j] > 1) {
- Snake.snakeMatrix[i][j] += 1;
- }
- if (Snake.snakeMatrix[i][j] === 1 && !Snake.changeHeadOnetime) {
- Snake.snakeMatrix[i][j] += 1;
- let nextSnakePosI = (i + Snake.direction[0]) % Snake.lenMatrixH;
- let nextSnakePosJ = (j + Snake.direction[1]) % Snake.lenMatrixW;
- if (i + Snake.direction[0] < 0) {
- nextSnakePosI = Snake.lenMatrixH - 1;
- } else if (j + Snake.direction[1] < 0) {
- nextSnakePosJ = Snake.lenMatrixW - 1;
- }
- if (Snake.snakeMatrix[nextSnakePosI][nextSnakePosJ] == -1) {
- Snake.snakeLen += 1;
- Snake.randomApplePos[0] = Snake.randomInteger(0, Snake.lenMatrixH - 1);
- Snake.randomApplePos[1] = Snake.randomInteger(0, Snake.lenMatrixW - 1);
- while (Snake.snakeMatrix[Snake.randomApplePos[0]][Snake.randomApplePos[1]] !== 0) {
- Snake.randomApplePos[0] = Snake.randomInteger(0, 15);
- Snake.randomApplePos[0] = Snake.randomInteger(0, 15);
- }
- Snake.snakeMatrix[Snake.randomApplePos[0]][Snake.randomApplePos[1]] = -1;
- } else if (Snake.snakeMatrix[nextSnakePosI][nextSnakePosJ] !== 0) {
- Snake.clickedPlaySnake = false;
- Snake.lose = true;
- }
- Snake.snakeMatrix[nextSnakePosI][nextSnakePosJ] = 1;
- Snake.changeHeadOnetime = true;
- }
- }
- }
- }
- }
- };
- RegisterScript(snake);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement