forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeGame.html
147 lines (129 loc) · 3.57 KB
/
snakeGame.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
145
146
147
<!DOCTYPE html>
<html>
<head>
<title>Snake Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
canvas {
border: 1px solid #000;
margin: 20px auto;
}
</style>
</head>
<body>
<h1>Snake Game</h1>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<button id="tryAgain" style="display: none">Try Again</button>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const tryAgainButton = document.getElementById("tryAgain");
const gridSize = 20;
const snakeColor = "#000";
const foodColor = "#f00";
let snake = [{ x: 5, y: 5 }];
let food = { x: 10, y: 10 };
let dx = 1;
let dy = 0;
let gameInterval;
function drawSnake() {
snake.forEach((segment) => {
ctx.fillStyle = snakeColor;
ctx.fillRect(
segment.x * gridSize,
segment.y * gridSize,
gridSize,
gridSize
);
});
}
function drawFood() {
ctx.fillStyle = foodColor;
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
}
function moveSnake() {
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
food = generateFood();
} else {
snake.pop();
}
}
function generateFood() {
const newFood = {
x: Math.floor(Math.random() * (canvas.width / gridSize)),
y: Math.floor(Math.random() * (canvas.height / gridSize)),
};
return newFood;
}
function checkCollision() {
const head = snake[0];
if (
head.x < 0 ||
head.x >= canvas.width / gridSize ||
head.y < 0 ||
head.y >= canvas.height / gridSize ||
snake
.slice(1)
.some((segment) => segment.x === head.x && segment.y === head.y)
) {
gameOver();
}
}
function gameOver() {
clearInterval(gameInterval);
tryAgainButton.style.display = "block";
alert("Game Over!");
}
function startGame() {
snake = [{ x: 5, y: 5 }];
food = generateFood();
dx = 1;
dy = 0;
gameInterval = setInterval(gameLoop, 100);
tryAgainButton.style.display = "none";
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSnake();
drawFood();
moveSnake();
checkCollision();
}
document.addEventListener("keydown", (e) => {
switch (e.key) {
case "ArrowUp":
if (dy === 0) {
dx = 0;
dy = -1;
}
break;
case "ArrowDown":
if (dy === 0) {
dx = 0;
dy = 1;
}
break;
case "ArrowLeft":
if (dx === 0) {
dx = -1;
dy = 0;
}
break;
case "ArrowRight":
if (dx === 0) {
dx = 1;
dy = 0;
}
break;
}
});
tryAgainButton.addEventListener("click", startGame);
startGame(); // Start the game initially.
</script>
</body>
</html>