Changed around line 1
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- const startButton = document.getElementById('startButton');
- const resetButton = document.getElementById('resetButton');
- const scoreDisplay = document.getElementById('score');
+ const canvas = document.getElementById("gameCanvas");
+ const ctx = canvas.getContext("2d");
+ const startButton = document.getElementById("startButton");
+ const resetButton = document.getElementById("resetButton");
+ const scoreDisplay = document.getElementById("score");
- let snake = [{x: 10, y: 10}];
- let food = {x: 5, y: 5};
- let direction = {x: 0, y: 0};
+ let snake = [{ x: 10, y: 10 }];
+ let food = { x: 5, y: 5 };
+ let direction = { x: 0, y: 0 };
-
+
- ctx.fillStyle = '#222';
+ ctx.fillStyle = "#222";
- ctx.fillStyle = '#4CAF50';
- snake.forEach(segment => {
- ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
+ ctx.fillStyle = "#4CAF50";
+ snake.forEach((segment) => {
+ ctx.fillRect(
+ segment.x * gridSize,
+ segment.y * gridSize,
+ gridSize,
+ gridSize,
+ );
- ctx.fillStyle = '#FF5252';
+ ctx.fillStyle = "#FF5252";
- const head = {x: snake[0].x + direction.x, y: snake[0].y + direction.y};
+ const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
- if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount ||
- snake.some(segment => segment.x === head.x && segment.y === head.y)) {
+ if (
+ head.x < 0 ||
+ head.x >= tileCount ||
+ head.y < 0 ||
+ head.y >= tileCount ||
+ snake.some((segment) => segment.x === head.x && segment.y === head.y)
+ ) {
Changed around line 67: function drawGame() {
- y: Math.floor(Math.random() * tileCount)
+ y: Math.floor(Math.random() * tileCount),
-
+
- while (snake.some(segment => segment.x === food.x && segment.y === food.y)) {
+ while (
+ snake.some((segment) => segment.x === food.x && segment.y === food.y)
+ ) {
- y: Math.floor(Math.random() * tileCount)
+ y: Math.floor(Math.random() * tileCount),
- snake = [{x: 10, y: 10}];
- direction = {x: 0, y: 0};
+ snake = [{ x: 10, y: 10 }];
+ direction = { x: 1, y: 0 }; // Start moving to the right
Changed around line 97: function resetGame() {
- window.addEventListener('keydown', e => {
+ window.addEventListener("keydown", (e) => {
-
- switch(e.key) {
- case 'ArrowUp':
- if (direction.y === 0) direction = {x: 0, y: -1};
+
+ switch (e.key) {
+ case "ArrowUp":
+ if (direction.y === 0) direction = { x: 0, y: -1 };
- case 'ArrowDown':
- if (direction.y === 0) direction = {x: 0, y: 1};
+ case "ArrowDown":
+ if (direction.y === 0) direction = { x: 0, y: 1 };
- case 'ArrowLeft':
- if (direction.x === 0) direction = {x: -1, y: 0};
+ case "ArrowLeft":
+ if (direction.x === 0) direction = { x: -1, y: 0 };
- case 'ArrowRight':
- if (direction.x === 0) direction = {x: 1, y: 0};
+ case "ArrowRight":
+ if (direction.x === 0) direction = { x: 1, y: 0 };
- startButton.addEventListener('click', startGame);
- resetButton.addEventListener('click', resetGame);
+ startButton.addEventListener("click", startGame);
+ resetButton.addEventListener("click", resetGame);