-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
170 lines (144 loc) · 4.5 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
// Background scrolling speed
let move_speed = 15;
// Gravity constant value
let gravity = 0.5;
// Getting reference to the bird element
let bird = document.querySelector('.bird');
// Getting bird element properties
let bird_props = bird.getBoundingClientRect();
//Assigining intial bird element properties here which will be used to reset vaule incase of a game restart
let inital_bird_props = bird_props;
let background =
document.querySelector('.background')
.getBoundingClientRect();
// Getting reference to the score element
let score_val =
document.querySelector('.score_val');
let message =
document.querySelector('.message');
let score_title =
document.querySelector('.score_title');
// Setting initial game state to start
let game_state = 'Start';
// Add an eventlistener for key presses
document.addEventListener('keydown', (e) => {
// Start the game if enter key is pressed
if (e.key == 'Enter' &&
game_state != 'Play') {
document.querySelectorAll('.pipe_sprite')
.forEach((e) => {
e.remove();
});
bird_props = inital_bird_props;
bird.style.top = '40vh';
game_state = 'Play';
message.innerHTML = '';
score_title.innerHTML = 'Score : ';
score_val.innerHTML = '0';
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 < 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
) {
// Change game state and end the game
// if collision occurs
game_state = 'End';
message.innerHTML = 'Press Enter To Restart';
message.style.left = '28vw';
return;
} else {
// Increase the score if player
// has the successfully dodged the
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);
let bird_dy = 0;
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 = -7.6;
}
});
// Collision detection with bird and
// window top and bottom
if (bird_props.top <= 0 ||
bird_props.bottom >= background.bottom) {
message.innerHTML = 'Press Enter To Restart';
game_state = 'End';
message.style.left = '28vw';
return;
}
bird.style.top = bird_props.top + bird_dy + 'px';
bird_props = bird.getBoundingClientRect();
requestAnimationFrame(apply_gravity);
}
requestAnimationFrame(apply_gravity);
let pipe_seperation = 0;
// Constant value for the gap between two pipes
let pipe_gap = 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 = 0
// Calculate random position of pipes on y axis
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';
// 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);
}