-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappybird.js
More file actions
191 lines (174 loc) · 5.47 KB
/
flappybird.js
File metadata and controls
191 lines (174 loc) · 5.47 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
let startButton;
let countdown = 3;
let countdownInterval;
let board;
let boardwidth = 360;
let boardheight = 940;
let context;
let quitButton;
let birdwidth = 34; //width/height ratio=408/228=17/12
let birdheight = 24;
let birdX = boardwidth / 8;
let birdY = boardheight / 2;
let birdimg;
let bird = { //Object of bird
x: birdX,
y: birdY,
width: birdwidth,
height: birdheight
}
//pipes
let pipeArray = [];
let pipeWidth = 64; //width/height ratio=384/30721=1/8;
let pipeheight = 512; // Reduced pipe height
let pipeX = boardwidth;
let pipeY = 0;
let topPipeimg;
let bottomPipeimg;
//Physics
let velocityx = -2; // Reduced bird speed
let velocityY = 0; //bird jump speed
let gravity = 0.4;
let gameOver = false;
let score = 0;
let highScore = localStorage.getItem('flappyBirdHighScore') || 0;
let gameStarted = false; // Variable to track if the game has started
window.onload = function () {
startButton = document.getElementById('startButton');
startButton.addEventListener('click', startGame);
board = document.getElementById("board");
board.height = boardheight;
board.width = boardwidth;
context = board.getContext("2d"); //used for drawing on the board
quitButton = document.getElementById('quitButton');
quitButton.addEventListener('click', () => {
const confirmQuit = confirm('Are you sure you want to quit?');
if (confirmQuit) {
window.location.href = 'Front.html';
}
});
//Load images
birdimg = new Image();
birdimg.src = "flappybird.png";
birdimg.onload = function () {
// Place initial pipes before starting the game
placePipes();
requestAnimationFrame(update); // Start the game loop
setInterval(placePipes, 1500); //every 1.5seconds
}
topPipeimg = new Image();
topPipeimg.src = "toppipe.png";
bottomPipeimg = new Image();
bottomPipeimg.src = "bottompipe.png";
document.addEventListener("keydown", moveBird);
}
function startGame() {
startButton.style.display = 'none'; // Hide the start button
countdown = 3; // Reset countdown
updateCountdown(); // Display the initial countdown
countdownInterval = setInterval(updateCountdown, 1000); // Start countdown
}
function updateCountdown() {
if (countdown > 0) {
context.clearRect(0, 0, board.width, board.height);
context.fillStyle = 'white';
context.font = '60px sans-serif';
context.fillText(countdown, board.width / 2 - 20, board.height / 2);
countdown--;
} else {
clearInterval(countdownInterval); // Stop the countdown
}
}
function update() {
if (!gameOver && countdown == 0) { // Check if the game has started
context.clearRect(0, 0, board.width, board.height);
//bird
velocityY += gravity;
bird.y = Math.max(bird.y + velocityY, 0);
context.drawImage(birdimg, bird.x, bird.y, bird.width, bird.height);
if (bird.y > board.height) {
gameOver = true;
}
//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 += 1;
pipe.passed = true;
}
if (detectCollision(bird, pipeArray[i])) {
gameOver = true;
}
}
while (pipeArray.length > 0 && pipeArray[0].x < -pipeWidth) {
pipeArray.shift();
}
updateHighScore();
//score
context.fillStyle = "white";
context.font = "45px sans-serif";
context.fillText(score, 5, 45);
context.fillStyle = "white";
context.font = "27px sans-serif";
context.fillText("High Score: " + highScore, board.width - 200, 45);
}
if (gameOver) {
context.fillStyle = "white";
context.font = "25px sans-serif";
context.fillText("GAME OVER", 5, 90);
context.fillText("Up arrow key to start again", 5, 140); // Display restart instructions
}
requestAnimationFrame(update);
}
function placePipes() {
if (gameOver) {
return;
}
let randomPipeY = pipeY - pipeheight / 4 - Math.random() * (pipeheight / 2); // Adjusted randomPipeY calculation
let openingSpace = board.height /4+40;
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") {
if (!gameOver && countdown == 0) { // Check if the game has started and the countdown is finished
velocityY = -6;
}
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 updateHighScore() {
if (score > highScore) {
highScore = score;
localStorage.setItem('flappyBirdHighScore', highScore);
}
}