From 8004c18fcdab74b4b6ce6550d6874408acb2ebab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Hallenbarter?= Date: Wed, 13 May 2026 17:21:31 +0200 Subject: [PATCH] added speed upgrade --- sketches/snake.js | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/sketches/snake.js b/sketches/snake.js index bb2acd0..d69b723 100644 --- a/sketches/snake.js +++ b/sketches/snake.js @@ -4,15 +4,18 @@ let alive = true; let snakeDirection = 0; let AItype = "random"; let gridSize = 20; -let gridColumns = 20; -let gridRows = 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; @@ -23,6 +26,7 @@ function setup() { 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++) { @@ -35,10 +39,28 @@ function setup() { 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; @@ -78,8 +100,8 @@ function buttonAddMoreApplesPressed(){ } function summonApple(){ - let x = grid.length - floor(random(1, grid.length)); - let y = grid[0].length - floor(random(1, grid[0].length)); + 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; @@ -215,13 +237,13 @@ function drawGrid() { } function draw() { - background(255); if (alive == false){ restartGame(); - return; } + background(255); + runSnake(); drawGrid(); @@ -229,6 +251,7 @@ function draw() { 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); @@ -248,6 +271,11 @@ function draw() { 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() {