Skip to content

Instantly share code, notes, and snippets.

@carefree-ladka
Created February 1, 2025 18:12
Show Gist options
  • Save carefree-ladka/3b5bbf9ab45d7bb2b31b129739498251 to your computer and use it in GitHub Desktop.
Save carefree-ladka/3b5bbf9ab45d7bb2b31b129739498251 to your computer and use it in GitHub Desktop.
Snake Game Based on OOPS
class Board {
constructor(size) {
this.size = size; // e.g., 20x20 grid
this.food = this.generateFood();
}
generateFood() {
return {
x: Math.floor(Math.random() * this.size),
y: Math.floor(Math.random() * this.size)
};
}
isFoodEaten(snakeHead) {
return this.food.x === snakeHead.x && this.food.y === snakeHead.y;
}
updateFoodPosition() {
this.food = this.generateFood();
}
}
class Snake {
constructor() {
this.body = [{ x: 10, y: 10 }]; // Initial position
this.direction = "RIGHT"; // Default movement
this.grow = false;
}
changeDirection(newDirection) {
const oppositeDirections = {
UP: "DOWN",
DOWN: "UP",
LEFT: "RIGHT",
RIGHT: "LEFT"
};
if (newDirection !== oppositeDirections[this.direction]) {
this.direction = newDirection;
}
}
move() {
let head = { ...this.body[0] };
switch (this.direction) {
case "UP":
head.y -= 1;
break;
case "DOWN":
head.y += 1;
break;
case "LEFT":
head.x -= 1;
break;
case "RIGHT":
head.x += 1;
break;
}
this.body.unshift(head);
if (!this.grow) {
this.body.pop();
}
this.grow = false;
}
eatFood() {
this.grow = true;
}
isColliding(boardSize) {
const [head, ...body] = this.body;
return (
head.x < 0 || head.y < 0 || head.x >= boardSize || head.y >= boardSize ||
body.some(segment => segment.x === head.x && segment.y === head.y)
);
}
}
class Game {
constructor(size = 20) {
this.board = new Board(size);
this.snake = new Snake();
this.isGameOver = false;
this.interval = null;
}
start() {
console.log("🐍 Snake Game Started!");
this.interval = setInterval(() => this.update(), 200); // Game loop every 200ms
}
update() {
if (this.isGameOver) {
console.log("πŸ’€ Game Over!");
clearInterval(this.interval);
return;
}
this.snake.move();
if (this.snake.isColliding(this.board.size)) {
this.isGameOver = true;
}
if (this.board.isFoodEaten(this.snake.body[0])) {
this.snake.eatFood();
this.board.updateFoodPosition();
console.log("🍏 Food eaten!");
}
this.render();
}
render() {
console.clear();
let grid = Array.from({ length: this.board.size }, () =>
Array(this.board.size).fill(" ")
);
// Draw food
grid[this.board.food.y][this.board.food.x] = "🍏";
// Draw snake
this.snake.body.forEach(segment => {
grid[segment.y][segment.x] = "🐍";
});
console.log(grid.map(row => row.join(" ")).join("\n"));
}
changeSnakeDirection(newDirection) {
this.snake.changeDirection(newDirection);
}
}
// Start Game
const game = new Game();
game.start();
// Example controls
document.addEventListener("keydown", (event) => {
const directionKeys = {
ArrowUp: "UP",
ArrowDown: "DOWN",
ArrowLeft: "LEFT",
ArrowRight: "RIGHT"
};
if (directionKeys[event.key]) {
game.changeSnakeDirection(directionKeys[event.key]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment