-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
329 lines (310 loc) · 9.73 KB
/
game.js
File metadata and controls
329 lines (310 loc) · 9.73 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
(function (global) {
'use strict';
var canvas = null;
var ctx = null;
var width = 400;
var height = 600;
var bird = { x: 80, y: 300, vy: 0, w: 32, h: 24 };
var gravity = 0.15;
var impulseUp = -6;
var impulseDown = 4;
var pipes = [];
var pipeWidth = 60;
var gap = 480;
var pipeSpeed = 1.25;
var score = 0;
var lives = 3;
var gameOver = false;
var gameOverAt = 0;
var invincibleUntil = 0;
var running = false;
var controlDirection = 'neutral';
var lastTime = 0;
var gameStartTime = 0;
var passedPipeIds = {};
var scaleW = 1;
var scaleH = 1;
var heartPath = null;
var birdFrames = [];
var BIRD_ANIM_INTERVAL = 80;
var pipeImg = null;
var HEART_PATH_D = 'M12 6.00019C10.2006 3.90317 7.19377 3.2551 4.93923 5.17534C2.68468 7.09558 2.36727 10.3061 4.13778 12.5772C5.60984 14.4654 10.0648 18.4479 11.5249 19.7369C11.6882 19.8811 11.7699 19.9532 11.8652 19.9815C11.9483 20.0062 12.0393 20.0062 12.1225 19.9815C12.2178 19.9532 12.2994 19.8811 12.4628 19.7369C13.9229 18.4479 18.3778 14.4654 19.8499 12.5772C21.6204 10.3061 21.3417 7.07538 19.0484 5.17534C16.7551 3.2753 13.7994 3.90317 12 6.00019Z';
function updateScale() {
scaleW = width / 400;
scaleH = height / 600;
bird.x = 80 * scaleW;
bird.w = 68 * scaleW;
bird.h = 48 * scaleH;
pipeWidth = 60 * scaleW;
gap = 480 * scaleH;
pipeSpeed = 1.25 * scaleW;
}
function init(canvasEl) {
canvas = canvasEl;
if (!canvas) return;
ctx = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
try { heartPath = new Path2D(HEART_PATH_D); } catch (e) { heartPath = null; }
if (birdFrames.length === 0) {
var urls = ['yellowbird-midflap.png', 'yellowbird-midflap-1.png', 'yellowbird-midflap-2.png'];
for (var i = 0; i < urls.length; i++) {
var img = new Image();
img.src = urls[i];
birdFrames.push(img);
}
}
if (!pipeImg) {
pipeImg = new Image();
pipeImg.src = 'pipe-green.png';
}
updateScale();
reset();
}
function resize(w, h) {
if (!canvas) return;
width = w;
height = h;
canvas.width = w;
canvas.height = h;
updateScale();
reset();
}
function reset() {
bird.y = height / 2 - bird.h / 2;
bird.vy = 0;
pipes = [];
score = 0;
lives = 3;
gameOver = false;
gameOverAt = 0;
invincibleUntil = 0;
controlDirection = 'neutral';
passedPipeIds = {};
lastTime = 0;
}
function setControl(direction) {
controlDirection = direction;
}
function spawnPipe() {
var minTop = 80 * scaleH;
var maxTop = height - gap - 80 * scaleH;
var topHeight = minTop + Math.random() * (maxTop - minTop);
var id = Date.now() + Math.random();
pipes.push({
id: id,
x: width,
top: { x: width, y: 0, w: pipeWidth, h: topHeight },
gapY: topHeight,
gapH: gap,
bottom: {
x: width,
y: topHeight + gap,
w: pipeWidth,
h: height - topHeight - gap
}
});
}
function updatePipes(dt) {
var norm = Math.min(dt / 16, 2);
for (var i = 0; i < pipes.length; i++) {
var p = pipes[i];
p.x -= pipeSpeed * norm;
p.top.x = p.x;
p.bottom.x = p.x;
}
pipes = pipes.filter(function (p) { return p.x + pipeWidth > 0; });
var gameTime = (performance.now() - gameStartTime) / 1000;
var pipeTarget = 3 + Math.min(2, Math.floor(gameTime / 30));
while (pipes.length < pipeTarget) spawnPipe();
for (var j = 0; j < pipes.length; j++) {
var pipe = pipes[j];
if (!passedPipeIds[pipe.id] && pipe.x + pipeWidth < bird.x) {
passedPipeIds[pipe.id] = true;
score++;
}
}
}
function aabb(ax, ay, aw, ah, bx, by, bw, bh) {
return ax < bx + bw && ax + aw > bx && ay < by + bh && ay + ah > by;
}
function checkCollision() {
var bx = bird.x;
var by = bird.y;
var bw = bird.w;
var bh = bird.h;
if (by <= 0 || by + bh >= height) return true;
for (var i = 0; i < pipes.length; i++) {
var p = pipes[i];
if (p.x + p.top.w < bx || p.x > bx + bw) continue;
if (aabb(bx, by, bw, bh, p.top.x, p.top.y, p.top.w, p.top.h)) return true;
if (aabb(bx, by, bw, bh, p.bottom.x, p.bottom.y, p.bottom.w, p.bottom.h)) return true;
}
return false;
}
function update(time) {
if (!running || gameOver) return;
var dt = time - lastTime;
lastTime = time;
if (controlDirection === 'up') {
bird.vy = impulseUp;
} else if (controlDirection === 'down') {
bird.vy = impulseDown;
}
bird.vy += gravity * (dt / 16);
bird.y += bird.vy * (dt / 16);
updatePipes(dt);
if (Date.now() < invincibleUntil) return;
if (checkCollision()) {
if (lives > 1) {
lives--;
invincibleUntil = Date.now() + 1500;
bird.y = height / 2 - bird.h / 2;
bird.vy = 0;
} else {
lives = 0;
gameOver = true;
gameOverAt = Date.now();
if (typeof global.onGameOver === 'function') global.onGameOver();
}
}
}
function draw() {
if (!ctx) return;
ctx.fillStyle = '#5ba3e8';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = '#7ec8e3';
ctx.fillRect(0, 0, width, height * 0.75);
ctx.fillStyle = '#2d6b2a';
ctx.fillRect(0, height * 0.7, width, height);
ctx.fillStyle = '#3d8b38';
ctx.fillRect(0, height * 0.72, width, height);
var birdImg = null;
var numFrames = birdFrames.length;
if (numFrames > 0) {
var elapsed = performance.now() - gameStartTime;
var frameIndex = Math.floor(elapsed / BIRD_ANIM_INTERVAL) % numFrames;
birdImg = birdFrames[frameIndex];
}
if (birdImg && birdImg.complete && birdImg.naturalWidth) {
var nw = birdImg.naturalWidth;
var nh = birdImg.naturalHeight;
var drawW = bird.h * (nw / nh);
var drawH = bird.h;
var drawX = bird.x + (bird.w - drawW) / 2;
ctx.drawImage(birdImg, drawX, bird.y, drawW, drawH);
} else {
ctx.fillStyle = '#f4d03f';
ctx.fillRect(bird.x, bird.y, bird.w, bird.h);
ctx.strokeStyle = '#c9a227';
ctx.lineWidth = Math.max(1, 2 * Math.min(scaleW, scaleH));
ctx.strokeRect(bird.x, bird.y, bird.w, bird.h);
}
if (pipeImg && pipeImg.complete && pipeImg.naturalWidth) {
for (var i = 0; i < pipes.length; i++) {
var p = pipes[i];
ctx.save();
ctx.translate(p.top.x, p.top.y + p.top.h);
ctx.scale(1, -1);
ctx.drawImage(pipeImg, 0, 0, p.top.w, p.top.h);
ctx.restore();
ctx.drawImage(pipeImg, p.bottom.x, p.bottom.y, p.bottom.w, p.bottom.h);
}
} else {
ctx.fillStyle = '#2d5a27';
ctx.strokeStyle = '#1e3d1a';
for (var i = 0; i < pipes.length; i++) {
var p = pipes[i];
ctx.fillRect(p.top.x, p.top.y, p.top.w, p.top.h);
ctx.strokeRect(p.top.x, p.top.y, p.top.w, p.top.h);
ctx.fillRect(p.bottom.x, p.bottom.y, p.bottom.w, p.bottom.h);
ctx.strokeRect(p.bottom.x, p.bottom.y, p.bottom.w, p.bottom.h);
}
}
ctx.fillStyle = '#fff';
var fontSize = 24 * Math.min(scaleW, scaleH);
ctx.font = fontSize + 'px system-ui, sans-serif';
ctx.fillText('' + score, width / 2 - fontSize * 0.5, 40 * scaleH);
var lifeSize = 54 * Math.min(scaleW, scaleH);
var lifeX = width - lifeSize * 3.2 - 8 * scaleW;
var lifeY = 8 * scaleH;
var strokeW = Math.max(1, 1.5 * Math.min(scaleW, scaleH));
ctx.strokeStyle = '#e94560';
ctx.lineWidth = strokeW;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
if (heartPath) {
for (var l = 0; l < 3; l++) {
ctx.save();
ctx.translate(lifeX + l * (lifeSize * 1.35), lifeY + lifeSize / 2);
ctx.scale(lifeSize / 24, lifeSize / 24);
ctx.translate(-12, -12);
if (l < lives) {
ctx.fillStyle = '#e94560';
ctx.fill(heartPath);
}
ctx.stroke(heartPath);
ctx.restore();
}
} else {
for (var l = 0; l < 3; l++) {
ctx.beginPath();
ctx.arc(lifeX + l * (lifeSize * 1.35), lifeY + lifeSize / 2, lifeSize / 2, 0, Math.PI * 2);
if (l < lives) {
ctx.fillStyle = '#e94560';
ctx.fill();
}
ctx.stroke();
}
}
if (gameOver) {
ctx.fillStyle = 'rgba(0,0,0,0.65)';
ctx.fillRect(0, 0, width, height);
var countSec = Math.max(0, Math.ceil((gameOverAt + 5000 - Date.now()) / 1000));
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = '#e94560';
ctx.font = 'bold ' + (32 * Math.min(scaleW, scaleH)) + 'px system-ui, sans-serif';
ctx.fillText('Game Over', width / 2, height / 2 - 35 * scaleH);
ctx.fillStyle = '#fff';
ctx.font = (20 * Math.min(scaleW, scaleH)) + 'px system-ui, sans-serif';
ctx.fillText('Счёт: ' + score, width / 2, height / 2 + 5 * scaleH);
ctx.font = (18 * Math.min(scaleW, scaleH)) + 'px system-ui, sans-serif';
ctx.fillStyle = 'rgba(255,255,255,0.9)';
ctx.fillText('Рестарт через ' + countSec + ' сек', width / 2, height / 2 + 45 * scaleH);
ctx.textAlign = 'left';
ctx.textBaseline = 'alphabetic';
}
}
function tick(time) {
update(time);
draw();
if (running) requestAnimationFrame(tick);
}
function start() {
reset();
running = true;
gameOver = false;
lastTime = performance.now();
gameStartTime = performance.now();
requestAnimationFrame(tick);
}
function stop() {
running = false;
}
function isGameOver() {
return gameOver;
}
function getRestartBtn() {
return document.getElementById('restartBtn');
}
global.Game = {
init: init,
resize: resize,
setControl: setControl,
start: start,
stop: stop,
reset: reset,
isGameOver: isGameOver
};
})(this);