let grid = []; let snake = []; let alive = true; let snakeDirection = 0; let AItype = "random"; let gridSize = 20; let gridColumns = 10; let gridRows = 10; let eatenApples = 0; let _textSize = 32; let appleAmount = 1; let gameSpeed = 10; let buttonAddColoums = null; let buttonAddRows = null; let buttonUpgradeAI = null; let buttonAddMoreApples = null; let buttonSpeedUpgrade = null; let SpeedUpgradePrice = 5; let AddMoreApplesPrice = 1; let AddColoumsPrice = 1; let AddRowsPrice = 1; let UpgradeAIPrice = 1; function setup() { buttonAddColoums = createButton("Add coloums"); buttonAddRows = createButton("Add rows"); buttonUpgradeAI = createButton("Upgrade AI"); buttonAddMoreApples = createButton("Add more apples"); buttonSpeedUpgrade = createButton("Increase speed"); createCanvas(windowWidth, windowHeight); background(255, 255, 255); for (let i = 0; i < gridColumns; i++) { grid[i] = []; for (let j = 0; j < gridRows; j++) { grid[i][j] = 0; } } buttonAddColoums.mousePressed(buttonAddColoumsPressed); buttonAddRows.mousePressed(buttonAddRowsPressed); buttonUpgradeAI.mousePressed(buttonUpgradeAIPressed); buttonAddMoreApples.mousePressed(buttonAddMoreApplesPressed); buttonSpeedUpgrade.mousePressed(buttonSpeedUpgradePressed); frameRate(gameSpeed); summonSnake(); summonApple(); drawGrid(); } function buttonSpeedUpgradePressed(){ if (eatenApples < SpeedUpgradePrice) { return; } eatenApples -= SpeedUpgradePrice; gameSpeed += 2; frameRate(gameSpeed); SpeedUpgradePrice += 5; } function buttonAddColoumsPressed(){ if (eatenApples < AddColoumsPrice) { return; } eatenApples -= AddColoumsPrice; gridColumns++; grid.push([]); for (let i = 0; i < gridRows; i++) { grid[grid.length - 1][i] = 0; } } function buttonAddRowsPressed(){ if (eatenApples < AddRowsPrice) { return; } eatenApples -= AddRowsPrice; gridRows++; for (let i = 0; i < gridColumns; i++) { grid[i].push(0); } } function buttonUpgradeAIPressed(){ if (eatenApples < UpgradeAIPrice) { return; } eatenApples -= UpgradeAIPrice; AItype = "smart"; } function buttonAddMoreApplesPressed(){ if (eatenApples < AddMoreApplesPrice) { return; } eatenApples -= AddMoreApplesPrice; summonApple(); appleAmount++; } function summonApple(){ if (appleAmount >= gridColumns * gridRows) { for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[0].length; j++) { if (grid[i][j] == 2) { grid[i][j] = 0; } } } return; } let x = grid.length - floor(random(1, grid.length +1)); let y = grid[0].length - floor(random(1, grid[0].length +1)); if(grid[x][y] == 2 || grid[x][y] == 1){ summonApple();//best practice coding right here return; } grid[x][y] = 1; } function dontMoveBackwards(lastSnakeX, lastSnakeY, currentSnakeX, currentSnakeY, direction){ if (direction == 0) { return lastSnakeX < currentSnakeX; }else if (direction == 1) { return lastSnakeY < currentSnakeY; }else if (direction == 2) { return lastSnakeX > currentSnakeX; }else if (direction == 3) { return lastSnakeY > currentSnakeY; } } function restartGame(){ for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[0].length; j++) { if (grid[i][j] == 2 || grid[i][j] == 1) { grid[i][j] = 0; } } } snake = []; summonSnake(); for (let i = 0; i < appleAmount; i++) { summonApple(); } alive = true; } function runSnake(){ let x = snake[0][0]; let y = snake[0][1]; for (let i = snake.length - 1; i > 0; i--) { snake[i][0] = snake[i - 1][0]; snake[i][1] = snake[i - 1][1]; } snakeAI(); let lastSnakeX = snake[1][0] let lastSnakeY = snake[1][1]; if (dontMoveBackwards(lastSnakeX, lastSnakeY, x, y, snakeDirection)) { snakeDirection = (snakeDirection + 2) % 4; } if (snakeDirection == 0) { snake[0][0] += 1; }else if (snakeDirection == 1) { snake[0][1] += 1; }else if (snakeDirection == 2) { snake[0][0] -= 1; }else if (snakeDirection == 3) { snake[0][1] -= 1; } if (snake[0][1] >= grid[0].length || snake[0][1] < 0 || snake[0][0] >= grid.length || snake[0][0] < 0) { alive = false; return; } if (grid[snake[0][0]][snake[0][1]] == 2) { alive = false; return; } if (grid[snake[0][0]][snake[0][1]] == 1) { snake.push([snake[snake.length - 1][0], snake[snake.length - 1][1]]); summonApple(); eatenApples++; } drawSnakeOnGrid(); } function summonSnake(){ let x = grid.length - floor(random(1, grid.length)); let y = grid[0].length - floor(random(1, grid[0].length)); snake.push([x, y]); snake.push([x - 1, y]); snake.push([x - 2, y]); drawSnakeOnGrid(); } function drawSnakeOnGrid(){ for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[0].length; j++) { if (grid[i][j] == 2) { grid[i][j] = 0; } } } for (let i = 0; i < snake.length; i++) { let x = snake[i][0]; let y = snake[i][1]; // prevent crash if ( x < 0 || y < 0 || x >= grid.length || y >= grid[0].length ){ alive = false; return; } grid[x][y] = 2; } } function drawGrid() { for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[0].length; j++) { if (grid[i][j] == 1) { fill(255, 0, 0); }else if (grid[i][j] == 2) { fill(0, 255, 0); }else { fill(255, 255, 255); } rect(i * gridSize, j * gridSize, gridSize, gridSize); } } } function draw() { if (alive == false){ restartGame(); } background(255); runSnake(); drawGrid(); fill(0, 0, 0); textSize(_textSize); text("₴ " + eatenApples, gridColumns * gridSize + gridSize + _textSize*3, _textSize * 1); text("Apples: " + appleAmount, gridColumns * gridSize + gridSize, _textSize * 2); buttonAddMoreApples.html("Add more apples ₴ " + AddMoreApplesPrice); buttonAddMoreApples.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 1.5); text("AI: " + AItype, gridColumns * gridSize + gridSize, _textSize * 3); buttonUpgradeAI.html("Upgrade AI ₴ " + UpgradeAIPrice); buttonUpgradeAI.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 2.5); text("Coloums: " + gridColumns, gridColumns * gridSize + gridSize, _textSize * 4); buttonAddColoums.html("Add coloums ₴ " + AddColoumsPrice); buttonAddColoums.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 3.5); text("Rows: " + gridRows, gridColumns * gridSize + gridSize, _textSize * 5); buttonAddRows.html("Add rows ₴ " + AddRowsPrice); buttonAddRows.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 4.5); text("Speed: " + gameSpeed, gridColumns * gridSize + gridSize, _textSize * 6); buttonSpeedUpgrade.html("Increase speed ₴ " + SpeedUpgradePrice); buttonSpeedUpgrade.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 5.5); } function mousePressed() { } function snakeAI(){ switch (AItype) { case "random": snakeAIrandom(); break; } } function snakeAIrandom() { snakeDirection = floor(random(0, 4)); } function snakeAIrandomNowallHit(){ snakeDirection = floor(random(0, 4)); }