-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
239 lines (206 loc) · 6.86 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Variables
let move_speed = 3.5;
let bird_dy = 0;
let gravity = 0.5;
let game_state = 'Start';
var hiscore = JSON.parse(localStorage.getItem("hiscore")) || [];
// DOM
const bird = document.querySelector('.bird');
const button = document.querySelector('.button');
const backgroundImage = document.querySelector('.background');
const computedStyle = window.getComputedStyle(backgroundImage);
const backgroundImagePos = computedStyle.getPropertyValue("background-position");
const score_val = document.querySelector('.score_val');
const message = document.querySelector('.message');
const high_score_val = document.querySelector('.high_score_val');
// SFX
const hitSound = new Audio("assets/bgm/hit.wav");
// Object
let bird_props = bird.getBoundingClientRect();
let background = document.querySelector('.background').getBoundingClientRect();
// Functions
function handleKeyPress(e) {
if (e.key == 'Enter' && game_state != 'Play') {
startGame();
}
}
function handleButtonClick() {
if (game_state != 'Play') {
startGame();
}
}
function handleTouchStart() {
if (game_state === 'Play') {
bird_dy = -9.5;
}
}
function handleKeyUp(e) {
if (game_state === 'Play') {
if (e.key == 'ArrowUp' || e.key == ' ') {
playJumpAudio();
}
}
}
function handleMouseClick() {
if (game_state === 'Play') {
playJumpAudio();
}
}
// Event Listeners
document.addEventListener('keydown', handleKeyPress);
button.addEventListener('click', handleButtonClick);
document.addEventListener('touchstart', handleTouchStart);
document.addEventListener('keyup', handleKeyUp);
document.addEventListener('click', handleMouseClick);
function startGame() {
clearPipes();
bird.style.top = '40vh';
game_state = 'Play';
button.innerHTML = '';
message.innerHTML = '';
score_val.innerHTML = '0';
high_score_val.innerHTML = '';
backgroundImage.style.animation = "none";
backgroundImage.style.animation = "backgroundMove 40s linear infinite";
play();
}
function clearPipes() {
document.querySelectorAll('.pipe_sprite').forEach((e) => {
e.remove();
});
}
function playJumpAudio() {
const jumpSound = new Audio("assets/bgm/jump.mp3");
jumpSound.play();
}
function play() {
function move() {
// Detect if game has ended
if (game_state != 'Play') return;
// Getting reference to all the pipe elements
let pipe_sprite = document.querySelectorAll('.pipe_sprite');
pipe_sprite.forEach((element) => {
let pipe_sprite_props = element.getBoundingClientRect();
bird_props = bird.getBoundingClientRect();
// Delete the pipes if they have moved out
// of the screen hence saving memory
if (pipe_sprite_props.right <= 0) {
element.remove();
} else {
// Collision detection with bird and pipes
if (
bird_props.left + 15 < pipe_sprite_props.left +
pipe_sprite_props.width &&
bird_props.left - 10 +
bird_props.width > pipe_sprite_props.left &&
bird_props.top + 10 < pipe_sprite_props.top +
pipe_sprite_props.height &&
bird_props.top - 10 +
bird_props.height > pipe_sprite_props.top
) {
// Change game state and end the game
// if collision occurs
game_state = 'End';
message.innerHTML = 'Press Enter To Restart';
//localstorage
localStorage.setItem("hiscore", JSON.stringify(hiscore));
hiscore.push(parseInt(score_val.innerHTML))
localStorage.setItem("hiscore", JSON.stringify(hiscore));
var latestScore = JSON.parse(localStorage.getItem("hiscore"))
high_score_val.innerHTML = `Hi-Score ${Math.max.apply(Math, latestScore)}`
button.innerHTML = 'Restart';
message.style.left = '50%';
backgroundImage.style.animationPlayState = "paused";
hitSound.play();
return;
}
else {
// Increase the score if player
// has the successfully dodged the pipe
if (
pipe_sprite_props.right < bird_props.left &&
pipe_sprite_props.right +
move_speed >= bird_props.left &&
element.increase_score == '1'
) {
score_val.innerHTML = +score_val.innerHTML + 1;
}
element.style.left =
pipe_sprite_props.left - move_speed + 'px';
}
}
});
requestAnimationFrame(move);
}
requestAnimationFrame(move);
function apply_gravity() {
if (game_state != 'Play') return;
bird_dy = bird_dy + gravity;
document.addEventListener('keydown', (e) => {
if (e.key == 'ArrowUp' || e.key == ' ') {
bird_dy = -10.5;
/* console.log(game_state)
console.log(bird_props.bottom)
console.log(background.bottom) */
}
});
//mobile
document.addEventListener('touchstart', () => {
if (game_state === 'Play') {
bird_dy = -10.5;
}
});
// Collision detection with bird and
// window bottom
if (bird_props.bottom >= background.bottom) {
game_state = 'End';
message.innerHTML = 'Please Restart the Page (Bug)';
high_score_val.innerHTML = 'High Score : '
message.style.left = '50%';
backgroundImage.style.animationPlayState = "paused"
hitSound.play();
return;
}
bird.style.top = bird_props.top + bird_dy + 'px';
bird_props = bird.getBoundingClientRect();
requestAnimationFrame(apply_gravity);
}
requestAnimationFrame(apply_gravity);
let pipe_seperation = -10;
// Constant value for the gap between two pipes
let pipe_gap = 35; //35
function create_pipe() {
if (game_state != 'Play') return;
// Create another set of pipes
// if distance between two pipe has exceeded
// a predefined value
if (pipe_seperation > 115) {
pipe_seperation = -10
// Calculate random position of pipes on y axis
let pipe_posi = Math.floor(Math.random() * 25) + 8;
let pipe_sprite_inv = document.createElement('div');
pipe_sprite_inv.className = 'pipe_sprite';
pipe_sprite_inv.style.top = pipe_posi - 78 + 'vh';
pipe_sprite_inv.style.left = '100vw';
pipe_sprite_inv.style.transform = 'scaleY(-1)';
// Append the created pipe element in DOM
document.body.appendChild(pipe_sprite_inv);
let pipe_sprite = document.createElement('div');
pipe_sprite.className = 'pipe_sprite';
pipe_sprite.style.top = pipe_posi + pipe_gap + 'vh';
pipe_sprite.style.left = '100vw';
pipe_sprite.increase_score = '1';
// Append the created pipe element in DOM
document.body.appendChild(pipe_sprite);
}
pipe_seperation++;
requestAnimationFrame(create_pipe);
}
requestAnimationFrame(create_pipe);
}
setInterval(() => {
toggleVisibility(button);
}, 1000);
function toggleVisibility(element) {
element.style.visibility = (element.style.visibility === "hidden") ? "visible" : "hidden";
}