👽️ Checkpoint

./script.js:9517054/8047
This commit is contained in:
Glitch (glitch-hello-website) 2022-03-04 17:25:24 +00:00
parent 591c7df84b
commit 0aeefdb5bc

View file

@ -76,7 +76,7 @@ function update() {
const x = Math.floor(Math.random() * game.size);
const y = Math.floor(Math.random() * game.size);
game.apple = [x, y];
if (!inSnek(game.apple[0], game.apple[1])) break;
}
} else {
@ -92,7 +92,7 @@ function update() {
if (l[0] < 0 || l[0] >= game.size || l[1] < 0 || l[1] >= game.size) {
reset();
}
// uncomment to wrap
// if (next[0] < 0) next[0] = game.size - 1;
// if (next[0] >= game.size) next[0] = 0;
@ -104,7 +104,7 @@ function update() {
// update score display
if (game.snek.dir !== null) {
const score = b.length - 5;
if(score > game.highscore) game.highscore = score; // update high score if necessary
if (score > game.highscore) game.highscore = score; // update high score if necessary
info.innerText = `${score < 15 ? "length" : "longth"}: ${score}`;
highscore.innerText = `highscore: ${game.highscore}\n`;
}
@ -129,8 +129,11 @@ function update() {
// change direction on keypress
document.addEventListener("keydown", (e) => {
if (e.ctrlKey || e.shiftKey) return;
const s = game.snek;
if (s.dir !== s.newdir) update(); // avoid snek from crashing into itself
e.preventDefault();
switch (e.key) {
case "ArrowLeft":
return s.dir !== "right" ? (s.newdir = "left") : 0;