Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
164 lines (139 loc) · 4.03 KB
/
Copy pathgame.js
File metadata and controls
164 lines (139 loc) · 4.03 KB
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
const gameBoard = document.getElementById('game-board')
const BOARD_SIZE = 21
const EXPAND_AMOUNT = 3
let newSnakeSegments = 0
let lastRenderTime = 0
let inputDirection = { x: 0, y: 0 }
let lastInputDirection = { x: 0, y: 0 }
let gameOver = false
let snake = [{ x: 11, y: 11 }]
let food = getRandomFoodPosition()
document.addEventListener('keydown', e => {
switch (e.key) {
case 'ArrowUp':
if (lastInputDirection.y !== 0) break;
inputDirection = { x: 0, y: -1 };
break;
case 'ArrowDown':
if (lastInputDirection.y !== 0) break;
inputDirection = { x: 0, y: 1 };
break;
case 'ArrowLeft':
if (lastInputDirection.x !== 0) break;
inputDirection = { x: -1, y: 0 };
break;
case 'ArrowRight':
if (lastInputDirection.x !== 0) break;
inputDirection = { x: 1, y: 0 };
break;
}
lastInputDirection = inputDirection;
});
function main(currentTime) {
if (gameOver) {
setTimeout(() => {
if (confirm('You lost. Press OK to restart.')) {
resetGame();
}
}, 2000);
return;
}
window.requestAnimationFrame(main);
const secondsSinceLastRender = (currentTime - lastRenderTime) / 1000;
if (secondsSinceLastRender < 1 / 5) return;
lastRenderTime = currentTime;
update();
draw();
}
window.requestAnimationFrame(main);
function update() {
updateSnake()
updateFood()
updateFail()
}
function updateFail() {
if (isSnakeOutOfBoard() || isSnakeCollision()) gameOver = true
}
function isSnakeCollision() {
if (onSnake(snake[0], true)) return true
}
function isSnakeOutOfBoard() {
return ((snake[0].x <= 0) || (snake[0].x >= BOARD_SIZE) ||
(snake[0].y <= 0) || (snake[0].y >= BOARD_SIZE))
}
function onSnake(position, ignoreHead = false) {
return snake.some((segment, index) => {
if (ignoreHead && index == 0) return false
return ((position.x === segment.x) && (position.y === segment.y))
})
}
function expandSnake() {
for (let i = 0; i < newSnakeSegments; i++) {
snake.push({ ...snake[snake.length - 1] })
}
newSnakeSegments = 0;
}
function updateSnake() {
expandSnake()
for (let i = snake.length - 2; i >= 0; i--) {
snake[i + 1] = { ...snake[i] }
}
snake[0].x += inputDirection.x
snake[0].y += inputDirection.y
}
function updateFood() {
if (onSnake(food)) {
newSnakeSegments += EXPAND_AMOUNT
food = getRandomFoodPosition()
}
}
function randomFoodPosition() {
return {
x: Math.floor(Math.random() * BOARD_SIZE) + 1,
y: Math.floor(Math.random() * BOARD_SIZE) + 1,
}
}
function getRandomFoodPosition() {
let newFoodPosition
while (newFoodPosition == null || onSnake(newFoodPosition)) {
newFoodPosition = randomFoodPosition()
}
return newFoodPosition
}
function draw() {
gameBoard.innerHTML = ''
drawSnake(gameBoard)
drawFood(gameBoard)
}
function resetGame() {
newSnakeSegments = 0;
gameOver = false;
snake = [{ x: 11, y: 11 }];
food = getRandomFoodPosition();
lastRenderTime = 0;
inputDirection = { x: 0, y: 0 };
lastInputDirection = { x: 0, y: 0 };
window.requestAnimationFrame(main);
}
// function resetGame() {
// if (confirm("Do you want to play again?")) {
// window.location = '/'
// }
// return
// }
export function drawSnake(gameBoard) {
snake.forEach(segment => {
const snakeElement = document.createElement('div');
snakeElement.style.gridRowStart = segment.y;
snakeElement.style.gridColumnStart = segment.x;
snakeElement.classList.add('snake');
gameBoard.appendChild(snakeElement);
});
}
export function drawFood(gameBoard) {
const foodElement = document.createElement('div');
foodElement.style.gridRowStart = food.y;
foodElement.style.gridColumnStart = food.x;
foodElement.classList.add('food');
gameBoard.appendChild(foodElement);
}