-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathflappybird.js
361 lines (293 loc) · 9.37 KB
/
flappybird.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//board
let board;
let boardWidth = window.innerWidth;
let boardHeight = window.innerHeight;
let context;
//bird
let birdWidth = 34;
let birdHeight = 24;
let birdX = boardWidth / 8;
let birdY = boardHeight / 2;
let birdImgs = [];
let birdImgIndex = 0;
let bird = {
x: birdX,
y: birdY,
width: birdWidth,
height: birdHeight
};
//pipes
let pipeArray = [];
let pipeWidth = 64;
let pipeHeight = 512;
let pipeX = boardWidth;
let pipeY = 0;
let topPipeImg;
let bottomPipeImg;
//physics
let velocityX = -2;
let velocityY = 0;
let gravity = 0.4;
let gameOver = false;
let score = 0;
let wingsound = new Audio("./sfx_wing.wav");
let hitsound = new Audio("./sfx_hit.wav");
let bgm = new Audio("./Petalburg_City.mp3");
bgm.loop = true;
let userName = ""; // Store username from JWT
let secretKey = "hritikmani"; // Replace with your actual secret key
window.onload = function () {
board = document.getElementById("board");
board.height = boardHeight;
board.width = boardWidth;
context = board.getContext("2d");
// Load images
for (let i = 0; i < 4; i++) {
let birdImg = new Image();
birdImg.src = `./flappybird${i}.png`;
birdImgs.push(birdImg);
}
topPipeImg = new Image();
topPipeImg.src = "./toppipe.png";
bottomPipeImg = new Image();
bottomPipeImg.src = "./bottompipe.png";
// Retrieve username from JWT token
getUserNameFromToken();
// Display initial screen
context.fillStyle = "white";
context.font = "45px sans-serif";
context.fillText("Tap to Start", boardWidth / 3, boardHeight / 2);
// Wait for first input to start the game
document.addEventListener("keydown", startGame);
document.addEventListener("mousedown", startGame);
document.addEventListener("touchstart", startGame);
};
let gameStarted = false;
function startGame() {
if (!gameStarted) {
gameStarted = true;
requestAnimationFrame(update);
placePipesInterval = setInterval(placePipes, 1500);
animateBirdInterval = setInterval(animateBird, 100);
// Remove event listeners after starting
document.removeEventListener("keydown", startGame);
document.removeEventListener("mousedown", startGame);
document.removeEventListener("touchstart", startGame);
// Add event listeners for bird movement
document.addEventListener("keydown", moveBird);
document.addEventListener("mousedown", moveBird);
document.addEventListener("touchstart", moveBird);
}
}
function update() {
requestAnimationFrame(update);
if (gameOver) {
return;
}
context.clearRect(0, 0, board.width, board.height);
// Apply gravity
velocityY += gravity;
bird.y = Math.max(bird.y + velocityY, 0);
context.drawImage(birdImgs[birdImgIndex], bird.x, bird.y, bird.width, bird.height);
if (bird.y > board.height) {
endGame();
}
// Draw pipes
for (let i = 0; i < pipeArray.length; i++) {
let pipe = pipeArray[i];
pipe.x += velocityX;
context.drawImage(pipe.img, pipe.x, pipe.y, pipe.width, pipe.height);
if (!pipe.passed && bird.x > pipe.x + pipe.width) {
score += 0.5;
pipe.passed = true;
}
if (detectCollision(bird, pipe)) {
hitsound.play();
endGame();
}
}
// Remove old pipes
while (pipeArray.length > 0 && pipeArray[0].x < -pipeWidth) {
pipeArray.shift();
}
// Display Score
context.fillStyle = "white";
context.font = "45px sans-serif";
context.fillText(score, 5, 45);
// Display Username
const usernameDiv = document.getElementById("username");
if (userName) {
usernameDiv.textContent = `Player: ${userName}`;
}
if (gameOver) {
context.fillText("GAME OVER", 5, 90);
bgm.pause();
bgm.currentTime = 0;
}
}
function animateBird() {
birdImgIndex++;
birdImgIndex %= birdImgs.length;
}
function placePipes() {
if (gameOver) {
return;
}
let randomPipeY = pipeY - pipeHeight / 4 - Math.random() * (pipeHeight / 2);
let openingSpace = board.height / 4;
let topPipe = {
img: topPipeImg,
x: pipeX,
y: randomPipeY,
width: pipeWidth,
height: pipeHeight,
passed: false
};
pipeArray.push(topPipe);
let bottomPipe = {
img: bottomPipeImg,
x: pipeX,
y: randomPipeY + pipeHeight + openingSpace,
width: pipeWidth,
height: pipeHeight,
passed: false
};
pipeArray.push(bottomPipe);
}
function moveBird(e) {
if (
e.code == "Space" ||
e.code == "ArrowUp" ||
e.code == "KeyX" ||
e.type == "mousedown" ||
e.type == "touchstart"
) {
if (bgm.paused) {
bgm.play();
}
velocityY = -6;
wingsound.play();
if (gameOver) {
bird.y = birdY;
pipeArray = [];
score = 0;
gameOver = false;
}
}
}
function detectCollision(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
// Function to get and decode JWT token
function getUserNameFromToken() {
const token = localStorage.getItem("token");
if (!token) {
window.location.href = "entry.html"; // Redirect if token is missing
return;
}
try {
const base64Url = token.split(".")[1];
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
const jsonPayload = decodeURIComponent(
atob(base64)
.split("")
.map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2))
.join("")
);
const user = JSON.parse(jsonPayload);
userName = user.name;
// console.log("Player Name:", userName);
} catch (error) {
console.error("Error decoding token:", error);
window.location.href = "entry.html"; // Redirect on token error
}
}
async function sendScore() {
if (!userName) return;
const data = {
name: userName,
score: score,
secretKey: secretKey
};
try {
const response = await fetch("https://tedx-al.vercel.app/api/user/score", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});
if (!response.ok) throw new Error("Failed to submit score");
console.log("Redirecting...");
setTimeout(() => {
window.location.assign("aftergame.html");
}, 100);
} catch (error) {
console.error("Error submitting score:", error);
window.location.assign("aftergame.html");
}
}
// Function to handle game over
// Function to handle game over and show the modal
function endGame() {
gameOver = true;
sendScore();
// Stop background music
bgm.pause();
bgm.currentTime = 0;
// Hide the game canvas
document.getElementById("board").style.display = "none";
// Create a game over screen
let gameOverScreen = document.createElement("div");
gameOverScreen.id = "gameOverScreen";
gameOverScreen.style.position = "absolute";
gameOverScreen.style.top = "50%";
gameOverScreen.style.left = "50%";
gameOverScreen.style.transform = "translate(-50%, -50%)";
gameOverScreen.style.textAlign = "center";
gameOverScreen.style.fontSize = "50px";
gameOverScreen.style.color = "white";
gameOverScreen.style.fontFamily = "sans-serif";
// Display game over text and score
gameOverScreen.innerHTML = `<p>Game Over</p><p>Score: ${score}</p>`;
document.body.appendChild(gameOverScreen);
}
// Event listeners for modal buttons
let placePipesInterval, animateBirdInterval;
document.getElementById("playAgainBtn").addEventListener("click", function () {
// Hide the modal
const modal = document.getElementById("gameOverModal");
modal.style.display = "none";
// Reset game state
gameOver = false;
bird.y = birdY;
velocityY = 0; // Reset bird velocity
pipeArray = [];
score = 0;
// Clear old intervals before setting new ones
clearInterval(placePipesInterval);
clearInterval(animateBirdInterval);
// Restart intervals
placePipesInterval = setInterval(placePipes, 1500);
animateBirdInterval = setInterval(animateBird, 100);
// Remove previous event listeners to avoid duplicates
document.removeEventListener("keydown", moveBird);
document.removeEventListener("mousedown", moveBird);
document.removeEventListener("touchstart", moveBird);
// Re-add event listeners
document.addEventListener("keydown", moveBird);
document.addEventListener("mousedown", moveBird);
document.addEventListener("touchstart", moveBird);
// Restart the game loop
requestAnimationFrame(update);
// Play background music
if (bgm.paused) {
bgm.play();
}
});
// Play Again Button - Fully Resets the Game