-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
174 lines (160 loc) · 5.68 KB
/
app.js
File metadata and controls
174 lines (160 loc) · 5.68 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
var pipe1 = document.getElementById("pipe1");
var pipe2 = document.getElementById("pipe2");
var bird = document.getElementById("bird");
var movepipe = setInterval(movePipe, 5);
var movebird = setInterval(moveBird, 10);
var pipe1right = parseInt(window.getComputedStyle(pipe1).getPropertyValue("right"));
var pipe1left = parseInt(window.getComputedStyle(pipe1).getPropertyValue("left"));
var birdtop = parseInt(window.getComputedStyle(bird).getPropertyValue("top"));
var birddown = parseInt(window.getComputedStyle(bird).getPropertyValue("bottom"));
var isGameOver = 1;
var gameover = document.getElementById('gameover')
var pos = 0;
var j = 0;
var pipe1height = parseInt(window.getComputedStyle(pipe1).getPropertyValue("height"));
var pipe2height = parseInt(window.getComputedStyle(pipe2).getPropertyValue("height"));
var score = 0;
var highscore = 0;
var i = 0;
var body = document.querySelector('body');
var scene = document.getElementById('scene');
var score_list = document.getElementsByClassName('top1');
window.onload = function () {
highscore = localStorage.getItem("score");
var a = document.getElementById("hiscore");
a.innerText = highscore;
}
// Function to move pipes
function movePipe() {
if (pos == 450) {
pos = -20;
pipe1height = Math.floor((Math.random() * 450));
pipe2height = 450 - pipe1height;
pipe1.style.height = pipe1height + "px";
pipe2.style.height = pipe2height + "px";
}
else {
pos++;
pipe1.style.right = pos + 'px';
pipe2.style.right = pos + 'px';
}
//Checking if bird hits the pipe OR bird hits the bottom
var pipe1down = 600 - pipe1height;
var pipe2top = pipe2height;
var pipe1right = parseInt(window.getComputedStyle(pipe1).getPropertyValue("right"));
var pipe1left = parseInt(window.getComputedStyle(pipe1).getPropertyValue("left"));
var birdtop = parseInt(window.getComputedStyle(bird).getPropertyValue("top"));
var birddown = parseInt(window.getComputedStyle(bird).getPropertyValue("bottom"));
if (birddown == 63) {
let Gameover = new Audio("./audio/hit.wav");
Gameover.play();
clearInterval(movepipe);
clearInterval(movebird);
gameover.style.display = "block";
setTimeout(() => {
window.location.reload();
}, 2000);
if (score > highscore) {
localStorage.setItem("score", score);
}
isGameOver = 0;
}
if ((pipe1right + 55) >= 219 && (pipe1right + 60) <= 320) {
if ((birdtop < (600 - pipe1down - 5)) || (birddown < pipe2top - 20)) {
clearInterval(movepipe);
clearInterval(movebird);
gameover.style.display = "block";
setTimeout(() => {
window.location.reload();
}, 2000);
if (score > highscore) {
localStorage.setItem("score", score);
}
isGameOver = 0;
let Gameover = new Audio("./audio/hit.wav");
Gameover.play();
}
}
else if ((pipe1right + 60) == 321) {
score++;
var s = document.getElementById('score');
s.innerText = score;
let point = new Audio("./audio/point.wav");
point.play();
}
}
//Function to move birds
function moveBird() {
var currentbirdpos = parseInt(window.getComputedStyle(bird).getPropertyValue("bottom"));
bird.style.bottom = (currentbirdpos - 3) + "px";
}
//Spacebar button function
document.onkeydown = function (e) {
if ((e.keyCode == 32||e.keyCode==38) && isGameOver) {
var currentbirdpos = parseInt(window.getComputedStyle(bird).getPropertyValue("bottom"));
var temp = setInterval(jump, 1);
function jump() {
if ((pipe1right + 55) >= 219 && (pipe1right + 60) <= 320) {
if ((birdtop < (600 - pipe1down - 5)) || (birddown < pipe2top - 20)) {
clearInterval(movepipe);
clearInterval(movebird);
clearInterval(temp);
gameover.style.display = "block";
setTimeout(() => {
window.location.reload();
}, 2000);
if (score > highscore) {
localStorage.setItem("score", score);
}
isGameOver = 0;
let Gameover = new Audio("./audio/hit.wav");
Gameover.play();
}
}
j++;
bird.style.bottom = (currentbirdpos + j) + "px";
if (j > 100) {
j = 0;
clearInterval(temp);
}
}
var imgsrc = document.getElementById("bird");
imgsrc.style.backgroundImage = "url(./img/up-removebg-preview.png)";
isGameOver = 1;
}
}
document.onkeyup = function (e) {
if (e.keyCode == 32||e.keyCode==38) {
var imgsrc = document.getElementById("bird");
imgsrc.style.backgroundImage = "url(./img/noup-removebg-preview.png)";
}
}
//Toggling of dark and light mode
function togglemode() {
if (window.getComputedStyle(body).getPropertyValue('background-color') == 'rgb(255, 255, 255)') {
body.style.backgroundColor = 'rgb(37, 37, 37)';
scene.style.backgroundColor = 'white';
for (let i = 0; i < score_list.length; ++i) {
score_list[i].style.color = 'white';
}
var toggle = document.getElementById('toggle');
toggle.style.backgroundImage = "url(./img/moon.jpg)";
}
else {
body.style.backgroundColor = 'white';
scene.style.backgroundColor = 'rgb(37, 37, 37)';
for (let i = 0; i < score_list.length; ++i) {
score_list[i].style.color = 'rgb(37, 37, 37)';
var toggle = document.getElementById('toggle');
toggle.style.backgroundImage = "url(./img/sun.avif)";
}
}
}
//Function to move background base
setInterval(moveimage, 5);
function moveimage() {
if (isGameOver) {
i -= 0.5;
document.getElementById('base').style.backgroundPosition = i + "px";
}
}