-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
231 lines (215 loc) · 5.63 KB
/
index.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
$("#level").fadeOut(1);
var birdPosition = 300;
var birdVelocity = 0;
var gravity = 0.3;
var groundLevel = 560;
var goingDown = false;
var obstaclePosition = 600;
var obs2 = obstaclePosition + 210;
var obs3 = obs2 + 210;
var obs4 = obs3 + 210;
var obstacleMoving = false;
var obstacle12Vanished = false;
var obstacle34Vanished = false;
var obstacle56Vanished = false;
var obstacle78Vanished = false;
var obstacleSpeed = 1;
var level = 1;
var touched = false;
function changePosition() {
$("#bird").css({ top: birdPosition + "px" });
birdPosition += birdVelocity;
birdVelocity += gravity;
if (birdPosition >= groundLevel) {
clearInterval(intervalId);
birdPosition = groundLevel;
birdVelocity = 0;
goingDown = false;
gameOver();
}
}
function jump() {
birdVelocity = -5; // Jumping velocity, adjust as needed
}
function startGame() {
$("h1").fadeOut(200);
$("#level").fadeIn(500);
$("#level").text("level: " + level);
goingDown = true;
$("#bird").css("background-color", "white");
jump();
// Start the game by calling changePosition continuously
intervalId = setInterval(changePosition, 10);
}
// button click and keyboard trigger partw
$(document).keydown(function (event) {
if (!goingDown) {
if (
event.key === " " ||
event.key === "w" ||
event.key === "W" ||
event.key === "ArrowUp"
) {
if(touched){
location.reload();
}
startGame();
if (!obstacleMoving) {
intervalObsId = setInterval(animateObstacle, 11);
}
obstacleMoving = true;
}
} else if (
(event.key === " " ||
event.key === "w" ||
event.key === "W" ||
event.key === "ArrowUp") &&
birdPosition > 50
) {
if(touched){
location.reload();
}
jump();
}
});
$(document).click(function (event) {
if (!goingDown) {
if (event) {
startGame();
if(touched){
location.reload();
}
if (!obstacleMoving) {
intervalObsId = setInterval(animateObstacle, 11);
getBoundingClientRect();
}
obstacleMoving = true;
}
} else if (event && birdPosition > 50) {
if(touched){
location.reload();
}
jump();
}
});
function animateObstacle() {
obstaclePosition -= obstacleSpeed;
obs2 -= obstacleSpeed;
obs3 -= obstacleSpeed;
obs4 -= obstacleSpeed;
var obstactlePos = obstaclePosition + "px";
var obs21 = obs2 + "px";
var obs31 = obs3 + "px";
var obs41 = obs4 + "px";
document.documentElement.style.setProperty("--left1", obstactlePos);
document.documentElement.style.setProperty("--left2", obs21);
document.documentElement.style.setProperty("--left3", obs31);
document.documentElement.style.setProperty("--left4", obs41);
vanishObstacle();
if (!touched) {
checkTouch();
}
}
function vanishObstacle() {
if (obstaclePosition === 10) {
$("#obstacle1").fadeOut(500);
$("#obstacle2").fadeOut(500);
obstacle12Vanished = true;
}
if (obs2 === 10) {
$("#obstacle3").fadeOut(500);
$("#obstacle4").fadeOut(500);
obstacle34Vanished = true;
}
if (obs3 === 10) {
$("#obstacle5").fadeOut(500);
$("#obstacle6").fadeOut(500);
obstacle56Vanished = true;
}
if (obs4 === 10) {
$("#obstacle7").fadeOut(500);
$("#obstacle8").fadeOut(500);
obstacle78Vanished = true;
}
if (obstacle78Vanished === true) {
level++;
$("#level").text("level: " + level);
}
appearObstacle();
}
function appearObstacle() {
if (obstacle12Vanished) {
obstaclePosition = 1260;
$("#obstacle1").fadeIn(500);
$("#obstacle2").fadeIn(500);
obstacle12Vanished = false;
}
if (obstacle34Vanished) {
obs2 = 1260;
$("#obstacle3").fadeIn(500);
$("#obstacle4").fadeIn(500);
obstacle34Vanished = false;
}
if (obstacle56Vanished) {
obs3 = 1260;
$("#obstacle5").fadeIn(500);
$("#obstacle6").fadeIn(500);
obstacle56Vanished = false;
}
if (obstacle78Vanished) {
obs4 = 1260;
$("#obstacle7").fadeIn(500);
$("#obstacle8").fadeIn(500);
obstacle78Vanished = false;
}
}
function collision($div1, $div2) {
var x1 = $div1.offset().left;
var y1 = $div1.offset().top;
var h1 = $div1.outerHeight(true);
var w1 = $div1.outerHeight(true);
var b1 = y1 + h1;
var r1 = x1 + w1;
var x2 = $div2.offset().left;
var y2 = $div2.offset().top;
var h2 = $div2.outerHeight(true);
var w2 = $div2.outerWidth(true);
var b2 = y2 + h2;
var r2 = x2 + w2;
if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
return true;
}
function checkTouch() {
if (collision($("#bird"), $("#obstacle1")) === true) {
gameOver();
}
if (collision($("#bird"), $("#obstacle2")) === true) {
gameOver();
}
if (collision($("#bird"), $("#obstacle3")) === true) {
gameOver();
}
if (collision($("#bird"), $("#obstacle4")) === true) {
gameOver();
}
if (collision($("#bird"), $("#obstacle5")) === true) {
gameOver();
}
if (collision($("#bird"), $("#obstacle6")) === true) {
gameOver();
}
if (collision($("#bird"), $("#obstacle7")) === true) {
gameOver();
}
if (collision($("#bird"), $("#obstacle8")) === true) {
gameOver();
}
}
function gameOver() {
touched = true;
$("#level").fadeOut(1);
$("#display").fadeIn(10);
$("#display").text("ohhhhh no! click or space to relode");
document.querySelector("link[href='style.css']").href = "gameOver.css";
$("#bird").css("background-color", "rgb(192, 115, 115)")
}