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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Beautiful Snake Game</title>
|
| 5 |
+
<style>
|
| 6 |
+
body {
|
| 7 |
+
display: flex;
|
| 8 |
+
justify-content: center;
|
| 9 |
+
align-items: center;
|
| 10 |
+
min-height: 100vh;
|
| 11 |
+
background: linear-gradient(45deg, #1a1a2e, #16213e);
|
| 12 |
+
margin: 0;
|
| 13 |
+
font-family: Arial, sans-serif;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
.game-container {
|
| 17 |
+
background: rgba(255, 255, 255, 0.1);
|
| 18 |
+
padding: 20px;
|
| 19 |
+
border-radius: 15px;
|
| 20 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
canvas {
|
| 24 |
+
background: #0f0f23;
|
| 25 |
+
border-radius: 10px;
|
| 26 |
+
border: 2px solid #4ecca3;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.score {
|
| 30 |
+
color: #fff;
|
| 31 |
+
text-align: center;
|
| 32 |
+
font-size: 24px;
|
| 33 |
+
margin-bottom: 10px;
|
| 34 |
+
text-shadow: 0 0 5px #4ecca3;
|
| 35 |
+
}
|
| 36 |
+
</style>
|
| 37 |
+
</head>
|
| 38 |
+
<body>
|
| 39 |
+
<div class="game-container">
|
| 40 |
+
<div class="score">Score: <span id="score">0</span></div>
|
| 41 |
+
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
| 42 |
+
</div>
|
| 43 |
+
|
| 44 |
+
<script>
|
| 45 |
+
const canvas = document.getElementById('gameCanvas');
|
| 46 |
+
const ctx = canvas.getContext('2d');
|
| 47 |
+
const scoreElement = document.getElementById('score');
|
| 48 |
+
|
| 49 |
+
const gridSize = 20;
|
| 50 |
+
const tileCount = canvas.width / gridSize;
|
| 51 |
+
|
| 52 |
+
let snake = [
|
| 53 |
+
{ x: 10, y: 10 },
|
| 54 |
+
];
|
| 55 |
+
let food = spawnFood();
|
| 56 |
+
let dx = 0;
|
| 57 |
+
let dy = 0;
|
| 58 |
+
let score = 0;
|
| 59 |
+
let gameSpeed = 100;
|
| 60 |
+
let gameLoop;
|
| 61 |
+
|
| 62 |
+
function drawGame() {
|
| 63 |
+
// Move snake
|
| 64 |
+
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
|
| 65 |
+
snake.unshift(head);
|
| 66 |
+
|
| 67 |
+
// Check if snake ate food
|
| 68 |
+
if (head.x === food.x && head.y === food.y) {
|
| 69 |
+
score += 10;
|
| 70 |
+
scoreElement.textContent = score;
|
| 71 |
+
food = spawnFood();
|
| 72 |
+
if (gameSpeed > 50) gameSpeed -= 2; // Increase speed
|
| 73 |
+
} else {
|
| 74 |
+
snake.pop();
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Clear canvas
|
| 78 |
+
ctx.fillStyle = '#0f0f23';
|
| 79 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 80 |
+
|
| 81 |
+
// Draw snake
|
| 82 |
+
snake.forEach((segment, index) => {
|
| 83 |
+
ctx.fillStyle = `hsl(${index * 5}, 70%, 50%)`;
|
| 84 |
+
ctx.beginPath();
|
| 85 |
+
ctx.arc(
|
| 86 |
+
segment.x * gridSize + gridSize / 2,
|
| 87 |
+
segment.y * gridSize + gridSize / 2,
|
| 88 |
+
gridSize / 2 - 1,
|
| 89 |
+
0,
|
| 90 |
+
Math.PI * 2
|
| 91 |
+
);
|
| 92 |
+
ctx.fill();
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
// Draw food
|
| 96 |
+
ctx.fillStyle = '#ff6b6b';
|
| 97 |
+
ctx.beginPath();
|
| 98 |
+
ctx.arc(
|
| 99 |
+
food.x * gridSize + gridSize / 2,
|
| 100 |
+
food.y * gridSize + gridSize / 2,
|
| 101 |
+
gridSize / 2 - 1,
|
| 102 |
+
0,
|
| 103 |
+
Math.PI * 2
|
| 104 |
+
);
|
| 105 |
+
ctx.fill();
|
| 106 |
+
|
| 107 |
+
// Check collision
|
| 108 |
+
if (checkCollision(head)) {
|
| 109 |
+
gameOver();
|
| 110 |
+
return;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
gameLoop = setTimeout(drawGame, gameSpeed);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
function spawnFood() {
|
| 117 |
+
return {
|
| 118 |
+
x: Math.floor(Math.random() * tileCount),
|
| 119 |
+
y: Math.floor(Math.random() * tileCount)
|
| 120 |
+
};
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
function checkCollision(head) {
|
| 124 |
+
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
|
| 125 |
+
return true;
|
| 126 |
+
}
|
| 127 |
+
for (let i = 1; i < snake.length; i++) {
|
| 128 |
+
if (head.x === snake[i].x && head.y === snake[i].y) {
|
| 129 |
+
return true;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
return false;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
function gameOver() {
|
| 136 |
+
clearTimeout(gameLoop);
|
| 137 |
+
alert(`Game Over! Score: ${score}`);
|
| 138 |
+
snake = [{ x: 10, y: 10 }];
|
| 139 |
+
dx = 0;
|
| 140 |
+
dy = 0;
|
| 141 |
+
score = 0;
|
| 142 |
+
gameSpeed = 100;
|
| 143 |
+
scoreElement.textContent = score;
|
| 144 |
+
food = spawnFood();
|
| 145 |
+
drawGame();
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
document.addEventListener('keydown', (e) => {
|
| 149 |
+
switch (e.key) {
|
| 150 |
+
case 'ArrowUp':
|
| 151 |
+
if (dy === 0) { dx = 0; dy = -1; }
|
| 152 |
+
break;
|
| 153 |
+
case 'ArrowDown':
|
| 154 |
+
if (dy === 0) { dx = 0; dy = 1; }
|
| 155 |
+
break;
|
| 156 |
+
case 'ArrowLeft':
|
| 157 |
+
if (dx === 0) { dx = -1; dy = 0; }
|
| 158 |
+
break;
|
| 159 |
+
case 'ArrowRight':
|
| 160 |
+
if (dx === 0) { dx = 1; dy = 0; }
|
| 161 |
+
break;
|
| 162 |
+
}
|
| 163 |
+
});
|
| 164 |
+
|
| 165 |
+
// Start the game
|
| 166 |
+
drawGame();
|
| 167 |
+
</script>
|
| 168 |
+
</body>
|
| 169 |
</html>
|