Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions front/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

// game loop
let interval;
let difficulty = 500;
let animationFrame;
let difficulty = 20;

//game state

Expand Down Expand Up @@ -102,13 +103,17 @@
initializeGame();
global_state = "playing";
console.log("starting game");
interval = setInterval(game_loop, difficulty);

animationFrame = requestAnimationFrame(game_loop);

// interval = setInterval(game_loop, difficulty);
}

function die() {
global_state = "start";
console.log("game over");
clearInterval(interval);
cancelAnimationFrame(animationFrame);
// clearInterval(interval);
}

function updateSnake() {
Expand Down Expand Up @@ -160,23 +165,33 @@
);
}

let lastUpdate = 0;
function game_loop() {
let newsnake = updateSnake();
if (lastUpdate % difficulty === 0) {
let newsnake = updateSnake();

if (hitsSelf(newsnake) || hitsWall(newsnake)) {
die();
return;
}
snake = newsnake;
if (eatBaby()) {
updateBaby();
mouthOpen = true;
score++;
} else {
mouthOpen = false;
snake.position.pop();
snake.position = [...snake.position];
if (hitsSelf(newsnake) || hitsWall(newsnake)) {
die();
return;
}
snake = newsnake;
if (eatBaby()) {
updateBaby();
mouthOpen = true;
score++;
if (score % 5 === 0) {
difficulty = Math.max(1, difficulty - 5);
alert("NOG SNELLER!");
}
} else {
mouthOpen = false;
snake.position.pop();
snake.position = [...snake.position];
}
}

lastUpdate++;
requestAnimationFrame(game_loop);
}
</script>

Expand All @@ -185,6 +200,8 @@
<h1>hello snake lord lets go yeah</h1>
{/if}

<p>Score: {score}</p>

<div class="grid" style="--grid-size: {num_cells}">
{#each cells as cell}
<div
Expand Down