cool i borked it even further
This commit is contained in:
parent
253acb460d
commit
849ec9f4bc
9 changed files with 113 additions and 125 deletions
BIN
secret/snake/audio/dead.mp3
Normal file
BIN
secret/snake/audio/dead.mp3
Normal file
Binary file not shown.
BIN
secret/snake/audio/down.mp3
Normal file
BIN
secret/snake/audio/down.mp3
Normal file
Binary file not shown.
BIN
secret/snake/audio/eat.mp3
Normal file
BIN
secret/snake/audio/eat.mp3
Normal file
Binary file not shown.
BIN
secret/snake/audio/left.mp3
Normal file
BIN
secret/snake/audio/left.mp3
Normal file
Binary file not shown.
BIN
secret/snake/audio/right.mp3
Normal file
BIN
secret/snake/audio/right.mp3
Normal file
Binary file not shown.
BIN
secret/snake/audio/up.mp3
Normal file
BIN
secret/snake/audio/up.mp3
Normal file
Binary file not shown.
|
@ -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"),
|
||||||
|
left: new Audio("audio/left.mp3"),
|
||||||
|
down: new Audio("audio/down.mp3"),
|
||||||
|
};
|
||||||
|
|
||||||
clearInterval(gameInterval);
|
this.snake = [{ x: 9 * this.box, y: 10 * this.box }];
|
||||||
gameInterval = setInterval(gameLoop, 200);
|
this.food = this.getRandomFoodPosition();
|
||||||
render();
|
this.score = 0;
|
||||||
}
|
this.direction = null;
|
||||||
|
this.game = null;
|
||||||
|
|
||||||
function spawnApple() {
|
document.addEventListener("keydown", (event) =>
|
||||||
let newApple;
|
this.handleKeyPress(event)
|
||||||
do {
|
);
|
||||||
newApple = {
|
this.initControls();
|
||||||
x: Math.floor(Math.random() * gridSize),
|
this.startBtn.addEventListener("click", () => this.startGame());
|
||||||
y: Math.floor(Math.random() * gridSize),
|
}
|
||||||
};
|
|
||||||
} while (
|
|
||||||
snake.some(
|
|
||||||
(segment) => segment.x === newApple.x && segment.y === newApple.y
|
|
||||||
)
|
|
||||||
);
|
|
||||||
return newApple;
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
initControls() {
|
||||||
snake.some((segment) => segment.x === newHead.x && segment.y === newHead.y)
|
document
|
||||||
) {
|
.getElementById("up")
|
||||||
gameOver();
|
.addEventListener("click", () => this.changeDirection("UP"));
|
||||||
return;
|
document
|
||||||
}
|
.getElementById("down")
|
||||||
|
.addEventListener("click", () => this.changeDirection("DOWN"));
|
||||||
|
document
|
||||||
|
.getElementById("left")
|
||||||
|
.addEventListener("click", () => this.changeDirection("LEFT"));
|
||||||
|
document
|
||||||
|
.getElementById("right")
|
||||||
|
.addEventListener("click", () => this.changeDirection("RIGHT"));
|
||||||
|
}
|
||||||
|
|
||||||
snake.unshift(newHead);
|
changeDirection(newDirection) {
|
||||||
|
this.direction = newDirection;
|
||||||
|
this.sounds[newDirection.toLowerCase()].play();
|
||||||
|
}
|
||||||
|
|
||||||
if (newHead.x === apple.x && newHead.y === apple.y) {
|
collision(head, array) {
|
||||||
apple = spawnApple();
|
return array.some(
|
||||||
} else {
|
(segment) => segment.x === head.x && segment.y === head.y
|
||||||
snake.pop();
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function render() {
|
draw() {
|
||||||
grid.innerHTML = "";
|
this.ctx.drawImage(this.ground, 0, 0);
|
||||||
|
|
||||||
for (let y = 0; y < gridSize; y++) {
|
this.snake.forEach((segment, index) => {
|
||||||
for (let x = 0; x < gridSize; x++) {
|
this.ctx.fillStyle = index === 0 ? "green" : "white";
|
||||||
const cell = document.createElement("div");
|
this.ctx.fillRect(segment.x, segment.y, this.box, this.box);
|
||||||
cell.classList.add("cell");
|
this.ctx.strokeStyle = "red";
|
||||||
|
this.ctx.strokeRect(segment.x, segment.y, this.box, this.box);
|
||||||
|
});
|
||||||
|
|
||||||
if ((x + y) % 2 === 0) {
|
this.ctx.drawImage(this.foodImg, this.food.x, this.food.y);
|
||||||
cell.classList.add("light-green");
|
|
||||||
|
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 {
|
} else {
|
||||||
cell.classList.add("dark-green");
|
this.snake.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (snake.some((segment) => segment.x === x && segment.y === y)) {
|
let newHead = { x: snakeX, y: snakeY };
|
||||||
cell.classList.add("snake");
|
if (
|
||||||
|
snakeX < this.box ||
|
||||||
|
snakeX > 17 * this.box ||
|
||||||
|
snakeY < 3 * this.box ||
|
||||||
|
snakeY > 17 * this.box ||
|
||||||
|
this.collision(newHead, this.snake)
|
||||||
|
) {
|
||||||
|
clearInterval(this.game);
|
||||||
|
this.sounds.dead.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apple.x === x && apple.y === y) {
|
this.snake.unshift(newHead);
|
||||||
cell.classList.add("apple");
|
this.ctx.fillStyle = "white";
|
||||||
}
|
this.ctx.font = "45px Changa one";
|
||||||
|
this.ctx.fillText(this.score, 2 * this.box, 1.6 * this.box);
|
||||||
|
}
|
||||||
|
|
||||||
grid.appendChild(cell);
|
startGame() {
|
||||||
|
this.description.style.display = "none";
|
||||||
|
this.game = setInterval(() => this.draw(), 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function gameLoop() {
|
new SnakeGame();
|
||||||
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
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
BIN
secret/snake/img/ground.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
Loading…
Add table
Reference in a new issue