-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
216 lines (181 loc) Β· 6.2 KB
/
app.js
File metadata and controls
216 lines (181 loc) Β· 6.2 KB
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
const score = document.querySelector('.score');
const gameMessage = document.querySelector('.gameMessage');
const startScreen = document.querySelector('.startScreen');
const gameArea = document.querySelector('.gameArea');
//setting up audio
let die = new Audio('./audio/sfx_die.wav');
let hit = new Audio('./audio/sfx_hit.wav');
let point = new Audio('./audio/sfx_point.wav');
let wingMove = new Audio('./audio/sfx_wing.wav');
//Game trigger when user clicks this
gameMessage.addEventListener('click', start);
startScreen.addEventListener('click', start);
//Objects
let keys = {};
let player = {};
//Array which toggle the background images as per the score..
let backgroundImg = ["url(./background_1.png)", "url(./background_2.png)", "url(./background_3.jpg)", "url(./background_4.jpg)"];
//Setting up event listeners to track keyBoard events
document.addEventListener('keydown', pressOn);
document.addEventListener('keyup', pressOff);
//KeyBoard events
function pressOn(e) {
e.preventDefault();
//console.log(e.code);
keys[e.code] = true;
}
function pressOff(e) {
e.preventDefault();
//console.log(e.code);
keys[e.code] = false;
}
//Function which starts the gamePlay..
function start() {
player.inplay = true;
score.classList.remove('hide');
startScreen.classList.add('hide');
gameMessage.classList.add('hide');
gameMessage.innerHTML = "";
player.touched = false;
player.speed = 3;
player.score = 0;
gameArea.innerHTML = "";
//default background
gameArea.style.background = backgroundImg[0];
gameArea.style.transition = "1s ease";
gameArea.style.backgroundSize = '100% 100%';
//Creating bird element
let bird = document.createElement('div');
bird.setAttribute('class', 'bird');
let wing = document.createElement('span')
wing.pos = 15;
wing.style.top = wing.pos + "px";
wing.setAttribute('class', 'wing');
bird.appendChild(wing)
gameArea.appendChild(bird);
player.x = bird.offsetLeft;
player.y = bird.offsetTop;
player.pipe = 0;
let spacing = 300;
let howMany = Math.floor((gameArea.offsetWidth) / spacing);
//console.log(howMany);
for (let x = 0; x < howMany; x++) {
buildPipes(player.pipe * spacing);
}
window.requestAnimationFrame(playGame);
}
function buildPipes(startPos) {
let totalHeight = gameArea.offsetHeight;
let totalWidth = gameArea.offsetWidth;
player.pipe++;
let pipe1 = document.createElement("div");
pipe1.start = startPos + totalWidth;
pipe1.classList.add("pipe");
pipe1.height = Math.floor(Math.random() * 350);
pipe1.style.height = pipe1.height + "px";
pipe1.style.left = pipe1.start + "px";
pipe1.style.top = "-10px";
pipe1.x = pipe1.start;
pipe1.id = player.pipe;
gameArea.appendChild(pipe1);
let pipeSpace = Math.floor(Math.random() * 250) + 150;
let pipe2 = document.createElement("div");
pipe2.start = pipe1.start;
pipe2.classList.add("pipe");
pipe2.style.height = totalHeight - pipe1.height - pipeSpace + "px";
pipe2.style.left = pipe1.start + "px";
pipe2.style.bottom = "0px";
pipe2.x = pipe1.start;
pipe2.id = player.pipe;
gameArea.appendChild(pipe2);
}
//function which moves pipes
function movePipes(bird) {
let lines = document.querySelectorAll(".pipe");
let counter = 0; //counts pips to remove
lines.forEach(function (item) {
item.x -= player.speed;
item.style.left = item.x + "px";
if (item.x < 0) {
item.parentElement.removeChild(item);
counter++;
}
if (isCollide(item, bird)) {
player.touched = true;
hit.play();
endGame(bird);
}
})
counter = counter / 2;
for (let x = 0; x < counter; x++) {
buildPipes(0);
}
}
//function which detects collision
function isCollide(a, b) {
let aRect = a.getBoundingClientRect();
let bRect = b.getBoundingClientRect();
return !(
(aRect.bottom < bRect.top) || (aRect.top > bRect.bottom) || (aRect.right < bRect.left) || (aRect.left > bRect.right))
}
function playGame() {
if (player.inplay) {
let bird = document.querySelector(".bird");
let wing = document.querySelector(".wing");
let move = false;
movePipes(bird);
if (keys.ArrowLeft && player.x > 0) {
player.x -= player.speed;
wingMove.play();
move = true;
}
if (keys.ArrowRight && player.x < (gameArea.offsetWidth - 100)) {
player.x += player.speed;
wingMove.play();
move = true;
}
if ((keys.ArrowUp || keys.Space) && player.y > 10) {
player.y -= player.speed * 2
wingMove.play();
move = true;
}
if (keys.ArrowDown && player.y < (gameArea.offsetHeight - 50)) {
player.y += player.speed;
wingMove.play();
move = true;
}
if (move) {
wing.pos = (wing.pos == 15) ? 20 : 15;
wing.style.top = wing.pos + 'px';
}
player.y += player.speed;
//Checking game over condition
if (player.y > (gameArea.offsetHeight - 50)) {
player.touched = true;
die.play();
endGame(bird);
}
bird.style.top = player.y + "px";
bird.style.left = player.x + "px";
window.requestAnimationFrame(playGame);
player.score++;
score.innerText = "Score : " + player.score;
}
}
//changes background for everny 5 seconds
setInterval(() => {
gameArea.style.background = backgroundImg[Math.floor(Math.random() * 4)];
gameArea.style.transition = "1s ease";
gameArea.style.backgroundSize = '100% 100%';
point.play();
}, 10000);
//EndGame function for termanation of the game
function endGame(bird) {
if (player.touched) {
player.inplay = false;
gameMessage.classList.remove('hide');
bird.setAttribute('style', "transform:rotate(180deg");
score.classList.add('hide');
gameMessage.insertAdjacentHTML('beforeend', `<p style="color:red;letter-spacing:3px;font-family:fantasy;margin-bottom:10px;">GAME OVER!!!</p><br>YOUR SCORE = ${player.score}<br><br>play again`);
}
}