-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflap2.html
123 lines (107 loc) · 3.43 KB
/
flap2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flappy Bird Clone</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #87CEEB;
}
canvas {
border: 2px solid black;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let bird = { x: 50, y: canvas.height / 2, size: 20, speedY: 0, speedX: 2 };
let bars = [];
let score = 0;
let isGameOver = false;
function drawBird() {
ctx.fillStyle = 'yellow';
ctx.fillRect(bird.x, bird.y, bird.size, bird.size);
}
function drawBars() {
ctx.fillStyle = 'green';
bars.forEach(bar => {
ctx.fillRect(bar.x, 0, bar.width, bar.top);
ctx.fillRect(bar.x, bar.bottom, bar.width, canvas.height - bar.bottom);
});
}
function drawScore() {
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 10, 30);
}
function update() {
if (!isGameOver) {
bird.y += bird.speedY;
bird.x += bird.speedX;
bird.speedY += 0.2; // Gravity effect
if (bird.y <= 0 || bird.y + bird.size >= canvas.height) {
gameOver();
}
bars.forEach(bar => {
if (bird.x + bird.size >= bar.x && bird.x <= bar.x + bar.width) {
if (bird.y <= bar.top || bird.y + bird.size >= bar.bottom) {
gameOver();
}
}
if (bird.x === bar.x + bar.width) {
score++;
}
});
if (bars.length === 0 || bars[bars.length - 1].x + 100 < canvas.width) {
createBar();
}
bars = bars.filter(bar => bar.x + bar.width > 0);
bars.forEach(bar => bar.x -= bird.speedX);
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBird();
drawBars();
drawScore();
} else {
ctx.fillStyle = 'black';
ctx.font = '30px Arial';
ctx.fillText('Game Over', 140, 200);
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 160, 230);
ctx.fillRect(150, 250, 100, 50);
ctx.fillStyle = 'white';
ctx.fillText('Replay', 170, 280);
}
requestAnimationFrame(update);
}
function createBar() {
const topHeight = Math.floor(Math.random() * (canvas.height - 200)) + 50;
const bar = { x: canvas.width, top: topHeight, bottom: topHeight + 100, width: 20 };
bars.push(bar);
}
function gameOver() {
isGameOver = true;
}
canvas.addEventListener('click', () => {
if (isGameOver) {
isGameOver = false;
bird.y = canvas.height / 2;
bird.speedY = -5; // Lift effect
bars = [];
score = 0;
} else {
bird.speedY = -5; // Lift effect
}
});
update();
</script>
</body>
</html>