-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
320 lines (259 loc) · 9.7 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Car-Game</title>
</head>
<style>
* {
padding: 0px;
margin: 0px;
}
body {
background: url('assets/grass.png') no-repeat center;
background-size: cover;
height: 100vh;
display: grid;
place-items: center;
}
.carimg {
width: 14vw;
position: absolute;
border: 1px red solid;
}
.mycarimg {
width: 13vw;
top: 60vh;
position: relative;
border: 1px red solid;
}
#mycar {
top: 50vh;
left: 0vw;
}
#road {
background: url(assets/road.jpg) center repeat-y;
height: 1200vh;
width: 50vw;
position: relative;
top: -100vh;
z-index: -9;
}
#score {
font-size: 2rem;
top: 20px;
left: 20px;
position: absolute;
}
#start {
position: absolute;
font-size: 1rem;
top: 50vh;
left: 47vw;
padding: 10px;
border-radius: 50px;
background-color: rgb(244, 135, 19);
z-index: 102;
}
#enemycar1 {
display: grid;
place-items: center;
position: relative;
left: 21vw;
z-index: 101;
}
#enemycar2 {
display: grid;
place-items: center;
position: relative;
left: 7vw;
z-index: 101;
}
#enemycar3 {
display: grid;
place-items: center;
position: relative;
left: -7vw;
z-index: 101;
}
#enemycar4 {
display: grid;
place-items: center;
position: relative;
left: -21vw;
z-index: 101;
}
@keyframes car1 {
0% {
top: 0vh;
}
45% {
top: 43vh;
}
100% {
top: 100vh;
}
}
@keyframes car2 {
0% {
top: 0vh;
}
45% {
top: 70vh;
}
100% {
top: 100vh;
}
}
@keyframes car3 {
0% {
top: 0vh;
}
45% {
top: 20vh;
}
100% {
top: 100vh;
}
}
@keyframes car4 {
0% {
top: 0vh;
}
45% {
top: 44vh;
}
100% {
top: 100vh;
}
}
</style>
<body>
<div id="score">Score: 0</div>
<button id="start" onclick="playAudio()">Start</button>
<div>
<div id="enemycar1">
<img style="transform: rotate(90deg);" id="mycarimg1" class="carimg" src="assets/car2.png" alt="">
</div>
<div id="enemycar2">
<img style="transform: rotate(-90deg);" id="mycarimg2" class="carimg" src="assets/car3.png" alt="">
</div>
<div id="enemycar3">
<img style="transform: rotate(90deg);" id="mycarimg3" class="carimg" src="assets/car2.png" alt="">
</div>
<div id="enemycar4">
<img style="transform: rotate(-90deg);" id="mycarimg4" class="carimg" src="assets/car3.png" alt="">
</div>
<div id="mycar"><img style="transform: rotate(90deg);" id="mycarimg" class="mycarimg" src="assets/car1.png"
alt=""></div>
</div>
<div id="road"></div>
<audio autoplay loop>
<source src="assets/sound.mp3" type="audio/mp3" />
</audio>
</body>
<audio id="myAudio" src="assets/sound.mp3"></audio>
<script>
function playAudio() {
const audio = document.getElementById('myAudio');
audio.play();
}
let n = 0
document.getElementById('start').addEventListener("click", function () {
setInterval(() => {
num1 = Math.floor(Math.random() * (21 - 7 + 1) + 7)
document.getElementById("enemycar1").style.left = `${num1}vw`
}, 7000)
setInterval(() => {
num2 = Math.floor(Math.random() * (21 - 7 + 1) + 7)
document.getElementById("enemycar2").style.left = `${num2}vw`
}, 6000)
setInterval(() => {
num3 = Math.floor(Math.random() * (21 - 7 + 1) + 7)
document.getElementById("enemycar3").style.left = `-${num3}vw`
}, 5000)
setInterval(() => {
num4 = Math.floor(Math.random() * (21 - 7 + 1) + 7)
document.getElementById("enemycar4").style.left = `-${num4}vw`
}, 4000)
document.getElementById('start').style.display = 'none'
document.getElementById('enemycar1').style.animation = 'car1 7s linear infinite'
document.getElementById('enemycar2').style.animation = 'car2 6s linear infinite'
document.getElementById('enemycar3').style.animation = 'car3 5s linear infinite'
document.getElementById('enemycar4').style.animation = 'car4 4s linear infinite'
setInterval(() => {
document.getElementById('score').innerText = `Score: ${n}`
n = n + 1
var ecar1_left = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().left)
var ecar2_left = Math.abs(document.getElementById('enemycar2').getBoundingClientRect().left)
var ecar3_left = Math.abs(document.getElementById('enemycar3').getBoundingClientRect().left)
var ecar4_left = Math.abs(document.getElementById('enemycar4').getBoundingClientRect().left)
var ecar1_right = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().right)
var ecar2_right = Math.abs(document.getElementById('enemycar2').getBoundingClientRect().right)
var ecar3_right = Math.abs(document.getElementById('enemycar3').getBoundingClientRect().right)
var ecar4_right = Math.abs(document.getElementById('enemycar4').getBoundingClientRect().right)
var ecar1_top = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().top)
var ecar2_top = Math.abs(document.getElementById('enemycar2').getBoundingClientRect().top)
var ecar3_top = Math.abs(document.getElementById('enemycar3').getBoundingClientRect().top)
var ecar4_top = Math.abs(document.getElementById('enemycar4').getBoundingClientRect().top)
var ecar1_bottom = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().bottom)
var ecar2_bottom = Math.abs(document.getElementById('enemycar2').getBoundingClientRect().bottom)
var ecar3_bottom = Math.abs(document.getElementById('enemycar3').getBoundingClientRect().bottom)
var ecar4_bottom = Math.abs(document.getElementById('enemycar4').getBoundingClientRect().bottom)
var mycar_left = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().left)
var mycar_right = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().left)
var mycar_top = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().left)
var mycar_bottom = Math.abs(document.getElementById('enemycar1').getBoundingClientRect().left)
if (mycar_left > 50 || mycar_right < -50) {
setTimeout(() => {
alert(`GAME OVER!`)
})
location.reload()
}
if (((ecar1_left < mycar_left && mycar_left < ecar1_right) || (ecar1_left < mycar_right && mycar_right < ecar1_right)) && ((ecar1_top < mycar_top && mycar_top < ecar1_bottom) || (ecar1_top < mycar_bottom && mycar_bottom < ecar1_bottom))) {
setTimeout(() => {
alert(`GAME OVER!`)
})
location.reload()
}
if (((ecar2_left < mycar_left && mycar_left < ecar2_right) || (ecar2_left < mycar_right && mycar_right < ecar2_right)) && ((ecar2_top < mycar_top && mycar_top < ecar2_bottom) || (ecar2_top < mycar_bottom && mycar_bottom < ecar2_bottom))) {
setTimeout(() => {
alert(`GAME OVER!`)
})
location.reload()
}
if (((ecar3_left < mycar_left && mycar_left < ecar3_right) || (ecar3_left < mycar_right && mycar_right < ecar3_right)) && ((ecar3_top < mycar_top && mycar_top < ecar3_bottom) || (ecar3_top < mycar_bottom && mycar_bottom < ecar3_bottom))) {
setTimeout(() => {
alert(`GAME OVER!`)
})
location.reload()
}
if (((ecar4_left < mycar_left && mycar_left < ecar4_right) || (ecar4_left < mycar_right && mycar_right < ecar4_right)) && ((ecar4_top < mycar_top && mycar_top < ecar4_bottom) || (ecar4_top < mycar_bottom && mycar_bottom < ecar4_bottom))) {
setTimeout(() => {
alert(`GAME OVER!`)
})
location.reload()
}
}, 1000)
let t = 60
let l = 0
window.addEventListener("keydown", function (x) {
if (x.keyCode == 87) {
t = t - 2;
}
if (x.keyCode == 65) {
l = l - 2;
}
if (x.keyCode == 83) {
t = t + 2;
}
if (x.keyCode == 68) {
l = l + 2;
}
document.getElementById("mycarimg").style.top = `${t}vh`
document.getElementById("mycarimg").style.left = `${l}vw`
})
})
</script>
</html>