make a html snake game
- User
make a html snake game
Assistant
I'll create a complete HTML Snake game for you. Here's the code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Snake Game</title> <style> body { display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background-color: #333; font-family: Arial, sans-serif; } .game-container { text-align: center; } canvas { border: 3px solid #fff; background-color: #000; } .score { color: #fff; font-size: 24px; margin-bottom: 10px; } .game-over { color: #ff4444; font-size: 32px; margin-top: 20px; } .instructions { color: #ccc; margin-top: 20px; font-size: 16px; } .restart-btn { background-color: #4CAF50; color: white; border: none; padding: 10px 20px; font-size: 16px; cursor: pointer; margin-top: 10px; border-radius: 5px; } .restart-btn:hover { background-color: #45a049; } </style> </head> <body> <div class="game-container"> <div class="score">Score: <span id="score">0</span></div> <canvas id="gameCanvas" width="400" height="400"></canvas> <div id="gameOver" class="game-over" style="display: none;"> Game Over! <br> <button class="restart-btn" onclick="restartGame()">Play Again</button> </div> <div class="instructions"> Use arrow keys to control the snake </div> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const scoreElement = document.getElementById('score'); const gameOverElement = document.getElementById('gameOver'); // Game variables const gridSize = 20; const tileCount = canvas.width / gridSize; let snake = [ {x: 10, y: 10} ]; let food = {}; let dx = 0; let dy = 0; let score = 0; let gameRunning = true; // Generate random food position function randomFood() { food = { x: Math.floor(Math.random() * tileCount), y: Math.floor(Math.random() * tileCount) }; } // Draw game elements function drawGame() { // Clear canvas ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw snake ctx.fillStyle = 'lime'; for (let segment of snake) { ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2); } // Draw food ctx.fillStyle = 'red'; ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2); } // Move snake function moveSnake() { const head = {x: snake[0].x + dx, y: snake[0].y + dy}; // Add new head snake.unshift(head); // Check if food is eaten if (head.x === food.x && head.y === food.y) { score += 10; scoreElement.textContent = score; randomFood(); } else { // Remove tail if no food eaten snake.pop(); } } // Check collisions function checkCollision() { const head = snake[0]; // Wall collision if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) { return true; } // Self collision for (let i = 1; i < snake.length; i++) { if (head.x === snake[i].x && head.y === snake[i].y) { return true; } } return false; } // Handle keyboard input function handleKeyPress(e) { if (!gameRunning) return; const LEFT_KEY = 37; const RIGHT_KEY = 39; const UP_KEY = 38; const DOWN_KEY = 40; const keyPressed = e.keyCode; const goingUp = dy === -1; const goingDown = dy === 1; const goingRight = dx === 1; const goingLeft = dx === -1; if (keyPressed === LEFT_KEY && !goingRight) { dx = -1; dy = 0; } if (keyPressed === UP_KEY && !goingDown) { dx = 0; dy = -1; } if (keyPressed === RIGHT_KEY && !goingLeft) { dx = 1; dy = 0; } if (keyPressed === DOWN_KEY && !goingUp) { dx = 0; dy = 1; } } // Game over function gameOver() { gameRunning = false; gameOverElement.style.display = 'block'; } // Restart game function restartGame() { snake = [{x: 10, y: 10}]; dx = 0; dy = 0; score = 0; scoreElement.textContent = score; gameRunning = true; gameOverElement.style.display = 'none'; randomFood(); } // Main game loop function gameLoop() { if (gameRunning) { if (dx !== 0 || dy !== 0) { // Only move if direction is set moveSnake(); if (checkCollision()) { gameOver(); return; } } drawGame(); } setTimeout(gameLoop, 100); } // Initialize game document.addEventListener('keydown', handleKeyPress); randomFood(); gameLoop(); </script> </body> </html>This Snake game includes:
Features:
- Classic snake gameplay with arrow key controls
- Score tracking (10 points per food item)
- Collision detection for walls and self-collision
- Game over screen with restart functionality
- Responsive design with a clean, retro look
How to play:
- Use arrow keys to control the snake
- Eat the red food to grow and increase your score
- Avoid hitting the walls or your own tail
- Click "Play Again" when the game ends
Controls:
- ↑ Arrow Key: Move up
- ↓ Arrow Key: Move down
- ← Arrow Key: Move left
- → Arrow Key: Move right
Just save this code as an HTML file and open it in your web browser to play the game!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.