Changed around line 1
+ class Snake {
+ constructor(canvas) {
+ this.canvas = canvas;
+ this.ctx = canvas.getContext('2d');
+ this.gridSize = 20;
+ this.snake = [{x: 10, y: 10}];
+ this.direction = {x: 0, y: 0};
+ this.food = this.generateFood();
+ this.score = 0;
+ this.highScore = localStorage.getItem('snakeHighScore') || 0;
+ this.gameLoop = null;
+ this.touchStart = null;
+ this.setupEventListeners();
+ this.updateHighScore();
+ }
+
+ setupEventListeners() {
+ document.addEventListener('keydown', this.handleKeyPress.bind(this));
+ this.canvas.addEventListener('touchstart', this.handleTouchStart.bind(this));
+ this.canvas.addEventListener('touchend', this.handleTouchEnd.bind(this));
+ document.getElementById('startButton').addEventListener('click', this.startGame.bind(this));
+ }
+
+ handleKeyPress(event) {
+ const keyActions = {
+ 'ArrowUp': () => this.direction.y !== 1 && this.setDirection(0, -1),
+ 'ArrowDown': () => this.direction.y !== -1 && this.setDirection(0, 1),
+ 'ArrowLeft': () => this.direction.x !== 1 && this.setDirection(-1, 0),
+ 'ArrowRight': () => this.direction.x !== -1 && this.setDirection(1, 0)
+ };
+ if (keyActions[event.key]) {
+ event.preventDefault();
+ keyActions[event.key]();
+ }
+ }
+
+ handleTouchStart(event) {
+ this.touchStart = {
+ x: event.touches[0].clientX,
+ y: event.touches[0].clientY
+ };
+ }
+
+ handleTouchEnd(event) {
+ if (!this.touchStart) return;
+ const touchEnd = {
+ x: event.changedTouches[0].clientX,
+ y: event.changedTouches[0].clientY
+ };
+ const dx = touchEnd.x - this.touchStart.x;
+ const dy = touchEnd.y - this.touchStart.y;
+
+ if (Math.abs(dx) > Math.abs(dy)) {
+ if (dx > 0 && this.direction.x !== -1) this.setDirection(1, 0);
+ else if (dx < 0 && this.direction.x !== 1) this.setDirection(-1, 0);
+ } else {
+ if (dy > 0 && this.direction.y !== -1) this.setDirection(0, 1);
+ else if (dy < 0 && this.direction.y !== 1) this.setDirection(0, -1);
+ }
+ }
+
+ setDirection(x, y) {
+ this.direction = {x, y};
+ }
+
+ generateFood() {
+ return {
+ x: Math.floor(Math.random() * (this.canvas.width / this.gridSize)),
+ y: Math.floor(Math.random() * (this.canvas.height / this.gridSize))
+ };
+ }
+
+ update() {
+ const head = {
+ x: this.snake[0].x + this.direction.x,
+ y: this.snake[0].y + this.direction.y
+ };
+
+ if (this.checkCollision(head)) {
+ this.gameOver();
+ return;
+ }
+
+ this.snake.unshift(head);
+
+ if (head.x === this.food.x && head.y === this.food.y) {
+ this.score += 10;
+ document.getElementById('scoreValue').textContent = this.score;
+ this.food = this.generateFood();
+ } else {
+ this.snake.pop();
+ }
+ }
+
+ checkCollision(head) {
+ return head.x < 0 ||
+ head.x >= this.canvas.width / this.gridSize ||
+ head.y < 0 ||
+ head.y >= this.canvas.height / this.gridSize ||
+ this.snake.some(segment => segment.x === head.x && segment.y === head.y);
+ }
+
+ draw() {
+ this.ctx.fillStyle = '#000';
+ this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
+
+ this.ctx.fillStyle = '#4CAF50';
+ this.snake.forEach(segment => {
+ this.ctx.fillRect(
+ segment.x * this.gridSize,
+ segment.y * this.gridSize,
+ this.gridSize - 1,
+ this.gridSize - 1
+ );
+ });
+
+ this.ctx.fillStyle = '#FF5252';
+ this.ctx.fillRect(
+ this.food.x * this.gridSize,
+ this.food.y * this.gridSize,
+ this.gridSize - 1,
+ this.gridSize - 1
+ );
+ }
+
+ gameOver() {
+ clearInterval(this.gameLoop);
+ if (this.score > this.highScore) {
+ this.highScore = this.score;
+ localStorage.setItem('snakeHighScore', this.highScore);
+ this.updateHighScore();
+ }
+ alert(`Game Over! Score: ${this.score}`);
+ document.getElementById('startButton').disabled = false;
+ }
+
+ updateHighScore() {
+ document.getElementById('highScoreValue').textContent = this.highScore;
+ }
+
+ startGame() {
+ this.snake = [{x: 10, y: 10}];
+ this.direction = {x: 0, y: 0};
+ this.score = 0;
+ document.getElementById('scoreValue').textContent = '0';
+ document.getElementById('startButton').disabled = true;
+ this.food = this.generateFood();
+ if (this.gameLoop) clearInterval(this.gameLoop);
+ this.gameLoop = setInterval(() => {
+ this.update();
+ this.draw();
+ }, 100);
+ }
+ }
+
+ window.onload = () => {
+ const canvas = document.getElementById('gameCanvas');
+ canvas.width = 400;
+ canvas.height = 400;
+ new Snake(canvas);
+ };