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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Below is a complete HTML code for a stylish Snake game with on-screen buttons suitable for phone play. The game includes a start screen, a scoreboard, and the snake moves at a manageable speed. You can copy and paste this code into a file with an .html extension and open it in your browser to play.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake Game</title>
<style>
body {
margin: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #222;
color: #fff;
overflow: hidden;
}
#startScreen, #gameOverScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #222;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 2;
}
#startScreen button, #gameOverScreen button {
padding: 15px 30px;
font-size: 24px;
cursor: pointer;
background: #28a745;
border: none;
border-radius: 5px;
color: #fff;
}
#gameContainer {
position: relative;
width: 100vw;
height: 100vh;
}
canvas {
background: #000;
display: block;
margin: 0 auto;
}
#scoreboard {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
font-size: 24px;
}
#controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
flex-wrap: wrap;
width: 200px;
justify-content: center;
}
.control-button {
width: 60px;
height: 60px;
margin: 5px;
background: #555;
border: none;
border-radius: 10px;
color: #fff;
font-size: 30px;
outline: none;
}
</style>
</head>
<body>
<div id="startScreen">
<h1>Welcome to Snake Game</h1>
<button onclick="startGame()">Start Game</button>
</div>
<div id="gameContainer">
<div id="scoreboard">Score: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div id="controls">
<button class="control-button" onclick="changeDirection('Up')">▲</button>
<div style="display: flex;">
<button class="control-button" onclick="changeDirection('Left')">◄</button>
<button class="control-button" onclick="changeDirection('Right')">►</button>
</div>
<button class="control-button" onclick="changeDirection('Down')">▼</button>
</div>
</div>
<div id="gameOverScreen" style="display:none;">
<h1>Game Over</h1>
<p>Your Score: <span id="finalScore">0</span></p>
<button onclick="startGame()">Play Again</button>
</div>
<script>
let canvas = document.getElementById('gameCanvas');
let ctx = canvas.getContext('2d');
let box = 20;
let snake = [];
let food;
let score = 0;
let direction = 'Right';
let gameInterval;
function startGame() {
document.getElementById('startScreen').style.display = 'none';
document.getElementById('gameOverScreen').style.display = 'none';
snake = [];
snake[0] = { x: 9 * box, y: 9 * box };
direction = 'Right';
score = 0;
document.getElementById('scoreboard').innerText = 'Score: ' + score;
placeFood();
if (gameInterval) clearInterval(gameInterval);
gameInterval = setInterval(drawGame, 100);
}
function placeFood() {
food = {
x: Math.floor(Math.random() * 20) * box,
y: Math.floor(Math.random() * 20) * box,
};
}
function drawGame() {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < snake.length; i++) {
ctx.fillStyle = i === 0 ? '#00FF00' : '#00AA00';
ctx.fillRect(snake[i].x, snake[i].y, box, box);
}
ctx.fillStyle = '#FF0000';
ctx.fillRect(food.x, food.y, box, box);
let snakeX = snake[0].x;
let snakeY = snake[0].y;
if (direction == 'Left') snakeX -= box;
if (direction == 'Up') snakeY -= box;
if (direction == 'Right') snakeX += box;
if (direction == 'Down') snakeY += box;
if (snakeX == food.x && snakeY == food.y) {
score++;
document.getElementById('scoreboard').innerText = 'Score: ' + score;
placeFood();
} else {
snake.pop();
}
let newHead = { x: snakeX, y: snakeY };
if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
clearInterval(gameInterval);
document.getElementById('finalScore').innerText = score;
document.getElementById('gameOverScreen').style.display = 'flex';
return;
}
snake.unshift(newHead);
}
function collision(head, array) {
for (let i = 1; i < array.length; i++) {
if (head.x == array[i].x && head.y == array[i].y) {
return true;
}
}
return false;
}
function changeDirection(dir) {
if (dir == 'Left' && direction != 'Right') direction = 'Left';
else if (dir == 'Up' && direction != 'Down') direction = 'Up';
else if (dir == 'Right' && direction != 'Left') direction = 'Right';
else if (dir == 'Down' && direction != 'Up') direction = 'Down';
}
document.addEventListener('keydown', function(event) {
let key = event.keyCode;
if ((key == 37 || key == 65) && direction != 'Right') direction = 'Left';
else if ((key == 38 || key == 87) && direction != 'Down') direction = 'Up';
else if ((key == 39 || key == 68) && direction != 'Left') direction = 'Right';
else if ((key == 40 || key == 83) && direction != 'Up') direction = 'Down';
});
</script>
</body>
</html>
Instructions:
Copy the entire code above.
Paste it into a text editor (e.g., Notepad, VSCode).
Save the file with a .html extension, like snake_game.html.
Open the saved file using a web browser (e.g., Chrome, Firefox).
How to Play:
Start Screen: Click the "Start Game" button to begin.
Controls: Use the on-screen buttons to control the snake's direction.
▲ Up
▼ Down
◄ Left
► Right
Objective: Eat the red food squares to grow longer and increase your score.
Game Over: The game ends if the snake hits the walls or itself. You can then choose to play again.
Features:
Responsive Design: The game scales to fit your browser window.
Scoreboard: Displays your current score at the top center.
Game Over Screen: Shows your final score with an option to play again.
Keyboard Support: You can also use arrow keys or WASD keys for control.