-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (120 loc) · 2.87 KB
/
Copy pathscript.js
File metadata and controls
132 lines (120 loc) · 2.87 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
let score=0;
let fullScreen=false;
const audio=document.createElement("audio");
audio.src="audio1.wav";
const audio1=document.createElement("audio");
audio1.src="audio.wav";
audio.autoplay=true
audio.type="audio/x-wav";
audio1.type="audio/x-wav"
onclick=()=>{
if(!fullScreen){
document.body.requestFullscreen();
fullScreen=true
}
// if(audio.ended){
// audio.play()
//}
}
let random=(max)=>Math.floor(Math.random()*max);
let colors=["blue", "orange","purple","yellow","green","white"];
let color=colors[random(colors.length-1)];
function createElement(x,len=0){
let index=0;
do{
index++
const el=document.createElement("div");
el.setAttribute("id","enemy");
el.style.marginLeft=x+"px";
el.style.marginTop="-150px"
el.style.background=color;
el.y=-150;
el.addEventListener("click",(e)=>{
if(!gameOver)
e.target.remove();
audio1.play()
score++;
});
document.body.appendChild(el);
}
while(index<len);
}
let gameOver=false;
let speed=1;
let createIndex=0;
speed+=0.005;
if(createIndex>360){
createIndex=Math.floor(Math.random()*180);
addEnemy();
}
function addEnemy(){
return createElement(Math.floor(Math.random()*(innerWidth-100)));
}
let animation=null;
function animate(){
createIndex+=speed*2;
const all=document.querySelectorAll("body #enemy")
if(speed>3.5 && all.length <= 0 && score > 12){
gameOver=true
cancelAnimationFrame(animation)
return swal({
allowOutsideClick: false,
title:"You won! | score : "+score,
text:"You rock , your score is "+score,
buttons:"Play Next"
}).then((e)=>{
gameOver=false
speed+=1;
color=colors[random(colors.length-1)];
score=0;
audio.play();
createElement(3);
animate();
})
}
for(let i=0;i<all.length;i++){
const e=all[i];
e.y+=speed;
e.style.marginTop=e.y+"px";
if(Number(e.y)> (innerHeight -170)){
e.setAttribute("class","gameOver")
gameOver=true;
swal({
allowOutsideClick: false,
title:"Game Over | score : "+score,
text:"You fail the game , your score is "+score,
buttons:"Play again"
}).then(()=>{
if (audio.readyState >= 4) {
audio.play();
}
e.remove();
gameOver=false
speed=speed > 1 ? speed=1:speed
all.forEach((e)=>{
e.remove();
})
animate();
color=colors[random(colors.length-1)];
score=0;
})
return cancelAnimationFrame(animation);
}
// console.log(getComputedStyle(e).getPropertyValue("marginTop"),e.style.marginTop);
}
speed+=0.005;
if(createIndex>360){
createIndex=Math.floor(Math.random()*180);
addEnemy();
}
animation=requestAnimationFrame(animate);
}
function tryAgain(){
createElement(2);
animate();
}
const start=document.querySelector(".start");
start.addEventListener("click",(e)=>{
start.remove();
animate()
})