diff --git a/sketches/snake.js b/sketches/snake.js new file mode 100644 index 0000000..ae89cbb --- /dev/null +++ b/sketches/snake.js @@ -0,0 +1,186 @@ +let grid = []; +let snake = []; +let alive = true; +let snakeDirection = 0; +let AItype = "random"; +let gridSize = 20; +let gridColumns = 20; +let gridRows = 20; +let eatenApples = 0; +let _textSize = 32; +let appleAmount = 1; +let buttonAddColoums = null; +let buttonAddRows = null; +let buttonUpgradeAI = null; +let buttonAddMoreApples = null; +let AddMoreApplesPrice = 1; + +function setup() { + buttonAddColoums = createButton("Add coloums"); + buttonAddRows = createButton("Add rows"); + buttonUpgradeAI = createButton("Upgrade AI"); + buttonAddMoreApples = createButton("Add more apples"); + 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; + } + } + buttonAddMoreApples.mousePressed(buttonAddMoreApplesPressed); + summonSnake(); + summonApple(); + drawGrid(); +} + +function buttonAddMoreApplesPressed(){ + if (eatenApples < AddMoreApplesPrice) { + return; + } + eatenApples -= AddMoreApplesPrice; + summonApple(); + appleAmount++; +} + +function summonApple(){ + let x = grid.length - floor(random(1, grid.length)); + let y = grid[0].length - floor(random(1, grid[0].length)); + 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 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]; + 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() { + background(255); + if (alive = false){ + return; + } + 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.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 2.5); + text("Coloums: " + gridColumns, gridColumns * gridSize + gridSize, _textSize * 4); + buttonAddColoums.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 3.5); + text("Rows: " + gridRows, gridColumns * gridSize + gridSize, _textSize * 5); + buttonAddRows.position(gridColumns * gridSize + gridSize + _textSize*6, _textSize * 4.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)); +} \ No newline at end of file