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 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Snake Game</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
display: flex;
|
| 10 |
+
justify-content: center;
|
| 11 |
+
align-items: center;
|
| 12 |
+
height: 100vh;
|
| 13 |
+
margin: 0;
|
| 14 |
+
background-color: #f0f0f0;
|
| 15 |
+
}
|
| 16 |
+
canvas {
|
| 17 |
+
border: 1px solid black;
|
| 18 |
+
background-color: #fff;
|
| 19 |
+
}
|
| 20 |
+
</style>
|
| 21 |
+
</head>
|
| 22 |
+
<body>
|
| 23 |
+
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
| 24 |
+
|
| 25 |
+
<script>
|
| 26 |
+
const canvas = document.getElementById('gameCanvas');
|
| 27 |
+
const ctx = canvas.getContext('2d');
|
| 28 |
+
const box = 20; // Size of each grid box
|
| 29 |
+
const canvasSize = 400;
|
| 30 |
+
|
| 31 |
+
let snake = [];
|
| 32 |
+
snake[0] = { x: 9 * box, y: 10 * box }; // Initial snake position
|
| 33 |
+
|
| 34 |
+
let food = {
|
| 35 |
+
x: Math.floor(Math.random() * (canvasSize / box)) * box,
|
| 36 |
+
y: Math.floor(Math.random() * (canvasSize / box)) * box
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
let score = 0;
|
| 40 |
+
let direction;
|
| 41 |
+
|
| 42 |
+
document.addEventListener('keydown', setDirection);
|
| 43 |
+
|
| 44 |
+
function setDirection(event) {
|
| 45 |
+
if (event.keyCode == 37 && direction != 'RIGHT') {
|
| 46 |
+
direction = 'LEFT';
|
| 47 |
+
} else if (event.keyCode == 38 && direction != 'DOWN') {
|
| 48 |
+
direction = 'UP';
|
| 49 |
+
} else if (event.keyCode == 39 && direction != 'LEFT') {
|
| 50 |
+
direction = 'RIGHT';
|
| 51 |
+
} else if (event.keyCode == 40 && direction != 'UP') {
|
| 52 |
+
direction = 'DOWN';
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
function collision(head, array) {
|
| 57 |
+
for (let i = 0; i < array.length; i++) {
|
| 58 |
+
if (head.x == array[i].x && head.y == array[i].y) {
|
| 59 |
+
return true;
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
return false;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
function draw() {
|
| 66 |
+
// Clear canvas
|
| 67 |
+
ctx.fillStyle = 'white';
|
| 68 |
+
ctx.fillRect(0, 0, canvasSize, canvasSize);
|
| 69 |
+
|
| 70 |
+
// Draw snake
|
| 71 |
+
for (let i = 0; i < snake.length; i++) {
|
| 72 |
+
ctx.fillStyle = (i == 0) ? 'green' : 'lightgreen';
|
| 73 |
+
ctx.fillRect(snake[i].x, snake[i].y, box, box);
|
| 74 |
+
ctx.strokeStyle = 'darkgreen';
|
| 75 |
+
ctx.strokeRect(snake[i].x, snake[i].y, box, box);
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// Draw food
|
| 79 |
+
ctx.fillStyle = 'red';
|
| 80 |
+
ctx.fillRect(food.x, food.y, box, box);
|
| 81 |
+
|
| 82 |
+
// Old head position
|
| 83 |
+
let snakeX = snake[0].x;
|
| 84 |
+
let snakeY = snake[0].y;
|
| 85 |
+
|
| 86 |
+
// Which direction
|
| 87 |
+
if (direction == 'LEFT') snakeX -= box;
|
| 88 |
+
if (direction == 'UP') snakeY -= box;
|
| 89 |
+
if (direction == 'RIGHT') snakeX += box;
|
| 90 |
+
if (direction == 'DOWN') snakeY += box;
|
| 91 |
+
|
| 92 |
+
// If the snake eats the food
|
| 93 |
+
if (snakeX == food.x && snakeY == food.y) {
|
| 94 |
+
score++;
|
| 95 |
+
food = {
|
| 96 |
+
x: Math.floor(Math.random() * (canvasSize / box)) * box,
|
| 97 |
+
y: Math.floor(Math.random() * (canvasSize / box)) * box
|
| 98 |
+
};
|
| 99 |
+
// Don't remove tail
|
| 100 |
+
} else {
|
| 101 |
+
// Remove the tail
|
| 102 |
+
snake.pop();
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
// New head
|
| 106 |
+
let newHead = {
|
| 107 |
+
x: snakeX,
|
| 108 |
+
y: snakeY
|
| 109 |
+
};
|
| 110 |
+
|
| 111 |
+
// Game over conditions
|
| 112 |
+
if (snakeX < 0 || snakeX >= canvasSize || snakeY < 0 || snakeY >= canvasSize || collision(newHead, snake)) {
|
| 113 |
+
clearInterval(game);
|
| 114 |
+
alert('Game Over! Score: ' + score);
|
| 115 |
+
// Optionally reset game or provide restart button
|
| 116 |
+
document.location.reload(); // Simple reload to restart
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
snake.unshift(newHead);
|
| 120 |
+
|
| 121 |
+
// Draw score
|
| 122 |
+
ctx.fillStyle = 'black';
|
| 123 |
+
ctx.font = '20px Arial';
|
| 124 |
+
ctx.fillText('Score: ' + score, box, 1.6 * box);
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
let game = setInterval(draw, 100); // Game loop speed
|
| 128 |
+
|
| 129 |
+
</script>
|
| 130 |
+
</body>
|
| 131 |
+
</html>
|