-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlappy_Bird.html
More file actions
496 lines (419 loc) · 15.2 KB
/
Flappy_Bird.html
File metadata and controls
496 lines (419 loc) · 15.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flappy Bird Game</title>
<!-- Load Tailwind CSS for utility classes on the container and controls -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Custom styles for the game container and canvas */
body {
font-family: 'Inter', sans-serif;
background-color: #1f2937; /* Dark background */
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
}
#game-container {
width: 100%;
max-width: 480px; /* Standard mobile/game width */
border: 8px solid #fcd34d; /* Yellow border */
border-radius: 12px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5), inset 0 0 15px rgba(255, 255, 255, 0.2);
overflow: hidden;
display: flex;
flex-direction: column;
background-color: #374151; /* Darker internal background */
}
#gameCanvas {
display: block;
width: 100%;
/* Aspect ratio 16:9 for the canvas area */
aspect-ratio: 16 / 9;
background: linear-gradient(to bottom, #7dd3fc, #3b82f6); /* Blue sky gradient */
cursor: pointer;
}
.game-ui {
padding: 1rem;
display: flex;
justify-content: space-between;
align-items: center;
color: #f3f4f6;
font-size: 1.25rem;
font-weight: bold;
}
.control-panel {
padding: 1rem;
display: flex;
justify-content: center;
}
.game-button {
padding: 0.75rem 1.5rem;
border-radius: 8px;
font-weight: bold;
text-transform: uppercase;
cursor: pointer;
transition: all 0.2s;
box-shadow: 0 4px #c2410c;
background-color: #f97316; /* Orange button */
color: white;
border: none;
}
.game-button:hover {
background-color: #ea580c;
}
.game-button:active {
box-shadow: 0 0 #c2410c;
transform: translateY(4px);
}
#messageBox {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
color: white;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
visibility: hidden;
opacity: 0;
transition: opacity 0.3s;
border-radius: 12px;
}
#messageBox.active {
visibility: visible;
opacity: 1;
}
#messageBox h2 {
font-size: 3rem;
margin-bottom: 0.5rem;
text-shadow: 0 0 10px #fcd34d;
}
#messageBox p {
font-size: 1.5rem;
margin-bottom: 2rem;
}
#highScoreText {
color: #fcd34d; /* Yellow for high score */
}
</style>
</head>
<body>
<div id="game-container" class="relative">
<div class="game-ui">
<div id="scoreDisplay">Score: 0</div>
<div id="highScoreText">High Score: 0</div>
</div>
<canvas id="gameCanvas"></canvas>
<div id="messageBox">
<h2 id="messageTitle">Flappy Bird Clone</h2>
<p id="messageBody">Click or press Space to jump!</p>
<button id="startButton" class="game-button">Start Game</button>
</div>
<div class="control-panel">
<button id="restartButton" class="game-button hidden">Restart Game</button>
</div>
</div>
<script>
// --- CANVAS AND CONTEXT SETUP ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gameContainer = document.getElementById('game-container');
const scoreDisplay = document.getElementById('scoreDisplay');
const highScoreText = document.getElementById('highScoreText');
const messageBox = document.getElementById('messageBox');
const messageTitle = document.getElementById('messageTitle');
const messageBody = document.getElementById('messageBody');
const startButton = document.getElementById('startButton');
const restartButton = document.getElementById('restartButton');
// --- GAME CONSTANTS AND STATE ---
let game;
let highscore = parseInt(localStorage.getItem('flappyHighScore')) || 0;
// Function to handle canvas resize and keep it responsive
function resizeCanvas() {
// Set canvas dimensions based on the container width and aspect ratio (16:9)
const containerWidth = gameContainer.clientWidth;
canvas.width = containerWidth;
canvas.height = containerWidth * (9 / 16);
// Redraw content if necessary (for persistent elements, but here only for the start message)
if (game && !game.isRunning) {
drawStartScreen();
}
}
// Initial resize and listener
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// --- GAME CLASSES ---
class Bird {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.vy = 0; // Vertical velocity
this.gravity = 0.5;
this.jumpStrength = -9;
this.color = '#fcd34d'; // Yellow
}
update() {
// Apply gravity
this.vy += this.gravity;
this.y += this.vy;
// Simple velocity damping for smooth flight
this.vy *= 0.98;
// Keep bird within bounds (though hitting the ground is a game over)
if (this.y + this.radius > canvas.height) {
this.y = canvas.height - this.radius;
this.vy = 0;
}
if (this.y - this.radius < 0) {
this.y = this.radius;
this.vy = 0;
}
}
jump() {
this.vy = this.jumpStrength;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.strokeStyle = '#92400e';
ctx.lineWidth = 2;
ctx.stroke();
// Draw a simple eye
ctx.beginPath();
ctx.arc(this.x + this.radius / 3, this.y - this.radius / 3, this.radius / 6, 0, Math.PI * 2);
ctx.fillStyle = 'black';
ctx.fill();
}
}
class Pipe {
constructor(x, width, gapHeight, gapY) {
this.x = x;
this.width = width;
this.gapHeight = gapHeight;
this.gapY = gapY; // Y-coordinate of the gap center
this.vx = -3; // Pipe horizontal speed
this.scored = false;
this.color = '#10b981'; // Green pipe
}
update() {
this.x += this.vx;
}
draw() {
ctx.fillStyle = this.color;
// Draw top pipe
ctx.fillRect(this.x, 0, this.width, this.gapY - this.gapHeight / 2);
// Draw bottom pipe
ctx.fillRect(this.x, this.gapY + this.gapHeight / 2, this.width, canvas.height - (this.gapY + this.gapHeight / 2));
// Add a darker border for depth
ctx.strokeStyle = '#047857';
ctx.lineWidth = 3;
ctx.strokeRect(this.x, 0, this.width, this.gapY - this.gapHeight / 2);
ctx.strokeRect(this.x, this.gapY + this.gapHeight / 2, this.width, canvas.height - (this.gapY + this.gapHeight / 2));
}
}
class FlappyGame {
constructor() {
this.bird = new Bird(canvas.width / 4, canvas.height / 2, 20);
this.pipes = [];
this.score = 0;
this.isRunning = false;
this.pipeGap = 160;
this.pipeWidth = 50;
this.pipeSpawnInterval = 1500; // ms
this.lastPipeTime = 0;
}
reset() {
this.bird = new Bird(canvas.width / 4, canvas.height / 2, 20);
this.pipes = [];
this.score = 0;
this.isRunning = true;
this.lastPipeTime = performance.now();
scoreDisplay.textContent = `Score: ${this.score}`;
highScoreText.textContent = `High Score: ${highscore}`;
messageBox.classList.remove('active');
restartButton.classList.add('hidden');
}
spawnPipe() {
// Random Y position for the gap center, ensuring it's not too close to top/bottom
const minGapY = canvas.height * 0.25;
const maxGapY = canvas.height * 0.75;
const gapY = minGapY + (Math.random() * (maxGapY - minGapY));
this.pipes.push(new Pipe(canvas.width, this.pipeWidth, this.pipeGap, gapY));
}
checkCollision() {
const bird = this.bird;
// 1. Check ground and ceiling collision
if (bird.y + bird.radius >= canvas.height || bird.y - bird.radius <= 0) {
return true;
}
// 2. Check pipe collisions
for (const pipe of this.pipes) {
// Check if the bird is horizontally aligned with the pipe
if (bird.x + bird.radius > pipe.x && bird.x - bird.radius < pipe.x + pipe.width) {
// Top pipe collision
const topPipeBottom = pipe.gapY - pipe.gapHeight / 2;
if (bird.y - bird.radius < topPipeBottom) {
return true;
}
// Bottom pipe collision
const bottomPipeTop = pipe.gapY + pipe.gapHeight / 2;
if (bird.y + bird.radius > bottomPipeTop) {
return true;
}
}
}
return false;
}
update(deltaTime) {
if (!this.isRunning) return;
this.bird.update();
// Handle pipe spawning
if (deltaTime - this.lastPipeTime > this.pipeSpawnInterval) {
this.spawnPipe();
this.lastPipeTime = deltaTime;
}
// Update and clean up pipes
this.pipes = this.pipes.filter(pipe => {
pipe.update();
// Score check
if (!pipe.scored && pipe.x + pipe.width < this.bird.x - this.bird.radius) {
this.score++;
pipe.scored = true;
scoreDisplay.textContent = `Score: ${this.score}`;
}
// Keep pipes that are still visible
return pipe.x + pipe.width > 0;
});
// Check for game over
if (this.checkCollision()) {
this.endGame();
}
}
draw() {
// Clear canvas for the next frame
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw ground (a simple green strip at the bottom)
ctx.fillStyle = '#4ade80';
ctx.fillRect(0, canvas.height - 30, canvas.width, 30);
// Draw ground border
ctx.strokeStyle = '#16a34a';
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(0, canvas.height - 30);
ctx.lineTo(canvas.width, canvas.height - 30);
ctx.stroke();
this.pipes.forEach(pipe => pipe.draw());
this.bird.draw();
}
endGame() {
this.isRunning = false;
// Update High Score
if (this.score > highscore) {
highscore = this.score;
localStorage.setItem('flappyHighScore', highscore);
}
// Display Game Over message
messageTitle.textContent = "Game Over!";
messageBody.innerHTML = `Your Score: ${this.score}<br>High Score: ${highscore}`;
startButton.classList.add('hidden');
restartButton.classList.remove('hidden');
messageBox.classList.add('active');
// Update UI
scoreDisplay.textContent = `Score: ${this.score}`;
highScoreText.textContent = `High Score: ${highscore}`;
}
}
// --- GAME LOOP AND INITIALIZATION ---
let lastTime = 0;
function gameLoop(time) {
if (!game || !game.isRunning) {
// If the game is not running, stop the loop and wait for start
drawStartScreen();
return;
}
const deltaTime = time - lastTime;
lastTime = time;
game.update(time);
game.draw();
// Use requestAnimationFrame to loop
requestAnimationFrame(gameLoop);
}
function drawStartScreen() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw ground
ctx.fillStyle = '#4ade80';
ctx.fillRect(0, canvas.height - 30, canvas.width, 30);
ctx.strokeStyle = '#16a34a';
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(0, canvas.height - 30);
ctx.lineTo(canvas.width, canvas.height - 30);
ctx.stroke();
// Draw a static bird in the middle
const tempBird = new Bird(canvas.width / 4, canvas.height / 2, 20);
tempBird.draw();
// Update high score display
highScoreText.textContent = `High Score: ${highscore}`;
}
// --- EVENT LISTENERS ---
// Jump handler
function handleJump() {
if (game && game.isRunning) {
game.bird.jump();
}
}
// Click/Touch input
canvas.addEventListener('click', handleJump);
canvas.addEventListener('touchstart', (e) => {
e.preventDefault(); // Prevent scrolling on touch
handleJump();
});
// Keyboard input (Spacebar)
document.addEventListener('keydown', (e) => {
if (e.code === 'Space' || e.key === ' ') {
e.preventDefault();
handleJump();
}
// Handle restart/start on spacebar
if (!game || !game.isRunning) {
if (e.code === 'Space' || e.key === ' ') {
startGame();
}
}
});
// Start/Restart button handlers
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
function startGame() {
if (!game) {
game = new FlappyGame();
}
game.reset();
lastTime = performance.now();
requestAnimationFrame(gameLoop);
}
// Initialize the game state when the page loads
window.onload = () => {
resizeCanvas();
game = new FlappyGame();
drawStartScreen();
// Show the initial start message
messageBox.classList.add('active');
startButton.classList.remove('hidden');
restartButton.classList.add('hidden');
};
</script>
</body>
</html>