-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappybook.js
160 lines (136 loc) · 4.21 KB
/
flappybook.js
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
const book = document.querySelector('.book');
const container = document.querySelector('.container');
const pipeTop = document.querySelector('.pipe.top');
const pipeBottom = document.querySelector('.pipe.bottom');
const startMessage = document.querySelector('.start-message');
const gameOverMessage = document.querySelector('.game-over-message');
let bookY = container.clientHeight / 2;
let bookVelocity = 0;
let gravity = 0.3;
let jumpForce = -8;
let isOpen = false;
let pipeX = container.clientWidth;
let pipeGap = 450;
let pipeWidth = 80;
let score = 0;
let isGameRunning = false;
let isGameOver = false;
function setViewportHeight() {
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
function update() {
if (!isGameRunning) return;
// Update book position
bookY += bookVelocity;
bookVelocity += gravity;
book.style.top = `${bookY}px`;
// Update pipe position
pipeX -= 6;
if (pipeX < -pipeWidth) {
pipeX = container.clientWidth;
setPipeHeights();
score++;
}
pipeTop.style.right = pipeBottom.style.right = `${container.clientWidth - pipeX}px`;
// Check for collisions
if (checkCollision()) {
gameOver();
return;
}
requestAnimationFrame(update);
}
function checkCollision() {
const bookRect = book.getBoundingClientRect();
const topPipeRect = pipeTop.getBoundingClientRect();
const bottomPipeRect = pipeBottom.getBoundingClientRect();
return (
bookRect.right > topPipeRect.left &&
bookRect.left < topPipeRect.right &&
(bookRect.top < topPipeRect.bottom || bookRect.bottom > bottomPipeRect.top)
) || bookY < 0 || bookY > container.clientHeight - book.clientHeight;
}
function jump() {
if (isGameOver) {
resetGame();
return;
}
if (!isGameRunning) {
startGame();
return;
}
bookVelocity = jumpForce;
toggleBook();
}
function toggleBook() {
isOpen = !isOpen;
book.textContent = isOpen ? '📖' : '📘';
}
function setPipeHeights() {
const minHeight = 100;
const maxHeight = container.clientHeight - pipeGap - minHeight;
const topHeight = Math.floor((Math.random() * (maxHeight - minHeight) + minHeight) / 120) * 120;
pipeTop.style.height = `${topHeight}px`;
pipeBottom.style.height = `${container.clientHeight - topHeight - pipeGap}px`;
// Adjust background position for bottom pipe
const bottomPipeHeight = container.clientHeight - topHeight - pipeGap;
const bottomPipeBackgroundPosition = `0px -${120 - (bottomPipeHeight % 120)}px`;
pipeBottom.style.backgroundPosition = bottomPipeBackgroundPosition;
}
function gameOver() {
isGameRunning = false;
isGameOver = true;
showGameOverMessage();
}
function resetGame() {
bookY = container.clientHeight / 2;
bookVelocity = 0;
book.style.top = `${bookY}px`;
book.textContent = '📘';
isOpen = false;
score = 0;
resetPipePosition();
setPipeHeights();
hideGameOverMessage();
showStartMessage();
isGameOver = false;
}
function resetPipePosition() {
pipeX = container.clientWidth;
pipeTop.style.right = pipeBottom.style.right = '0px';
}
function showStartMessage() {
startMessage.style.display = 'block';
gameOverMessage.style.display = 'none';
}
function hideStartMessage() {
startMessage.style.display = 'none';
}
function showGameOverMessage() {
gameOverMessage.innerHTML = `Game over!<br>Your score: ${score}<br>Tap to play again`;
gameOverMessage.style.display = 'block';
}
function hideGameOverMessage() {
gameOverMessage.style.display = 'none';
}
function startGame() {
isGameRunning = true;
isGameOver = false;
hideStartMessage();
hideGameOverMessage();
setViewportHeight(); // Set correct height before starting the game
requestAnimationFrame(update);
}
// Event listeners for touch and click
container.addEventListener('click', jump);
container.addEventListener('touchstart', (e) => {
e.preventDefault();
jump();
});
// Set the height on page load
setViewportHeight();
// Update the height on window resize
window.addEventListener('resize', setViewportHeight);
// Initial setup
setPipeHeights();
resetGame();