Code me a snake game, let the game be according to the phone, let there be buttons on…
- User
Code me a snake game, let the game be according to the phone, let there be buttons on the screen, let me play with it, let the design be very stylish and comprehensive. The game does not start directly, there should be a start screen, when I press the button, the game starts and the snake should not be too fast, there should be a scoreboard. Write everything in a single piece of code and be in html language
- Assistant
Here's a simple implementation of a snake game using HTML, CSS, and JavaScript. This version includes a stylish start screen, buttons for controlling the snake, and a scoreboard. You can copy and paste the entire code into an HTML file and open it in a web browser to play the game.
<!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; flex-direction: column; align-items: center; font-family: 'Arial', sans-serif; background-color: #f2f2f2; } canvas { border: 2px solid #333; background-color: #fff; } #start-screen { text-align: center; margin-top: 50px; } #start-button { padding: 15px 30px; font-size: 24px; cursor: pointer; background-color: #4CAF50; color: white; border: none; border-radius: 5px; } #score { font-size: 24px; margin-top: 20px; } .controls { margin-top: 20px; } .control-button { padding: 10px 20px; font-size: 18px; cursor: pointer; background-color: #2196F3; color: white; border: none; border-radius: 5px; margin: 5px; } </style> </head> <body> <div id="start-screen"> <h1>Snake Game</h1> <button id="start-button">Start Game</button> </div> <canvas id="gameCanvas" width="400" height="400" style="display: none;"></canvas> <div id="score">Score: 0</div> <div class="controls" style="display: none;"> <button class="control-button" id="up">Up</button> <button class="control-button" id="left">Left</button> <button class="control-button" id="down">Down</button> <button class="control-button" id="right">Right</button> </div> <script> const canvas = document.getElementById("gameCanvas"); const context = canvas.getContext("2d"); const startButton = document.getElementById("start-button"); const scoreDisplay = document.getElementById("score"); const controls = document.querySelector('.controls'); let snake; let food; let score; let changingDirection; let gameInterval; function startGame() { snake = [{ x: 200, y: 200 }]; food = spawnFood(); score = 0; changingDirection = false; document.getElementById("start-screen").style.display = "none"; canvas.style.display = "block"; scoreDisplay.style.display = "block"; controls.style.display = "flex"; gameInterval = setInterval(main, 100); } function spawnFood() { return { x: Math.floor(Math.random() * (canvas.width / 10)) * 10, y: Math.floor(Math.random() * (canvas.height / 10)) * 10 }; } function main() { if (changingDirection) return; const snakeX = snake[0].x; const snakeY = snake[0].y; if (direction === "up") snake[0].y -= 10; if (direction === "down") snake[0].y += 10; if (direction === "left") snake[0].x -= 10; if (direction === "right") snake[0].x += 10; if (snake[0].x === food.x && snake[0].y === food.y) { score++; scoreDisplay.textContent = "Score: " + score; food = spawnFood(); } else { snake.pop(); } if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height || collision()) { clearInterval(gameInterval); alert("Game Over! Your score: " + score); document.location.reload(); } context.clearRect(0, 0, canvas.width, canvas.height); drawSnake(); drawFood(); } function drawSnake() { context.fillStyle = "green"; snake.forEach(part => { context.fillRect(part.x, part.y, 10, 10); }); } function drawFood() { context.fillStyle = "red"; context.fillRect(food.x, food.y, 10, 10); } function collision() { for (let i = 1; i < snake.length; i++) { if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) { return true; } } return false; } let direction = "right"; document.getElementById("up").addEventListener("click", () => { if (direction !== "down") { direction = "up"; changingDirection = true; } }); document.getElementById("down").addEventListener("click", () => { if (direction !== "up") { direction = "down"; changingDirection = true; } }); document.getElementById("left").addEventListener("click", () => { if (direction !== "right") { direction = "left"; changingDirection = true; } }); document.getElementById("right").addEventListener("click", () => { if (direction !== "left") { direction = "right"; changingDirection = true; } }); startButton.addEventListener("click", startGame); </script> </body> </html>Explanation of the Code:
- HTML Structure: The game consists of a start screen, a canvas for drawing the game, a score display, and control buttons.
- CSS Styles: Basic styles are applied to make the game visually appealing.
- JavaScript Functions:
startGame: Initializes the game variables and starts the game loop.spawnFood: Randomly places food on the canvas.main: The main game loop that updates the game state.drawSnakeanddrawFood: Functions to render the snake and food on the canvas.- Direction handling with buttons to control the snake's movement.
You can play the game by clicking the buttons displayed below the game area. Enjoy!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.