-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
170 lines (147 loc) · 3.54 KB
/
game.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
const gameBoard = document.querySelector(".game-board");
const SNAKE_SPEED = 5;
const EXPANSION_RATE = 3;
const GRID_SIZE = 21;
let inputDirection = { x: 1, y: 0 };
let lastInputDirection = { x: 0, y: 0 };
let newSegment = 0;
let lastRenderTime = 0;
let intervalID = null;
let gameOver = false;
let food = {
x: 10,
y: 10,
};
const snakeBody = [
{
x: 11,
y: 11,
},
];
window.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;
}
});
main();
/* functions */
function main() {
intervalID = setInterval(DOMUpdate, 1000 / (SNAKE_SPEED * snakeBody.length));
}
function DOMUpdate() {
if (gameOver) {
clearInterval(intervalID);
return alert("you loose");
}
update();
draw();
}
function update() {
updateSnake();
updateFood();
checkDeath();
}
function updateSnake() {
addSegments();
const direction = getInputDirection();
console.log("update");
for (let i = snakeBody.length - 2; i >= 0; i--) {
snakeBody[i + 1] = { ...snakeBody[i] };
}
snakeBody[0].x += direction.x;
snakeBody[0].y += direction.y;
}
function updateFood() {
if (onSnake(food)) {
expandSnake(EXPANSION_RATE);
food = getRandomFoodPosition();
}
}
function draw() {
gameBoard.innerHTML = "";
drawSnake();
drawFood();
}
function drawSnake() {
snakeBody.map((segment) => {
const snakeElement = document.createElement("div");
snakeElement.style.gridRowStart = segment.y;
snakeElement.style.gridColumnStart = segment.x;
snakeElement.classList.add("snake");
gameBoard.appendChild(snakeElement);
});
}
function drawFood() {
const foodElement = document.createElement("div");
foodElement.style.gridRowStart = food.y;
foodElement.style.gridColumnStart = food.x;
foodElement.classList.add("food");
gameBoard.appendChild(foodElement);
}
function getInputDirection() {
lastInputDirection = inputDirection;
return inputDirection;
}
function expandSnake(amount) {
newSegment += amount;
}
function onSnake(position, { ignoreHead = false } = {}) {
return snakeBody.some((segment, index) => {
if (ignoreHead && index === 0) return false;
return equalPosition(segment, position);
});
}
function equalPosition(pos1, pos2) {
return pos1.x === pos2.x && pos1.y === pos2.y;
}
function addSegments() {
for (let i = 0; i < newSegment; i++) {
snakeBody.push({ ...snakeBody[snakeBody.length - 1] });
}
newSegment = 0;
}
function getRandomFoodPosition() {
let newFoodPosition;
while (newFoodPosition == null || onSnake(newFoodPosition)) {
newFoodPosition = randomGridPosition();
}
return newFoodPosition;
}
function randomGridPosition() {
return {
x: Math.floor(Math.random() * GRID_SIZE) + 1,
y: Math.floor(Math.random() * GRID_SIZE) + 1,
};
}
function checkDeath() {
gameOver = outsideGrid(getSnakeHead()) || snakeIntersection();
}
function outsideGrid(position) {
return (
position.x < 1 ||
position.x > GRID_SIZE ||
position.y < 1 ||
position.y > GRID_SIZE
);
}
function getSnakeHead() {
return snakeBody[0];
}
function snakeIntersection() {
return onSnake(snakeBody[0], { ignoreHead: true });
}