-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
215 lines (181 loc) · 6.34 KB
/
script.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
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
const game = (function (bird, btn, restart, pointElId = "point") {
class Game {
// a set of blocks
#blocks = [
[this.#createBlock("20", "top"), this.#createBlock("50", "bottom")],
[this.#createBlock("30", "top"), this.#createBlock("40", "bottom")],
[this.#createBlock("40", "top"), this.#createBlock("30", "bottom")],
[this.#createBlock("50", "top"), this.#createBlock("20", "bottom")],
].sort(() => 0.5 - Math.random());
#bird;
#firsttime = true;
#points = 0;
#movingInterval;
#show;
constructor(bird, btn, pointElId = "point", restart = "restart") {
// elements
this.btn = document.getElementById(btn);
this.pointEl = document.getElementById(pointElId);
this.screen = document.querySelector(".screen");
this.restartEl = document.getElementById(restart);
this.restartEl.parentNode.style.visibility = "hidden";
// Events
this.btn.addEventListener("click", this.#btnOnClick.bind(this));
this.restartEl.addEventListener("click", this.#restart.bind(this));
// bird object
this.#bird = bird;
}
// Generating blocks
#createBlock(height, position) {
return { height, position };
}
// btn click event handler
#btnOnClick() {
if (!this.#firsttime) return this.#bird.jump();
this.#start();
this.#firsttime = false;
}
// starting or restarting the game
#start() {
this.#bird.continuouslyDropping();
this.#show = setInterval(this.#showBlocks.bind(this), 1500);
this.#movingInterval = setInterval(this.#moving.bind(this), 20);
this.btn.innerText = "Jump!";
}
#restart() {
this.#points = 0;
this.pointEl.innerText = this.#points;
this.btn.innerText = "Start";
document.querySelectorAll(".block").forEach((el) => el.remove());
this.#bird.init();
this.#firsttime = true;
this.restartEl.parentNode.style.visibility = "hidden";
}
// ending the game when it is over
#end() {
clearInterval(this.#movingInterval);
clearInterval(this.#show);
this.#bird.die();
this.restartEl.parentNode.style.visibility = "visible";
this.btn.removeEventListener("click", this.#btnOnClick.bind(this));
}
// generating a div element which has classes and styles of a block
#generateBlock(block) {
const el = document.createElement("div");
el.classList.add("block");
el.classList.add(block.position);
el.style.right = "-30px";
el.style.height = block.height + "%";
return el;
}
// showing blocks to the screen
#showBlocks() {
const blocks = this.#selectRandomBlocks();
blocks.forEach((el) => this.screen.append(this.#generateBlock(el)));
}
// selecting random blocks to show them to the screen
#selectRandomBlocks() {
return this.#blocks[Math.floor(Math.random() * this.#blocks.length)];
}
// making those blocks move
#moving() {
// selecting all the blocks on the screen
const blocks = document.querySelectorAll(".block");
blocks.forEach((el) => {
// moving them
const right = parseInt(el.style.right);
el.style.right = right + 2 + "px";
// adding check class to element if they are near to the bird
// checking whether if the game is over or not
this.#checkIfOver(blocks);
// Condition for adding points
if (right === 380) {
this.#gainPoint();
}
// removing elements for better performance after it passes through the screen
if (right > 420) {
el.remove();
}
});
}
// adding points and showing it to the screen
#gainPoint() {
this.#points += 0.5;
this.pointEl.innerText = this.#points;
}
// checking if the game is over and not by checking each block
#checkIfOver(blocks) {
for (let block of blocks) this.#gameOverLogics(block) && this.#end();
}
// game over conditions
#gameOverLogics(block) {
if (this.#bird.el.classList.contains("die")) return true;
const left = parseInt(block.style.right) + 30;
const screenHeight = parseInt(
window.getComputedStyle(this.screen).height
);
const style = window.getComputedStyle(block);
const blockHeightpercent = (parseInt(style.height) / screenHeight) * 100;
if (left > 345 && left < 382) {
if (
block.classList.contains("top") &&
this.#bird.jumpedHeight + this.#bird.height > 100 - blockHeightpercent
)
return true;
if (
block.classList.contains("bottom") &&
this.#bird.jumpedHeight < blockHeightpercent
)
return true;
}
}
}
class Bird {
jumpedHeight = 50;
// height estimated value (1sf) in percentage
height = 10;
#riseRate = 12;
#dropRate = 2;
#droppingInterval;
constructor(id) {
// getting the element
this.el = document.getElementById(id);
this.el.style.bottom = this.jumpedHeight + "%";
}
// initial state
init() {
this.el.classList.remove("die");
this.jumpedHeight = 50;
this.el.style.bottom = this.jumpedHeight + "%";
}
// making the bird jump
jump() {
// making sure that the bird doesn't fly higher than the box
if (this.jumpedHeight > 92 || !this.jumpedHeight) return;
// slowing it down to make it relastic when it is bounced with or too closed to the upper border
if (this.jumpedHeight > 75) {
this.jumpedHeight += 10;
} else if (this.jumpedHeight < 85) {
// normal when the bird is flying lower than 85%
this.jumpedHeight += this.#riseRate;
}
this.el.style.bottom = this.jumpedHeight + "%";
}
// game over scene for the bird
die() {
this.el.classList.add("die");
this.jumpedHeight = 0;
this.el.style.bottom = this.jumpedHeight + "%";
clearInterval(this.#droppingInterval);
}
// making the bird drop to the ground when it is alive
continuouslyDropping() {
this.#droppingInterval = setInterval(() => {
if (this.jumpedHeight < 0) return this.die();
this.jumpedHeight -= this.#dropRate;
this.el.style.bottom = this.jumpedHeight + "%";
}, 60);
}
}
return new Game(new Bird(bird), btn, pointElId, restart);
})("bird", "jump", (restart = "restart"));