create a snake game in web browser
- User
create a snake game in web browser
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
create a snake game in web browser
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a complete Snake game implementation in a single HTML file with modern styling:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Modern Snake Game</title>
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
font-family: 'Arial', sans-serif;
}
.game-container {
text-align: center;
}
canvas {
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
margin: 20px 0;
}
.score {
color: #e94560;
font-size: 24px;
text-shadow: 0 0 10px rgba(233, 69, 96, 0.5);
}
h1 {
color: #fff;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="game-container">
<h1>Snake Game</h1>
<div class="score">Score: <span id="score">0</span></div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
// Game settings
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [
{ x: 10, y: 10 },
{ x: 9, y: 10 },
{ x: 8, y: 10 }
];
let food = generateFood();
let dx = 1;
let dy = 0;
let score = 0;
let gameSpeed = 100;
let gameRunning = true;
// Game loop
function gameLoop() {
if (!gameRunning) return;
moveSnake();
checkCollision();
checkFood();
drawGame();
}
function moveSnake() {
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
snake.unshift(head);
snake.pop();
}
function checkFood() {
if (headCollision(food)) {
snake.push({});
score++;
scoreElement.textContent = score;
food = generateFood();
gameSpeed = Math.max(50, gameSpeed - 2);
}
}
function generateFood() {
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
} while (snake.some(segment => segment.x === newFood.x && segment.y === newFood.y));
return newFood;
}
function checkCollision() {
const head = snake[0];
// Wall collision
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
}
// Self collision
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
}
}
}
function gameOver() {
gameRunning = false;
alert(`Game Over! Score: ${score}`);
resetGame();
}
function resetGame() {
snake = [
{ x: 10, y: 10 },
{ x: 9, y: 10 },
{ x: 8, y: 10 }
];
dx = 1;
dy = 0;
score = 0;
scoreElement.textContent = score;
gameSpeed = 100;
gameRunning = true;
food = generateFood();
}
function drawGame() {
// Clear canvas
ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#00ff9d' : `hsl(${index * 10}, 100%, 50%)`;
ctx.beginPath();
ctx.roundRect(
segment.x * gridSize,
segment.y * gridSize,
gridSize - 1,
gridSize - 1,
index === 0 ? 5 : 3
);
ctx.fill();
});
// Draw food
ctx.fillStyle = '#ff3864';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize/2,
food.y * gridSize + gridSize/2,
gridSize/2 - 1,
0,
Math.PI * 2
);
ctx.fill();
// Draw grid (optional)
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 0.5;
for (let i = 0; i < tileCount; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize, 0);
ctx.lineTo(i * gridSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * gridSize);
ctx.lineTo(canvas.width, i * gridSize);
ctx.stroke();
}
}
// Input handling
document.addEventListener('keydown', (e) => {
switch(e.key) {
case 'ArrowUp':
if (dy !== 1) { dx = 0; dy = -1; }
break;
case 'ArrowDown':
if (dy !== -1) { dx = 0; dy = 1; }
break;
case 'ArrowLeft':
if (dx !== 1) { dx = -1; dy = 0; }
break;
case 'ArrowRight':
if (dx !== -1) { dx = 1; dy = 0; }
break;
}
});
// Helper functions
function headCollision(pos) {
return snake[0].x === pos.x && snake[0].y === pos.y;
}
// Start game
drawGame();
setInterval(gameLoop, gameSpeed);
</script>
</body>
</html>
This implementation features:
To play:
The game automatically speeds up as you progress, making it increasingly challenging. The UI has a modern dark theme with glowing elements and smooth animations.