Meta Description" name="description" />
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake Arena Game</title>
<style>
body { margin: 0; overflow: hidden; background: #d8c2b5; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="game"></canvas><script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let snake = [{x: 300, y: 300}];
let dx = 2;
let dy = 0;
let foods = [];
// Create random food
for (let i = 0; i < 30; i++) {
foods.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height
});
}
// Mouse control
canvas.addEventListener('mousemove', (e) => {
const angle = Math.atan2(e.clientY - snake[0].y, e.clientX - snake[0].x);
dx = Math.cos(angle) * 2;
dy = Math.sin(angle) * 2;
});
function drawSnake() {
ctx.fillStyle = '#66ffcc';
snake.forEach((part, i) => {
ctx.beginPath();
ctx.arc(part.x, part.y, 10 - i * 0.3, 0, Math.PI * 2);
ctx.fill();
});
}
function drawFood() {
foods.forEach(f => {
ctx.fillStyle = '#ff6666';
ctx.beginPath();
ctx.arc(f.x, f.y, 6, 0, Math.PI * 2);
ctx.fill();
});
}
function update() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
// Check food collision
foods.forEach((f, index) => {
const dist = Math.hypot(head.x - f.x, head.y - f.y);
if (dist < 10) {
foods.splice(index, 1);
foods.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height
});
}
});
snake.pop();
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
update();
drawFood();
drawSnake();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script></body>
</html>1
1
2KB
2KB
121.0ms
132.0ms
121.0ms