-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
237 lines (195 loc) · 5.41 KB
/
Copy pathscript.js
File metadata and controls
237 lines (195 loc) · 5.41 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
const game = document.getElementById('game');
const bird = document.getElementById('bird');
const pipesContainer = document.getElementById('pipes');
const scoreEl = document.getElementById('score');
const finalScoreEl = document.getElementById('finalScore');
const hintEl = document.getElementById('hint');
const gameOverBox = document.getElementById('gameOverBox');
const restartBtn = document.getElementById('restartBtn');
const wingSound = new Audio("assets/sfx_wing.mp3");
const pointSound = new Audio("assets/sfx_point.mp3");
const dieSound = new Audio("assets/sfx_die.mp3");
wingSound.preload = "auto";
pointSound.preload = "auto";
dieSound.preload = "auto";
function playSound(sound) {
sound.currentTime = 0;
sound.play().catch(() => {});
}
const state = {
started: false,
over: false,
score: 0,
birdY: 260,
velocity: 0,
gravity: 0.42,
flapStrength: -7.2,
pipeSpeed: 2.2,
pipeGap: 170,
spawnTimer: 0,
spawnInterval: 1400,
lastTime: 0,
pipes: []
};
const BIRD_X = 90;
const BIRD_W = 56;
const BIRD_H = 40;
const GROUND_H = 72;
const PIPE_W = 72;
function getGameHeight() {
return game.clientHeight;
}
function getGameWidth() {
return game.clientWidth;
}
function resetGame() {
state.started = false;
state.over = false;
state.score = 0;
state.birdY = getGameHeight() * 0.42;
state.velocity = 0;
state.spawnTimer = 0;
state.lastTime = 0;
state.pipes = [];
pipesContainer.innerHTML = '';
scoreEl.textContent = '0';
finalScoreEl.textContent = '0';
hintEl.textContent = 'Clique, toque ou aperte espaço para começar';
gameOverBox.classList.add('hidden');
renderBird();
}
function startGame() {
if (state.over) return;
if (!state.started) {
state.started = true;
hintEl.textContent = '';
}
flap();
}
function flap() {
state.velocity = state.flapStrength;
playSound(wingSound);
}
function createPipePair() {
const minTop = 80;
const maxTop = getGameHeight() - GROUND_H - state.pipeGap - 120;
const topHeight = Math.floor(Math.random() * (maxTop - minTop + 1)) + minTop;
const bottomY = topHeight + state.pipeGap;
const bottomHeight = getGameHeight() - GROUND_H - bottomY;
const pairEl = document.createElement('div');
pairEl.className = 'pipe-pair';
pairEl.style.left = `${getGameWidth()}px`;
pairEl.style.width = `${PIPE_W}px`;
const topEl = document.createElement('div');
topEl.className = 'pipe top';
topEl.style.height = `${topHeight}px`;
const bottomEl = document.createElement('div');
bottomEl.className = 'pipe bottom';
bottomEl.style.height = `${bottomHeight}px`;
pairEl.appendChild(topEl);
pairEl.appendChild(bottomEl);
pipesContainer.appendChild(pairEl);
state.pipes.push({
x: getGameWidth(),
topHeight,
bottomY,
width: PIPE_W,
passed: false,
el: pairEl
});
}
function renderBird() {
bird.style.top = `${state.birdY}px`;
const rotation = Math.max(-25, Math.min(90, state.velocity * 7));
bird.style.transform = `rotate(${rotation}deg)`;
}
function hitPipe(pipe) {
const birdLeft = BIRD_X + 6;
const birdRight = BIRD_X + BIRD_W - 6;
const birdTop = state.birdY + 4;
const birdBottom = state.birdY + BIRD_H - 4;
const pipeLeft = pipe.x;
const pipeRight = pipe.x + pipe.width;
const overlapX = birdRight > pipeLeft && birdLeft < pipeRight;
const hitTop = birdTop < pipe.topHeight;
const hitBottom = birdBottom > pipe.bottomY;
return overlapX && (hitTop || hitBottom);
}
function endGame() {
if (state.over) return;
state.over = true;
state.started = false;
finalScoreEl.textContent = state.score;
gameOverBox.classList.remove('hidden');
hintEl.textContent = '';
playSound(dieSound);
}
function update(delta) {
if (!state.started || state.over) return;
state.velocity += state.gravity * delta * 0.06;
state.birdY += state.velocity * delta * 0.06;
const maxBirdY = getGameHeight() - GROUND_H - BIRD_H;
if (state.birdY < 0) {
state.birdY = 0;
state.velocity = 0;
}
if (state.birdY >= maxBirdY) {
state.birdY = maxBirdY;
renderBird();
endGame();
return;
}
state.spawnTimer += delta;
if (state.spawnTimer >= state.spawnInterval) {
state.spawnTimer = 0;
createPipePair();
}
for (let i = state.pipes.length - 1; i >= 0; i--) {
const pipe = state.pipes[i];
pipe.x -= state.pipeSpeed * delta * 0.06;
pipe.el.style.left = `${pipe.x}px`;
if (!pipe.passed && pipe.x + pipe.width < BIRD_X) {
pipe.passed = true;
state.score += 1;
scoreEl.textContent = state.score;
playSound(pointSound);
}
if (hitPipe(pipe)) {
renderBird();
endGame();
return;
}
if (pipe.x + pipe.width < -10) {
pipe.el.remove();
state.pipes.splice(i, 1);
}
}
renderBird();
}
function loop(timestamp) {
if (!state.lastTime) state.lastTime = timestamp;
const delta = Math.min(32, timestamp - state.lastTime);
state.lastTime = timestamp;
update(delta);
requestAnimationFrame(loop);
}
window.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
e.preventDefault();
if (state.over) return;
startGame();
}
});
game.addEventListener('mousedown', () => {
if (state.over) return;
startGame();
});
game.addEventListener('touchstart', (e) => {
e.preventDefault();
if (state.over) return;
startGame();
}, { passive: false });
restartBtn.addEventListener('click', resetGame);
window.addEventListener('resize', resetGame);
resetGame();
requestAnimationFrame(loop);