-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
253 lines (226 loc) · 6.89 KB
/
script.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Functional utilities
const getElement = (id) => document.getElementById(id);
const createGameElement = (tag, className) => {
const element = document.createElement(tag);
element.classList.add(className);
return element;
};
const setPosition = (element, position) => {
element.style.gridColumn = position.x;
element.style.gridRow = position.y;
};
const generateRandomCoordinate = (gridSize) => Math.floor(Math.random() * gridSize) + 1;
const generateFood = (gridSize) => ({ x: generateRandomCoordinate(gridSize), y: generateRandomCoordinate(gridSize) });
const drawSnake = (board, snake) => {
snake.forEach((segment) => {
const snakeElement = createGameElement("div", "snake");
setPosition(snakeElement, segment);
board.appendChild(snakeElement);
});
};
const drawFood = (board, food, gameStarted) => {
if (gameStarted) {
const foodElement = createGameElement("div", "food");
setPosition(foodElement, food);
board.appendChild(foodElement);
}
};
const updateScore = (scoreElement, snake) => {
const currentScore = snake.length - 1;
scoreElement.textContent = currentScore.toString().padStart(3, "0");
};
const draw = (gameState) => {
gameState.board.textContent = "";
drawSnake(gameState.board, gameState.snake);
drawFood(gameState.board, gameState.food, gameState.gameStarted);
updateScore(gameState.score, gameState.snake);
};
// Game functions
const move = (gameState) => {
const head = { ...gameState.snake[0] };
switch (gameState.direction) {
case "up":
head.y--;
break;
case "down":
head.y++;
break;
case "left":
head.x--;
break;
case "right":
head.x++;
break;
}
gameState.snake.unshift(head);
if (head.x === gameState.food.x && head.y === gameState.food.y) {
gameState.food = generateFood(gameState.gridSize);
increaseSpeed(gameState);
clearInterval(gameState.gameInterval); // Clear past interval
gameState.gameInterval = setInterval(() => {
move(gameState);
checkCollision(gameState);
draw(gameState);
}, gameState.gameSpeedDelay);
} else {
gameState.snake.pop();
}
};
const increaseSpeed = (gameState) => {
if (gameState.gameSpeedDelay > 25) {
gameState.gameSpeedDelay -=
gameState.gameSpeedDelay > 150
? 5
: gameState.gameSpeedDelay > 100
? 3
: gameState.gameSpeedDelay > 50
? 2
: 1;
}
};
const resetGame = (gameState) => {
updateHighScore(gameState);
stopGame(gameState);
gameState.snake = [{ x: 10, y: 10 }];
gameState.food = generateFood(gameState.gridSize);
gameState.direction = "right";
gameState.gameSpeedDelay = 200;
updateScore(gameState.score, gameState.snake);
};
const stopGame = (gameState) => {
clearInterval(gameState.gameInterval);
gameState.gameStarted = false;
gameState.instructionText.style.display = "block";
gameState.link.style.display = "block";
};
const updateHighScore = (gameState) => {
const currentScore = gameState.snake.length - 1;
if (currentScore > gameState.highScore) {
gameState.highScore = currentScore;
gameState.highScoreText.textContent = gameState.highScore.toString().padStart(3, "0");
}
gameState.highScoreText.style.display = "block";
};
const isOppositeDirection = (dir1, dir2) => (
(dir1 === "up" && dir2 === "down") ||
(dir1 === "down" && dir2 === "up") ||
(dir1 === "left" && dir2 === "right") ||
(dir1 === "right" && dir2 === "left")
);
// Event listener for pointerdown, pointermove, and pointerup
const handleInput = (gameState, event) => {
let newDirection;
if (
!gameState.gameStarted &&
((event.type === "pointerdown" && isClickInsideBoard(event, gameState)) ||
(event.type === "keydown" && event.key === " "))
) {
startGame(gameState);
} else if (gameState.gameStarted) {
switch (event.type) {
case "keydown":
newDirection = getDirectionFromKey(event.key);
break;
case "pointerdown":
if (isClickInsideBoard(event, gameState)) {
gameState.isDragging = true;
setDragStart(event, gameState);
}
break;
case "pointermove":
if (gameState.isDragging) {
newDirection = setDirectionFromDrag(event, gameState);
}
break;
case "pointerup":
gameState.isDragging = false;
break;
}
if (newDirection !== undefined && !isOppositeDirection(newDirection, gameState.direction)) {
gameState.direction = newDirection;
return;
}
}
};
const getDirectionFromKey = (key) => {
switch (key) {
case "ArrowUp":
return "up";
case "ArrowDown":
return "down";
case "ArrowLeft":
return "left";
case "ArrowRight":
return "right";
default:
return gameState.direction;
}
};
const setDragStart = (event, gameState) => {
gameState.dragStartX = event.clientX;
gameState.dragStartY = event.clientY;
};
const isClickInsideBoard = (event, gameState) => {
const rect = gameState.board.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
return x >= 0 && x <= rect.width && y >= 0 && y <= rect.height;
};
const setDirectionFromDrag = (event, gameState) => {
const deltaX = event.clientX - gameState.dragStartX;
const deltaY = event.clientY - gameState.dragStartY;
return Math.abs(deltaX) > Math.abs(deltaY)
? deltaX > 0
? "right"
: "left"
: deltaY > 0
? "down"
: "up";
};
// Game functions
const startGame = (gameState) => {
gameState.gameStarted = true;
gameState.instructionText.style.display = "none";
gameState.link.style.display = "none";
gameState.gameInterval = setInterval(() => {
move(gameState);
checkCollision(gameState);
draw(gameState);
}, gameState.gameSpeedDelay);
};
// Game functions
const checkCollision = (gameState) => {
const head = gameState.snake[0];
if (head.x < 1 || head.x > gameState.gridSize || head.y < 1 || head.y > gameState.gridSize) {
resetGame(gameState);
}
for (let i = 1; i < gameState.snake.length; i++) {
if (head.x === gameState.snake[i].x && head.y === gameState.snake[i].y) {
resetGame(gameState);
}
}
};
// Initial game state
const gameState = {
board: getElement("game-board"),
instructionText: getElement("instruction-text"),
score: getElement("score"),
highScoreText: getElement("highScore"),
link: getElement("extLink"),
gridSize: 20,
snake: [{ x: 10, y: 10 }],
food: generateFood(20),
highScore: 0,
direction: "right",
gameInterval: null,
gameSpeedDelay: 200,
gameStarted: false,
isDragging: false,
dragStartX: 0,
dragStartY: 0,
};
// Event listeners
document.addEventListener("keydown", handleInput.bind(null, gameState));
document.addEventListener("pointerdown", handleInput.bind(null, gameState));
document.addEventListener("pointermove", handleInput.bind(null, gameState));
document.addEventListener("pointerup", handleInput.bind(null, gameState));