-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_game.html
144 lines (128 loc) · 4.53 KB
/
snake_game.html
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
<!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>
<style>
body {
background-color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
canvas {
border: 1px solid #222;
background-color: #111;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
document.addEventListener("DOMContentLoaded", function() {
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Define grid and tile size
const tileSize = 20;
const gridWidth = canvas.width / tileSize;
const gridHeight = canvas.height / tileSize;
// Initial snake position and velocity
let snake = [{ x: 10, y: 10 }];
let dx = 0;
let dy = 0;
// Initial food position
let food = { x: Math.floor(Math.random() * gridWidth), y: Math.floor(Math.random() * gridHeight) };
// Initial score
let score = 0;
// Initial speed and interval
let speed = 100;
let interval = setInterval(gameLoop, speed);
// Main game loop
function gameLoop() {
// Move snake
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
snake.unshift(head);
// Check for collision with food
if (head.x === food.x && head.y === food.y) {
score++;
food = { x: Math.floor(Math.random() * gridWidth), y: Math.floor(Math.random() * gridHeight) };
if (score % 5 === 0) {
clearInterval(interval);
speed -= 10; // Increase speed
interval = setInterval(gameLoop, speed); // Update interval
}
} else {
snake.pop();
}
// Check for collision with walls or self
if (head.x < 0 || head.x >= gridWidth || head.y < 0 || head.y >= gridHeight || checkCollision(head, snake.slice(1))) {
clearInterval(interval);
alert("Game Over! Your Score: " + score);
location.reload(); // Refresh the page to restart the game
return;
}
// Draw everything
draw();
}
// Draw function
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw snake
snake.forEach(segment => {
ctx.fillStyle = "#999";
ctx.fillRect(segment.x * tileSize, segment.y * tileSize, tileSize, tileSize);
ctx.strokeStyle = "#000";
ctx.strokeRect(segment.x * tileSize, segment.y * tileSize, tileSize, tileSize);
});
// Draw food
ctx.fillStyle = "#0F0";
ctx.shadowBlur = 10;
ctx.shadowColor = "#0F0";
ctx.fillRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
// Draw score
ctx.fillStyle = "#FFF";
ctx.font = "20px Arial";
ctx.fillText("Score: " + score, 10, 30);
}
// Check collision function
function checkCollision(pos, array) {
return array.some(segment => segment.x === pos.x && segment.y === pos.y);
}
// Keyboard event listener
document.addEventListener("keydown", function(e) {
const direction = e.key.replace("Arrow", "");
switch (direction) {
case "Up":
if (dy !== 1) {
dx = 0;
dy = -1;
}
break;
case "Down":
if (dy !== -1) {
dx = 0;
dy = 1;
}
break;
case "Left":
if (dx !== 1) {
dx = -1;
dy = 0;
}
break;
case "Right":
if (dx !== -1) {
dx = 1;
dy = 0;
}
break;
}
});
});
</script>
</body>
</html>