cool i borked it even further

This commit is contained in:
sageTheDM 2025-02-25 11:31:44 +01:00
parent 253acb460d
commit 849ec9f4bc
9 changed files with 113 additions and 125 deletions

BIN
secret/snake/audio/dead.mp3 Normal file

Binary file not shown.

BIN
secret/snake/audio/down.mp3 Normal file

Binary file not shown.

BIN
secret/snake/audio/eat.mp3 Normal file

Binary file not shown.

BIN
secret/snake/audio/left.mp3 Normal file

Binary file not shown.

Binary file not shown.

BIN
secret/snake/audio/up.mp3 Normal file

Binary file not shown.

View file

@ -1,150 +1,138 @@
"use strict"; "use strict";
const upButton = document.getElementById("up"); document.addEventListener("DOMContentLoaded", () => {
const downButton = document.getElementById("down"); class SnakeGame {
const leftButton = document.getElementById("left"); constructor() {
const rightButton = document.getElementById("right"); this.cvs = document.getElementById("game");
const startButton = document.getElementById("start"); this.ctx = this.cvs.getContext("2d");
const grid = document.createElement("div"); this.startBtn = document.getElementById("start");
grid.id = "grid"; this.description = document.getElementById("description");
document.querySelector(".game").appendChild(grid); this.box = 32;
let snake = [{ x: 5, y: 5 }]; this.ground = new Image();
let apple = { x: 8, y: 5 }; this.ground.src = "img/ground.png";
let direction = { x: 0, y: 0 };
let gameInterval = null;
let isGameRunning = false;
const gridSize = 10;
function initGame() { this.foodImg = new Image();
grid.style.display = "grid"; this.foodImg.src = "img/food.png";
grid.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
grid.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
grid.style.width = "350px";
grid.style.height = "350px";
document.getElementById("title").style.display = "none";
document.getElementById("description").style.display = "none";
isGameRunning = true; this.sounds = {
snake = [{ x: 5, y: 5 }]; dead: new Audio("audio/dead.mp3"),
apple = spawnApple(); eat: new Audio("audio/eat.mp3"),
direction = { x: 0, y: 0 }; up: new Audio("audio/up.mp3"),
right: new Audio("audio/right.mp3"),
clearInterval(gameInterval); left: new Audio("audio/left.mp3"),
gameInterval = setInterval(gameLoop, 200); down: new Audio("audio/down.mp3"),
render();
}
function spawnApple() {
let newApple;
do {
newApple = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize),
}; };
} while (
snake.some( this.snake = [{ x: 9 * this.box, y: 10 * this.box }];
(segment) => segment.x === newApple.x && segment.y === newApple.y this.food = this.getRandomFoodPosition();
) this.score = 0;
this.direction = null;
this.game = null;
document.addEventListener("keydown", (event) =>
this.handleKeyPress(event)
); );
return newApple; this.initControls();
this.startBtn.addEventListener("click", () => this.startGame());
} }
function updateSnake() { getRandomFoodPosition() {
const newHead = { return {
x: snake[0].x + direction.x, x: Math.floor(Math.random() * 17 + 1) * this.box,
y: snake[0].y + direction.y, y: Math.floor(Math.random() * 15 + 3) * this.box,
}; };
}
if (newHead.x < 0) newHead.x = gridSize - 1; handleKeyPress(event) {
if (newHead.y < 0) newHead.y = gridSize - 1; const key = event.key.toLowerCase();
if (newHead.x >= gridSize) newHead.x = 0; if ((key === "arrowleft" || key === "a") && this.direction !== "RIGHT")
if (newHead.y >= gridSize) newHead.y = 0; this.changeDirection("LEFT");
if ((key === "arrowup" || key === "w") && this.direction !== "DOWN")
this.changeDirection("UP");
if ((key === "arrowright" || key === "d") && this.direction !== "LEFT")
this.changeDirection("RIGHT");
if ((key === "arrowdown" || key === "s") && this.direction !== "UP")
this.changeDirection("DOWN");
}
initControls() {
document
.getElementById("up")
.addEventListener("click", () => this.changeDirection("UP"));
document
.getElementById("down")
.addEventListener("click", () => this.changeDirection("DOWN"));
document
.getElementById("left")
.addEventListener("click", () => this.changeDirection("LEFT"));
document
.getElementById("right")
.addEventListener("click", () => this.changeDirection("RIGHT"));
}
changeDirection(newDirection) {
this.direction = newDirection;
this.sounds[newDirection.toLowerCase()].play();
}
collision(head, array) {
return array.some(
(segment) => segment.x === head.x && segment.y === head.y
);
}
draw() {
this.ctx.drawImage(this.ground, 0, 0);
this.snake.forEach((segment, index) => {
this.ctx.fillStyle = index === 0 ? "green" : "white";
this.ctx.fillRect(segment.x, segment.y, this.box, this.box);
this.ctx.strokeStyle = "red";
this.ctx.strokeRect(segment.x, segment.y, this.box, this.box);
});
this.ctx.drawImage(this.foodImg, this.food.x, this.food.y);
let snakeX = this.snake[0].x;
let snakeY = this.snake[0].y;
if (this.direction === "LEFT") snakeX -= this.box;
if (this.direction === "UP") snakeY -= this.box;
if (this.direction === "RIGHT") snakeX += this.box;
if (this.direction === "DOWN") snakeY += this.box;
if (snakeX === this.food.x && snakeY === this.food.y) {
this.score++;
this.sounds.eat.play();
this.food = this.getRandomFoodPosition();
} else {
this.snake.pop();
}
let newHead = { x: snakeX, y: snakeY };
if ( if (
snake.some((segment) => segment.x === newHead.x && segment.y === newHead.y) snakeX < this.box ||
snakeX > 17 * this.box ||
snakeY < 3 * this.box ||
snakeY > 17 * this.box ||
this.collision(newHead, this.snake)
) { ) {
gameOver(); clearInterval(this.game);
return; this.sounds.dead.play();
} }
snake.unshift(newHead); this.snake.unshift(newHead);
this.ctx.fillStyle = "white";
this.ctx.font = "45px Changa one";
this.ctx.fillText(this.score, 2 * this.box, 1.6 * this.box);
}
if (newHead.x === apple.x && newHead.y === apple.y) { startGame() {
apple = spawnApple(); this.description.style.display = "none";
} else { this.game = setInterval(() => this.draw(), 100);
snake.pop();
} }
} }
function render() { new SnakeGame();
grid.innerHTML = "";
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
const cell = document.createElement("div");
cell.classList.add("cell");
if ((x + y) % 2 === 0) {
cell.classList.add("light-green");
} else {
cell.classList.add("dark-green");
}
if (snake.some((segment) => segment.x === x && segment.y === y)) {
cell.classList.add("snake");
}
if (apple.x === x && apple.y === y) {
cell.classList.add("apple");
}
grid.appendChild(cell);
}
}
}
function gameLoop() {
if (!isGameRunning) return;
updateSnake();
render();
}
function gameOver() {
alert("Game Over!");
clearInterval(gameInterval);
isGameRunning = false;
}
function handleDirectionInput(key) {
switch (key) {
case "ArrowUp":
case "w":
if (direction.y === 0) direction = { x: 0, y: -1 };
break;
case "ArrowDown":
case "s":
if (direction.y === 0) direction = { x: 0, y: 1 };
break;
case "ArrowLeft":
case "a":
if (direction.x === 0) direction = { x: -1, y: 0 };
break;
case "ArrowRight":
case "d":
if (direction.x === 0) direction = { x: 1, y: 0 };
break;
}
}
upButton.addEventListener("click", () => handleDirectionInput("ArrowUp"));
downButton.addEventListener("click", () => handleDirectionInput("ArrowDown"));
leftButton.addEventListener("click", () => handleDirectionInput("ArrowLeft"));
rightButton.addEventListener("click", () => handleDirectionInput("ArrowRight"));
startButton.addEventListener("click", () => {
if (!isGameRunning) initGame();
});
document.addEventListener("keydown", (event) => {
handleDirectionInput(event.key);
}); });

BIN
secret/snake/img/food.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
secret/snake/img/ground.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB