forked from Beat0154/FlappyBird
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (41 loc) · 1.42 KB
/
script.js
File metadata and controls
43 lines (41 loc) · 1.42 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
var block = document.getElementById("block");
var hole = document.getElementById("hole");
var character = document.getElementById("character");
var jumping = 0;
var counter = 0;
hole.addEventListener('animationiteration', () => {
var random = -((Math.random()*300)+150);
hole.style.top = random + "px";
counter++;
});
setInterval(function(){
var characterTop = parseInt(window.getComputedStyle(character).getPropertyValue("top"));
if(jumping==0){
character.style.top = (characterTop+3)+"px";
}
var blockLeft = parseInt(window.getComputedStyle(block).getPropertyValue("left"));
var holeTop = parseInt(window.getComputedStyle(hole).getPropertyValue("top"));
var cTop = -(500-characterTop);
if((characterTop>480)||((blockLeft<20)&&(blockLeft>-50)&&((cTop<holeTop)||(cTop>holeTop+130)))){
alert("Game over. Score: "+(counter));
location.reload();
character.style.top = 100 + "px";
counter=0;
}
},10);
function jump(){
jumping = 1;
let jumpCount = 0;
var jumpInterval = setInterval(function(){
var characterTop = parseInt(window.getComputedStyle(character).getPropertyValue("top"));
if((characterTop>6)&&(jumpCount<15)){
character.style.top = (characterTop-5)+"px";
}
if(jumpCount>20){
clearInterval(jumpInterval);
jumping=0;
jumpCount=0;
}
jumpCount++;
},10);
}