Uko67's picture
Add 3 files
b4f8b37 verified
Raw History Blame
5.43 kB
<!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>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
canvas {
background-color: #111827;
border: 2px solid #374151;
}
@keyframes pop {
from {
transform: scale(0.8);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
.popup {
animation: pop 0.3s ease-out forwards;
}
</style>
</head>
<body class="bg-gray-900 text-white flex flex-col items-center justify-center min-h-screen">
<h1 class="text-4xl font-bold mb-4">🐍 Snake Game</h1>
<div class="border border-gray-600 rounded-lg shadow-2xl bg-gray-800 p-4">
<canvas id="gameCanvas" width="400" height="400" class="border border-gray-500"></canvas>
<div class="mt-4 text-center space-y-2">
<p class="text-lg font-semibold">Score: <span id="score" class="text-green-400">0</span></p>
<button onclick="restartGame()" class="hidden px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-md text-white transition" id="restartBtn">
Restart Game
</button>
</div>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{ x: 10, y: 10 }];
let apple = { x: 15, y: 15 };
let velocity = { x: 0, y: 0 };
let gameInterval;
let score = 0;
let gameOver = false;
function drawTile(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * gridSize, y * gridSize, gridSize - 2, gridSize - 2);
}
function drawSnake() {
snake.forEach((segment, index) => {
drawTile(segment.x, segment.y, index === 0 ? "#10B981" : "#34D399");
});
}
function drawApple() {
drawTile(apple.x, apple.y, "#EF4444");
}
function spawnApple() {
apple = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount),
};
// Prevent apple from spawning on the snake
while (snake.some(segment => segment.x === apple.x && segment.y === apple.y)) {
apple = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount),
};
}
}
function updateScore() {
document.getElementById("score").textContent = score;
}
function restartGame() {
snake = [{ x: 10, y: 10 }];
velocity = { x: 0, y: 0 };
score = 0;
updateScore();
gameOver = false;
document.getElementById("restartBtn").classList.add("hidden");
gameLoop();
}
function handleGameOver() {
gameOver = true;
document.getElementById("restartBtn").classList.remove("hidden");
ctx.fillStyle = "rgba(0,0,0,0.6)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#FFF";
ctx.font = "30px Arial";
ctx.textAlign = "center";
ctx.fillText("Game Over", canvas.width / 2, canvas.height / 2);
}
function isSelfColliding(head, body) {
return body.some(seg => seg.x === head.x && seg.y === head.y);
}
function gameLoop() {
const newHead = { x: snake[0].x + velocity.x, y: snake[0].y + velocity.y };
// Collision detection
if (
newHead.x < 0 || newHead.x >= tileCount ||
newHead.y < 0 || newHead.y >= tileCount ||
isSelfColliding(newHead, snake)
) {
handleGameOver();
return;
}
snake.unshift(newHead);
// Eat apple
if (newHead.x === apple.x && newHead.y === apple.y) {
score += 10;
updateScore();
spawnApple();
} else {
snake.pop();
}
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawApple();
drawSnake();
}
document.addEventListener("keydown", (e) => {
if (gameOver) return;
if (e.key === "ArrowUp" && velocity.y === 0) {
velocity = { x: 0, y: -1 };
} else if (e.key === "ArrowDown" && velocity.y === 0) {
velocity = { x: 0, y: 1 };
} else if (e.key === "ArrowLeft" && velocity.x === 0) {
velocity = { x: -1, y: 0 };
} else if (e.key === "ArrowRight" && velocity.x === 0) {
velocity = { x: 1, y: 0 };
}
});
// Start game
spawnApple();
document.getElementById("score").textContent = score;
gameInterval = setInterval(gameLoop, 100);
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-qwensite.hf.space/logo.svg" alt="qwensite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-qwensite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >QwenSite</a> - 🧬 <a href="https://enzostvs-qwensite.hf.space?remix=Uko67/https-huggingface-co-api-repos" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>