-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (110 loc) · 4.77 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
let move_speed = 3, grativy = 0.3;
let bird = document.querySelector('.bird');
let img = document.getElementById('bird-1');
let sound_point = new Audio('sounds effect/point.mp3');
let sound_die = new Audio('sounds effect/die.mp3');
// getting bird element properties
let bird_props = bird.getBoundingClientRect();
// This method returns DOMReact -> top, right, bottom, left, x, y, width and height
let background = document.querySelector('.background').getBoundingClientRect();
let score_val = document.querySelector('.score_val');
let message = document.querySelector('.message');
let score_title = document.querySelector('.score_title');
let game_state = 'Start';
img.style.display = 'none';
message.classList.add('messageStyle');
document.addEventListener('keydown', (e) => {
if(e.key == 'Enter' && game_state != 'Play'){
document.querySelectorAll('.pipe_sprite').forEach((e) => {
e.remove();
});
img.style.display = 'block';
bird.style.top = '40vh';
game_state = 'Play';
message.innerHTML = '';
score_title.innerHTML = 'Score : ';
score_val.innerHTML = '0';
message.classList.remove('messageStyle');
play();
}
});
function play(){
function move(){
if(game_state != 'Play') return;
let pipe_sprite = document.querySelectorAll('.pipe_sprite');
pipe_sprite.forEach((element) => {
let pipe_sprite_props = element.getBoundingClientRect();
bird_props = bird.getBoundingClientRect();
if(pipe_sprite_props.right <= 0){
element.remove();
}else{
if(bird_props.left < pipe_sprite_props.left + pipe_sprite_props.width && bird_props.left + bird_props.width > pipe_sprite_props.left && bird_props.top < pipe_sprite_props.top + pipe_sprite_props.height && bird_props.top + bird_props.height > pipe_sprite_props.top){
game_state = 'End';
message.innerHTML = 'Game Over'.fontcolor('red') + '<br>Press Enter To Restart';
message.classList.add('messageStyle');
img.style.display = 'none';
sound_die.play();
return;
}else{
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;
sound_point.play();
}
element.style.left = pipe_sprite_props.left - move_speed + 'px';
}
}
});
requestAnimationFrame(move);
}
requestAnimationFrame(move);
let bird_dy = 0;
function apply_gravity(){
if(game_state != 'Play') return;
bird_dy = bird_dy + grativy;
document.addEventListener('keydown', (e) => {
if(e.key == 'ArrowUp' || e.key == ' '){
img.src = 'images/Bird-2.png';
bird_dy = -7.6;
}
});
document.addEventListener('keyup', (e) => {
if(e.key == 'ArrowUp' || e.key == ' '){
img.src = 'images/Bird.png';
}
});
if(bird_props.top <= 0 || bird_props.bottom >= background.bottom){
game_state = 'End';
message.style.left = '28vw';
window.location.reload();
message.classList.remove('messageStyle');
return;
}
bird.style.top = bird_props.top + bird_dy + 'px';
bird_props = bird.getBoundingClientRect();
requestAnimationFrame(apply_gravity);
}
requestAnimationFrame(apply_gravity);
let pipe_seperation = 0;
let pipe_gap = 35;
function create_pipe(){
if(game_state != 'Play') return;
if(pipe_seperation > 115){
pipe_seperation = 0;
let pipe_posi = Math.floor(Math.random() * 43) + 8;
let pipe_sprite_inv = document.createElement('div');
pipe_sprite_inv.className = 'pipe_sprite';
pipe_sprite_inv.style.top = pipe_posi - 70 + 'vh';
pipe_sprite_inv.style.left = '100vw';
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';
document.body.appendChild(pipe_sprite);
}
pipe_seperation++;
requestAnimationFrame(create_pipe);
}
requestAnimationFrame(create_pipe);
}