Changed around line 1
+ const canvas = document.getElementById('gameCanvas');
+ const ctx = canvas.getContext('2d');
+ const startBtn = document.getElementById('startBtn');
+ const scoreValue = document.getElementById('scoreValue');
+ const highScoreValue = document.getElementById('highScoreValue');
+
+ canvas.width = 400;
+ canvas.height = 400;
+ const gridSize = 20;
+ const tileCount = canvas.width / gridSize;
+
+ let snake = [];
+ let food = {};
+ let dx = gridSize;
+ let dy = 0;
+ let score = 0;
+ let highScore = localStorage.getItem('snakeHighScore') || 0;
+ let gameLoop;
+ let gameStarted = false;
+
+ highScoreValue.textContent = highScore;
+
+ function initGame() {
+ snake = [{x: 200, y: 200}];
+ placeFood();
+ score = 0;
+ dx = gridSize;
+ dy = 0;
+ scoreValue.textContent = score;
+ }
+
+ function placeFood() {
+ food = {
+ x: Math.floor(Math.random() * tileCount) * gridSize,
+ y: Math.floor(Math.random() * tileCount) * gridSize
+ };
+ }
+
+ function draw() {
+ ctx.fillStyle = '#000';
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+
+ // Draw snake
+ ctx.fillStyle = '#00ff00';
+ snake.forEach((segment, i) => {
+ ctx.fillRect(segment.x, segment.y, gridSize-2, gridSize-2);
+ if (i === 0) {
+ ctx.fillStyle = '#00cc00';
+ }
+ });
+
+ // Draw food
+ ctx.fillStyle = '#ff0000';
+ ctx.fillRect(food.x, food.y, gridSize-2, gridSize-2);
+ }
+
+ function move() {
+ const head = {x: snake[0].x + dx, y: snake[0].y + dy};
+
+ // Wall collision
+ if (head.x >= canvas.width) head.x = 0;
+ if (head.x < 0) head.x = canvas.width - gridSize;
+ if (head.y >= canvas.height) head.y = 0;
+ if (head.y < 0) head.y = canvas.height - gridSize;
+
+ snake.unshift(head);
+
+ // Food collision
+ if (head.x === food.x && head.y === food.y) {
+ score += 10;
+ scoreValue.textContent = score;
+ if (score > highScore) {
+ highScore = score;
+ highScoreValue.textContent = highScore;
+ localStorage.setItem('snakeHighScore', highScore);
+ }
+ placeFood();
+ } else {
+ snake.pop();
+ }
+
+ // Self collision
+ for (let i = 1; i < snake.length; i++) {
+ if (head.x === snake[i].x && head.y === snake[i].y) {
+ gameOver();
+ }
+ }
+ }
+
+ function gameOver() {
+ clearInterval(gameLoop);
+ gameStarted = false;
+ startBtn.textContent = 'Play Again';
+ }
+
+ function gameUpdate() {
+ move();
+ draw();
+ }
+
+ startBtn.addEventListener('click', () => {
+ if (gameStarted) {
+ gameOver();
+ } else {
+ gameStarted = true;
+ initGame();
+ gameLoop = setInterval(gameUpdate, 100);
+ startBtn.textContent = 'Stop Game';
+ }
+ });
+
+ document.addEventListener('keydown', (e) => {
+ if (!gameStarted) return;
+
+ switch(e.key) {
+ case 'ArrowUp':
+ if (dy === 0) { dx = 0; dy = -gridSize; }
+ break;
+ case 'ArrowDown':
+ if (dy === 0) { dx = 0; dy = gridSize; }
+ break;
+ case 'ArrowLeft':
+ if (dx === 0) { dx = -gridSize; dy = 0; }
+ break;
+ case 'ArrowRight':
+ if (dx === 0) { dx = gridSize; dy = 0; }
+ break;
+ }
+ });
+
+ // Touch controls
+ let touchStartX, touchStartY;
+ canvas.addEventListener('touchstart', (e) => {
+ touchStartX = e.touches[0].clientX;
+ touchStartY = e.touches[0].clientY;
+ });
+
+ canvas.addEventListener('touchend', (e) => {
+ if (!gameStarted) return;
+
+ const touchEndX = e.changedTouches[0].clientX;
+ const touchEndY = e.changedTouches[0].clientY;
+ const deltaX = touchEndX - touchStartX;
+ const deltaY = touchEndY - touchStartY;
+
+ if (Math.abs(deltaX) > Math.abs(deltaY)) {
+ if (deltaX > 0 && dx === 0) { dx = gridSize; dy = 0; }
+ else if (deltaX < 0 && dx === 0) { dx = -gridSize; dy = 0; }
+ } else {
+ if (deltaY > 0 && dy === 0) { dx = 0; dy = gridSize; }
+ else if (deltaY < 0 && dy === 0) { dx = 0; dy = -gridSize; }
+ }
+ });
+
+ initGame();
+ draw();