File size: 5,428 Bytes
b4f8b37 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | <!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> |