-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (198 loc) · 5.18 KB
/
index.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
const playerCar = {
x: 32, y: 12, height: '90px', width: '45px',
};
const playerMove = document.querySelector('.playerCar');
playerMove.style.left = '650px';
playerMove.style.top = '240px';
const isThereObstacle = (obstacleCar) => {
const playerX = parseInt(playerCar.x, 10) * 20;
const playerY = parseInt(playerCar.y, 10) * 20;
const playerH = parseInt(playerCar.height, 10);
const playerW = parseInt(playerCar.width, 10);
const obstacleX = parseInt(obstacleCar.style.left, 10);
const obstacleY = parseInt(obstacleCar.style.top, 10);
const obstacleH = parseInt(obstacleCar.style.height, 10);
const obstacleW = parseInt(obstacleCar.style.width, 10);
if (
(playerX < obstacleX + obstacleW)
&& (playerX + playerW > obstacleX)
&& (playerY < obstacleY + obstacleH)
&& (playerY + playerH > obstacleY)
) {
window.location.replace('lose.html');
return true;
}
return false;
};
// timer
let timeNow = 60;
setInterval(() => {
timeNow -= 1;
document.querySelector('.timer').innerHTML = timeNow;
}, 1000);
setInterval(() => {
window.location.replace('win.html');
timeNow = 0;
}, 60000);
// create obstacles
const createBlue = () => {
const obstacleCar = document.createElement('div');
obstacleCar.classList.add('blue-car');
obstacleCar.style.left = `${Math.random() * 1424}px`;
obstacleCar.style.height = '90px';
obstacleCar.style.width = '45px';
document.querySelector('.road').appendChild(obstacleCar);
obstacleCar.y = 0;
const obstacleCarInterval = setInterval(
() => {
if (obstacleCar.y === 625) {
obstacleCar.remove();
clearInterval(obstacleCarInterval);
} else {
isThereObstacle(obstacleCar);
obstacleCar.y += 2;
obstacleCar.style.top = `${obstacleCar.y}px`;
}
},
1,
);
};
const createWhite = () => {
const obstacleCar = document.createElement('div');
obstacleCar.classList.add('white-car');
obstacleCar.style.left = `${Math.random() * 1424}px`;
obstacleCar.style.height = '90px';
obstacleCar.style.width = '45px';
document.querySelector('.road').appendChild(obstacleCar);
obstacleCar.y = 0;
const obstacleCarInterval = setInterval(
() => {
if (obstacleCar.y === 625) {
obstacleCar.remove();
clearInterval(obstacleCarInterval);
} else {
isThereObstacle(obstacleCar);
obstacleCar.y += 4;
obstacleCar.style.top = `${obstacleCar.y}px`;
}
},
1,
);
};
const createRed = () => {
const obstacleCar = document.createElement('div');
obstacleCar.classList.add('red-car');
obstacleCar.style.left = `${Math.random() * 1424}px`;
obstacleCar.style.height = '90px';
obstacleCar.style.width = '45px';
document.querySelector('.road').appendChild(obstacleCar);
obstacleCar.y = 0;
const obstacleCarInterval = setInterval(
() => {
if (obstacleCar.y === 625) {
obstacleCar.remove();
clearInterval(obstacleCarInterval);
} else {
isThereObstacle(obstacleCar);
obstacleCar.y += 2;
obstacleCar.style.top = `${obstacleCar.y}px`;
}
},
1,
);
};
const createGreen = () => {
const obstacleCar = document.createElement('div');
obstacleCar.classList.add('green-car');
obstacleCar.style.left = `${Math.random() * 1424}px`;
obstacleCar.style.height = '90px';
obstacleCar.style.width = '45px';
document.querySelector('.road').appendChild(obstacleCar);
obstacleCar.y = 0;
const obstacleCarInterval = setInterval(
() => {
if (obstacleCar.y === 625) {
obstacleCar.remove();
clearInterval(obstacleCarInterval);
} else {
isThereObstacle(obstacleCar);
obstacleCar.y += 4;
obstacleCar.style.top = `${obstacleCar.y}px`;
}
},
1,
);
};
setInterval(() => {
createBlue();
}, 1000);
setInterval(() => {
createWhite();
}, 1000);
setInterval(() => {
createRed();
}, 1000);
setInterval(() => {
createGreen();
}, 2000);
// move player
const inGrid = (x, y) => {
if (x < 0 || y < 0 || x > 66 || y > 27) {
return false;
}
return true;
};
const onScreen = (x, y) => {
if (!inGrid(x, y)) {
return false;
}
return true;
};
const movePlayerCar = () => {
playerMove.style.left = `${(playerCar.x * 20).toString()}px`;
playerMove.style.top = `${(playerCar.y * 20).toString()}px`;
};
const moveDown = () => {
if (onScreen(playerCar.x, playerCar.y + 1)) {
playerCar.y += 1;
movePlayerCar(playerCar.x, playerCar.y);
}
};
const moveLeft = () => {
if (onScreen(playerCar.x - 1, playerCar.y)) {
playerCar.x -= 2;
movePlayerCar(playerCar.x, playerCar.y);
}
};
const moveUp = () => {
if (onScreen(playerCar.x, playerCar.y - 1)) {
playerCar.y -= 1;
movePlayerCar(playerCar.x, playerCar.y);
}
};
const moveRight = () => {
if (onScreen(playerCar.x + 1, playerCar.y)) {
playerCar.x += 2;
movePlayerCar(playerCar.x, playerCar.y);
}
};
window.addEventListener('keydown', (evt) => {
if ([37, 38, 39, 40].includes(evt.keyCode)) {
evt.preventDefault();
}
switch (evt.keyCode) {
case 37:
moveLeft();
break;
case 38:
moveUp();
break;
case 39:
moveRight();
break;
case 40:
moveDown();
break;
default:
}
});