-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
270 lines (228 loc) · 6.83 KB
/
index.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<html>
<head>
<style>
#container {
display: flex;
justify-content: space-between;
width: 600px;
}
.street {
height: 800px;
width: 105px;
background-color: rgb(50,50,50);
margin: 10px;
position: relative;
}
body {
background-color: black;
}
.car {
height: 20px;
width: 50px;
background-color: white;
position: absolute;
bottom: 0;
left: 0;
}
#scoreboard {
display: flex;
justify-content: space-around;
width: 600px;
font-family: Arial, sans-serif;
font-size: 20px;
}
</style>
</head>
<body>
<div id="scoreboard">
<div id="time">Time elapsed: 0 seconds</div>
<div id="collisions">Collisions: 0</div>
<div id="removals">Removals: 0</div>
</div>
<div id="container">
<div class="street">
<div class="car"></div>
</div>
<div class="street">
<div class="car"></div>
</div>
<div class="street">
<div class="car"></div>
</div>
<div class="street">
<div class="car"></div>
</div>
</div>
<script>
// get the array of street divs
var streets = document.getElementsByClassName("street");
for (let i = 0; i < streets.length; i++){
streets[i].addEventListener("pointerdown", e => {
moveCar(i);
})
}
// get the array of car divs
var cars = document.getElementsByClassName("car");
// add an event listener for key presses
document.addEventListener("keydown", function(event) {
// get the key code
var key = event.keyCode;
// check which key was pressed and move the corresponding car
switch (key) {
case 81: // q key
moveCar(0); // move the car in street 0
break;
case 87: // w key
moveCar(1); // move the car in street 1
break;
case 69: // e key
moveCar(2); // move the car in street 2
break;
case 82: // r key
moveCar(3); // move the car in street 3
break;
default:
break;
}
});
// define a function to move a car by 50px to the right or left
function moveCar(index) {
// get the current left position of the car
var left = parseInt(cars[index].style.left) || 0;
// check if the car has already been moved right or not
if (left == 0) {
// move the car right by 50px
cars[index].style.left = "55px";
} else {
// move the car left by 50px
cars[index].style.left = "0px";
}
}
// define a function to generate an obstacle on a random street
function generateObstacle() {
// get a random index between 0 and 3
var index = Math.floor(Math.random() * 4);
// create a new div element for the obstacle
var obstacle = document.createElement("div");
// set the style of the obstacle
obstacle.style.height = "20px";
obstacle.style.width = "50px";
obstacle.style.backgroundColor = "red";
obstacle.style.position = "absolute";
obstacle.style.top = "0px";
if(Math.random() > .5) obstacle.style.left = "0px";
else obstacle.style.left = "55px";
// append the obstacle to the street div
streets[index].appendChild(obstacle);
obstacle.classList.add("obstacle")
// return the obstacle element
return obstacle;
}
// define a function to move an obstacle downward at a random speed
function moveObstacle(obstacle) {
// get a random speed between 1 and 30 px per second
var speed = Math.floor(Math.random() * 1) + 5;
// set an interval to update the position of the obstacle every second
var interval = setInterval(function() {
// get the current top position of the obstacle
var top = parseInt(obstacle.style.top);
// check if the obstacle has reached the bottom of the street or not
if (top >= 800) {
// clear the interval and remove the obstacle from the street
clearInterval(interval);
obstacle.remove();
updateRemovals();
} else {
// increase the top position by the speed
top += speed;
// set the new top position of the obstacle
obstacle.style.top = top + "px";
}
}, 10);
}
// generate and move an obstacle every 5 seconds
setInterval(function() {
var obstacle = generateObstacle();
moveObstacle(obstacle);
}, 500);
// define a function to check if two elements are overlapping
function isOverlapping(element1, element2) {
// get the bounding rectangles of the elements
var rect1 = element1.getBoundingClientRect();
var rect2 = element2.getBoundingClientRect();
// check if the rectangles are intersecting
return !(
rect1.right < rect2.left ||
rect1.left > rect2.right ||
rect1.bottom < rect2.top ||
rect1.top > rect2.bottom
);
}
// define a function to check if any obstacle has collided with any car
function checkCollision() {
// loop through the array of cars
for (var i = 0; i < cars.length; i++) {
// get the current car element
var car = cars[i];
// get the parent street element of the car
var street = car.parentNode;
// get the array of obstacles in the street
var obstacles = street.getElementsByClassName("obstacle");
// loop through the array of obstacles
for (var j = 0; j < obstacles.length; j++) {
// get the current obstacle element
var obstacle = obstacles[j];
// check if the obstacle is overlapping with the car
if (isOverlapping(obstacle, car)) {
// do something when collision happens
obstacle.remove();
updateCollisions();
console.log("Collision detected!");
}
}
}
}
// check for collision every second
setInterval(checkCollision, 20);
let timeDiv = document.getElementById("time")
let collisionsDiv = document.getElementById("collisions")
let removalsDiv = document.getElementById("removals")
let removals = 0
let collisions = 0
// define a function to update the time div with the time elapsed
function updateTime() {
// increase the time by one second
time++;
// update the time div with the formatted time
timeDiv.textContent = "Time elapsed: " + time + " seconds";
}
// define a function to update the collisions div with the number of collisions
function updateCollisions() {
// increase the collisions by one
collisions++;
// update the collisions div with the formatted number
collisionsDiv.textContent = "Collisions: " + collisions;
}
// define a function to update the removals div with the number of removals
function updateRemovals() {
// increase the removals by one
removals++;
// update the removals div with the formatted number
removalsDiv.textContent = "Removals: " + removals;
}
// get the start time of the site load
var startTime = performance.now();
// define a function to update the time div with the time elapsed
function updateTime() {
// get the current time
var currentTime = performance.now();
// calculate the time elapsed in seconds
var timeElapsed = Math.floor((currentTime - startTime) / 1000);
// update the time div with the formatted time
timeDiv.textContent = "Time elapsed: " + timeElapsed + " seconds";
}
// update the time every second
setInterval(updateTime, 1000);
</script>
</body>
</html>