-
Notifications
You must be signed in to change notification settings - Fork 0
/
flap4.html
140 lines (123 loc) · 4.01 KB
/
flap4.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
<!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;
}
.game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(255, 255, 255, 0.8);
padding: 20px;
border-radius: 10px;
text-align: center;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="game-over" style="display: none;">
<h1 style="color: white;">Game Over</h1>
<p style="color: white;">Score: <span id="finalScore">0</span></p>
<p style="color: white;">High Score: <span id="highScore">0</span></p>
<button onclick="restartGame()">Replay</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let bird = { x: 50, y: canvas.height / 2, size: 20, speedY: 0 };
let bars = [];
let score = 0;
let highScore = 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);
ctx.fillText(`High Score: ${highScore}`, 10, 60);
}
function update() {
if (!isGameOver) {
bird.y += bird.speedY;
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 (score > highScore) {
highScore = 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 -= 2); // Adjust speed here
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBird();
drawBars();
drawScore();
} else {
document.getElementById('finalScore').textContent = score;
document.getElementById('highScore').textContent = highScore;
document.querySelector('.game-over').style.display = 'block';
}
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;
}
function restartGame() {
isGameOver = false;
bird.y = canvas.height / 2;
bird.speedY = -5; // Lift effect
bars = [];
score = 0;
}
canvas.addEventListener('click', () => {
if (!isGameOver) {
bird.speedY = -5; // Lift effect
}
});
update();
</script>
</body>
</html>