diff --git a/sketches/snake.js b/sketches/snake.js index 3c6cd7c..bb2acd0 100644 --- a/sketches/snake.js +++ b/sketches/snake.js @@ -101,6 +101,28 @@ function dontMoveBackwards(lastSnakeX, lastSnakeY, currentSnakeX, 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]; @@ -156,9 +178,23 @@ function drawSnakeOnGrid(){ } } } + 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; } } @@ -180,24 +216,36 @@ function drawGrid() { function draw() { background(255); - if (alive = false){ + + if (alive == false){ + restartGame(); 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.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); }