Created
February 1, 2025 18:12
-
-
Save carefree-ladka/3b5bbf9ab45d7bb2b31b129739498251 to your computer and use it in GitHub Desktop.
Snake Game Based on OOPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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