-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflappy.js
99 lines (83 loc) · 3.14 KB
/
flappy.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
var track = 0, previous = null;
function GetBlocks()
{
const width = '8vw';
let height = null;
let MinHeight = 20;
let MaxHeight = 60;
let BirdHeight = 25;
let Range = 10;
if (previous != null)
{
let low = Math.max(MinHeight, previous - Range);
let high = Math.min(previous + Range, MaxHeight);
height = ((Math.random() * 100) % (high - low)) + low;
}
else
{
height = ((Math.random() * 200) % 40) + MinHeight;
}
const block = document.createElement("div");
block.classList.add('Block');
document.getElementsByClassName('Obstacles')[0].append(block);
block.style.height = height + 'vh';
block.style.width = width;
previous = height;
track++;
const GroundHeight = 100 - height - BirdHeight - 10 - ((Math.random() * 100) % 7);
const ground = document.createElement("div");
ground.classList.add("Ground");
document.getElementsByClassName('Obstacles')[0].append(ground);
ground.style.height = GroundHeight + 'vh';
ground.style.width = width;
let elapsedTime = 0;
const intervalId = setInterval(() => {
elapsedTime += 0.1;
if (elapsedTime >= 3.2)
{
let cube = document.getElementsByClassName("Cube")[0];
const BlockRect = block.getBoundingClientRect();
const { top : BlockTop, left : BlockLeft, bottom : BlockBottom, right : BlockRight} = BlockRect;
const CubeRect = cube.getBoundingClientRect();
const { top : CubeTop, left : CubeLeft, bottom : CubeBottom, right : CubeRight} = CubeRect;
const GroundRect = ground.getBoundingClientRect();
const { top : GroundTop, left : GroundLeft, bottom : GroundBottom, right : GroundRight} = GroundRect;
if((CubeRight >= BlockLeft && CubeRight <= BlockRight) || (CubeLeft <= BlockRight && CubeLeft >= BlockLeft) || (CubeLeft == BlockLeft && CubeRight == BlockRight))
{
if(CubeTop <= BlockBottom)
{
clearInterval(BlockInterval);
clearInterval(MoveCube);
}
}
if((CubeRight >= GroundLeft && CubeRight <= GroundRight) || (CubeLeft <= GroundRight && CubeLeft >= GroundLeft) || (CubeLeft == GroundLeft && CubeRight == GroundRight))
{
if(CubeBottom >= GroundTop)
{
clearInterval(BlockInterval);
clearInterval(MoveCube);
}
}
}
}, 100);
}
let BlockInterval = setInterval(GetBlocks, 1000)
let CTop = 45;
let MoveCube = setInterval( () =>
{
var cube = document.getElementsByClassName("Cube")[0];
CTop += 0.1;
cube.style.marginTop = CTop + 'vh';
}, 2)
let CheckEnd = setInterval( () => {
if(CTop < -10 || CTop > 84)
{
clearInterval(MoveCube);
clearInterval(BlockInterval);
}
}, 200)
function KeepUp()
{
CTop -= 10;
}
document.addEventListener('keyup', KeepUp);