create a snake game
- User
create a snake game
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
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll create a complete Snake game using HTML, CSS, and JavaScript. Here's the full implementation:
<!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: #1a1a1a;
font-family: Arial, sans-serif;
}
.game-container {
text-align: center;
background-color: #2d2d2d;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 255, 0, 0.3);
}
.score {
color: #00ff00;
font-size: 24px;
margin-bottom: 10px;
}
#gameCanvas {
border: 2px solid #00ff00;
background-color: #000;
}
.controls {
color: #fff;
margin-top: 15px;
font-size: 14px;
}
.game-over {
color: #ff0000;
font-size: 20px;
margin-top: 10px;
}
button {
background-color: #00ff00;
color: #000;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #00cc00;
}
</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 class="controls">
Use arrow keys to control the snake
</div>
<div id="gameOver" class="game-over" style="display: none;">
Game Over!
<br>
<button onclick="resetGame()">Play Again</button>
</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;
// Initialize the game
function init() {
generateFood();
gameLoop();
}
// Generate random food position
function generateFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// Make sure food doesn't spawn on snake
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
generateFood();
return;
}
}
}
// Draw everything on the canvas
function draw() {
// Clear canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
ctx.fillStyle = '#00ff00';
for (let segment of snake) {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
}
// Draw food
ctx.fillStyle = '#ff0000';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
}
// Update game state
function update() {
if (!gameRunning) return;
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
// Check wall collision
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
return;
}
// Check self collision
for (let segment of snake) {
if (head.x === segment.x && head.y === segment.y) {
gameOver();
return;
}
}
snake.unshift(head);
// Check if food is eaten
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score;
generateFood();
} else {
snake.pop();
}
}
// Game over
function gameOver() {
gameRunning = false;
gameOverElement.style.display = 'block';
}
// Reset game
function resetGame() {
snake = [{ x: 10, y: 10 }];
dx = 0;
dy = 0;
score = 0;
scoreElement.textContent = score;
gameRunning = true;
gameOverElement.style.display = 'none';
generateFood();
}
// Main game loop
function gameLoop() {
update();
draw();
if (gameRunning) {
setTimeout(gameLoop, 150);
}
}
// Handle keyboard input
document.addEventListener('keydown', (e) => {
if (!gameRunning) return;
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;
}
});
// Start the game
init();
</script>
</body>
</html>
This Snake game includes:
Features:
How to play:
snake-game.html)The snake moves automatically once you press an arrow key, and the game prevents you from moving directly backward into yourself. The speed is set to a comfortable pace, but you can adjust it by changing the timeout value in the gameLoop function.