-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy.html
412 lines (368 loc) Β· 13.7 KB
/
flappy.html
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flappy Bird with Power-ups and Custom Music</title>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(to bottom, #87CEEB, #E0F6FF);
font-family: Arial, sans-serif;
}
.game-container {
position: relative;
}
canvas {
border: 2px solid #2c3e50;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
#restartButton, #startButton {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
z-index: 1000;
}
#restartButton:hover, #startButton:hover {
background-color: #2980b9;
}
#scoreDisplay, #highScoreDisplay {
position: absolute;
font-size: 24px;
color: #2c3e50;
}
#scoreDisplay {
top: 10px;
left: 10px;
}
#highScoreDisplay {
top: 10px;
right: 10px;
}
#powerUpDisplay {
position: absolute;
bottom: 10px;
left: 10px;
font-size: 18px;
color: #2c3e50;
}
#musicToggle {
position: absolute;
bottom: 10px;
right: 10px;
font-size: 18px;
color: #2c3e50;
cursor: pointer;
}
#musicFileInput {
margin-top: 10px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="game-container">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<button id="startButton">Start Game</button>
<button id="restartButton" style="display:none;">Restart</button>
<div id="scoreDisplay">Score: 0</div>
<div id="highScoreDisplay">High Score: 0</div>
<div id="powerUpDisplay"></div>
<div id="musicToggle">π</div>
</div>
<input type="file" id="musicFileInput" accept="audio/*">
<audio id="backgroundMusic" loop>
Your browser does not support the audio element.
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const restartButton = document.getElementById('restartButton');
const startButton = document.getElementById('startButton');
const scoreDisplay = document.getElementById('scoreDisplay');
const highScoreDisplay = document.getElementById('highScoreDisplay');
const powerUpDisplay = document.getElementById('powerUpDisplay');
const musicToggle = document.getElementById('musicToggle');
const backgroundMusic = document.getElementById('backgroundMusic');
const musicFileInput = document.getElementById('musicFileInput');
let birdY = canvas.height / 2;
let birdVelocity = 0;
let birdGravity = 0.25; // Reduced gravity
let birdLift = -5; // Adjusted lift
let pipes = [];
let pipeWidth = 50;
let pipeGap = 170;
let score = 0;
let highScore = 0;
let gameActive = false;
let powerUpActive = false;
let powerUpTimer = 0;
let currentPowerUp = null;
let scoreMultiplier = 1;
let gameSpeed = 1;
let isMusicPlaying = false;
let musicPlayPromise = null;
let customMusicURL = null;
const POWER_UPS = {
INVINCIBILITY: { color: '#FF69B4', duration: 300, name: 'Invincibility' },
SCORE_MULTIPLIER: { color: '#FFD700', duration: 300, name: 'Score x2' },
SLOW_MOTION: { color: '#4169E1', duration: 300, name: 'Slow Motion' }
};
function drawBird(x, y) {
ctx.fillStyle = powerUpActive && currentPowerUp ? currentPowerUp.color : '#F4D03F';
ctx.beginPath();
ctx.arc(x, y, 15, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(x + 5, y - 5, 5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(x + 6, y - 5, 2, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#E67E22';
ctx.beginPath();
ctx.moveTo(x + 15, y);
ctx.lineTo(x + 25, y);
ctx.lineTo(x + 15, y + 5);
ctx.closePath();
ctx.fill();
if (powerUpActive && currentPowerUp) {
ctx.strokeStyle = currentPowerUp.color;
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(x, y, 20, 0, Math.PI * 2);
ctx.stroke();
}
}
function drawPipe(x, topHeight) {
ctx.fillStyle = '#2ECC71';
ctx.fillRect(x, 0, pipeWidth, topHeight);
ctx.fillRect(x, topHeight + pipeGap, pipeWidth, canvas.height);
}
function drawPowerUp(x, y, powerUp) {
if (powerUp && powerUp.color) {
ctx.fillStyle = powerUp.color;
ctx.beginPath();
ctx.arc(x, y, 10, 0, Math.PI * 2);
ctx.fill();
}
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (gameActive) {
birdVelocity += birdGravity;
birdY += birdVelocity;
if (birdVelocity > 7) { // Reduced max velocity
birdVelocity = 7;
}
drawBird(75, birdY);
if (birdY + 15 > canvas.height) { // 15 is the radius of the bird
gameOver();
return;
}
if (powerUpActive && currentPowerUp) {
powerUpTimer--;
if (powerUpTimer <= 0) {
deactivatePowerUp();
}
updatePowerUpDisplay();
}
pipes.forEach((pipe, index) => {
pipe.x -= 1.5 * gameSpeed;
drawPipe(pipe.x, pipe.topHeight);
if (pipe.x < -pipeWidth) {
pipes.splice(index, 1);
score += scoreMultiplier;
scoreDisplay.textContent = `Score: ${score}`;
}
if (pipe.powerUp && !pipe.powerUpCollected) {
drawPowerUp(pipe.x + pipeWidth / 2, pipe.topHeight + pipeGap / 2, pipe.powerUp);
if (Math.abs(75 - pipe.x - pipeWidth / 2) < 20 && Math.abs(birdY - (pipe.topHeight + pipeGap / 2)) < 20) {
pipe.powerUpCollected = true;
activatePowerUp(pipe.powerUp);
}
}
if ((!powerUpActive || (currentPowerUp && currentPowerUp !== POWER_UPS.INVINCIBILITY)) &&
(pipe.x < 90 && pipe.x + pipeWidth > 60 &&
(birdY < pipe.topHeight + 15 || birdY > pipe.topHeight + pipeGap - 15))) {
gameOver();
return;
}
});
if (pipes.length === 0 || pipes[pipes.length - 1].x < canvas.width - 250) {
let topHeight = Math.random() * (canvas.height - pipeGap - 100) + 50;
let newPipe = {
x: canvas.width,
topHeight: topHeight,
powerUp: Math.random() < 0.2 ? getRandomPowerUp() : null,
powerUpCollected: false
};
pipes.push(newPipe);
}
// Check if the bird touches the top or bottom of the screen
if (birdY > canvas.height) {
gameOver();
return;
}
} else {
// Draw the bird even when the game is not active
drawBird(75, birdY);
}
requestAnimationFrame(gameLoop);
}
function getRandomPowerUp() {
const powerUps = Object.values(POWER_UPS);
return powerUps[Math.floor(Math.random() * powerUps.length)];
}
function activatePowerUp(powerUp) {
if (!powerUp) return;
deactivatePowerUp();
currentPowerUp = powerUp;
powerUpActive = true;
powerUpTimer = powerUp.duration;
if (powerUp === POWER_UPS.SCORE_MULTIPLIER) {
scoreMultiplier = 2;
} else if (powerUp === POWER_UPS.SLOW_MOTION) {
gameSpeed = 0.5;
}
updatePowerUpDisplay();
}
function deactivatePowerUp() {
powerUpActive = false;
currentPowerUp = null;
scoreMultiplier = 1;
gameSpeed = 1;
updatePowerUpDisplay();
}
function updatePowerUpDisplay() {
if (powerUpActive && currentPowerUp && currentPowerUp.name) {
powerUpDisplay.textContent = `${currentPowerUp.name}: ${Math.ceil(powerUpTimer / 60)}s`;
} else {
powerUpDisplay.textContent = '';
}
}
function gameOver() {
gameActive = false;
restartButton.style.display = 'block';
if (score > highScore) {
highScore = score;
highScoreDisplay.textContent = `High Score: ${highScore}`;
}
deactivatePowerUp();
// Draw "Game Over" text
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.font = '48px Arial';
ctx.textAlign = 'center';
ctx.fillText('Game Over', canvas.width / 2, canvas.height / 2 - 50);
ctx.font = '24px Arial';
ctx.fillText(`Score: ${score}`, canvas.width / 2, canvas.height / 2 + 50);
}
function startGame() {
birdY = canvas.height / 2;
birdVelocity = 0;
pipes = [];
score = 0;
gameActive = true;
powerUpActive = false;
powerUpTimer = 0;
currentPowerUp = null;
scoreMultiplier = 1;
gameSpeed = 1;
startButton.style.display = 'none';
restartButton.style.display = 'none';
scoreDisplay.textContent = 'Score: 0';
updatePowerUpDisplay();
if (customMusicURL) {
playMusic();
}
}
function playMusic() {
if (customMusicURL) {
backgroundMusic.src = customMusicURL;
musicPlayPromise = backgroundMusic.play();
if (musicPlayPromise !== undefined) {
musicPlayPromise.then(_ => {
isMusicPlaying = true;
musicToggle.textContent = 'π';
}).catch(error => {
console.log('Audio play failed:', error);
isMusicPlaying = false;
musicToggle.textContent = 'π';
});
}
}
}
function stopMusic() {
if (backgroundMusic.src) {
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
isMusicPlaying = false;
musicToggle.textContent = 'π';
}
}
function toggleMusic() {
if (customMusicURL) {
if (isMusicPlaying) {
stopMusic();
} else {
playMusic();
}
}
}
function handleCustomMusicUpload(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
customMusicURL = e.target.result;
backgroundMusic.src = customMusicURL;
musicToggle.textContent = 'π';
isMusicPlaying = false;
};
reader.readAsDataURL(file);
}
}
gameLoop();
document.addEventListener('keydown', function(e) {
if (e.code === 'Space') {
e.preventDefault(); // Prevent spacebar from scrolling the page
if (gameActive) {
birdVelocity = birdLift;
} else {
startGame();
}
}
});
canvas.addEventListener('click', function() {
if (gameActive) {
birdVelocity = birdLift;
} else {
startGame();
}
});
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
musicToggle.addEventListener('click', toggleMusic);
musicFileInput.addEventListener('change', handleCustomMusicUpload);
</script>
</body>
</html>