-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
751 lines (699 loc) · 25.9 KB
/
index.html
File metadata and controls
751 lines (699 loc) · 25.9 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>FrogEdge</title>
<script src="https://cdn.jsdelivr.net/npm/@farcade/game-sdk@latest/dist/index.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #4caf50;
overflow: hidden;
touch-action: none;
}
#game-container {
width: 100%;
max-width: 500px;
height: 100%;
max-height: 1000px;
position: relative;
}
canvas {
width: 100%;
height: 100%;
display: block;
background-color: #87ceeb;
}
/* Font pixel cho hướng dẫn */
#welcomeScreen .pixel-font {
font-family: 'Press Start 2P', monospace;
font-size: 0.6em;
line-height: 1.5;
max-width: 95vw;
}
@media (max-width: 400px) {
#welcomeScreen .pixel-font {
font-size: 0.38em;
}
}
#startBtn.pixel-font {
font-family: 'Press Start 2P', monospace;
font-size: 1.1em;
}
</style>
</head>
<body>
<div id="game-container">
<canvas id="gameCanvas"></canvas>
</div>
<!-- Trang hướng dẫn -->
<div id="welcomeScreen" style="
position:fixed;top:0;left:0;width:100vw;height:100vh;
background:rgba(0,0,0,0.7);color:#fff;z-index:100;display:flex;flex-direction:column;align-items:center;justify-content:center;">
<div class="pixel-font" style="max-width:400px;text-align:center;font-size:1.3em;margin-bottom:40px;">
<b>How to Play:</b><br>
- Press <b>←</b> or <b>→</b> to move<br>
- Press <b>Space</b> or <b>↑</b> to jump<br>
- Long press key to jump higher<br>
<div style="margin-top:16px;">
<img src="/assets/logoedge.png" alt="Item" style="width:86px;height:86px;vertical-align:middle;">
<span style="font-size:1em;vertical-align:middle;">Collect items to increase score and survival</span>
</div>
- Avoid obstacles and don't fall down!<br>
<span style="font-size:1em;vertical-align:middle;">Created by jason</span>
</div>
<button id="startBtn" class="pixel-font" style="font-size:2em;padding:20px 60px;cursor:pointer;">Start</button>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const gameContainer = document.getElementById("game-container");
// Responsive canvas sizing
function resizeCanvas() {
const maxWidth = 500;
const maxHeight = 1000;
const aspectRatio = maxWidth / maxHeight;
let width = Math.min(window.innerWidth, maxWidth);
let height = width / aspectRatio;
if (height > window.innerHeight || height > maxHeight) {
height = Math.min(window.innerHeight, maxHeight);
width = height * aspectRatio;
}
canvas.width = width;
canvas.height = height;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
gameContainer.style.width = `${width}px`;
gameContainer.style.height = `${height}px`;
}
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
// Game state
let gameState = {
score: 0,
highScore: localStorage.getItem("highScore") || 0,
gameOver: false,
cameraY: 0,
difficulty: 1,
lastPlatformY: 0,
isMuted: false, // Placeholder for audio mute state
hasStarted: false, // Đã nhảy lần đầu chưa
hasLanded: false, // Đã tiếp đất sau nhảy đầu chưa
timeLeft: 30, // Thời gian còn lại (giây)
};
// Frog
const frog = {
x: canvas.width / 2,
y: canvas.height - 50,
width: 40,
height: 40,
vx: 0,
vy: 0,
jumpPower: 0,
maxJumpPower: 20,
isJumping: false,
onGround: true,
};
const maxMoveSpeed = 6;
const moveAccel = 1.2;
const friction = 0.7;
// Platforms, obstacles, collectibles, particles
let platforms = [];
let obstacles = [];
let collectibles = [];
let particles = [];
// Input handling
let moveDirection = 0;
let isHolding = false;
let holdStartTime = 0;
const maxJump = 28;
const minJump = 13;
const maxHold = 1; // giữ tối đa 1 giây để đạt lực nhảy tối đa
// Load frog sprites
const frogLeftSprite = new Image();
frogLeftSprite.src = '/assets/pepeLeft.png';
const frogRightSprite = new Image();
frogRightSprite.src = '/assets/pepeRight.png';
const frogIdleSprite = new Image();
frogIdleSprite.src = '/assets/pepeStand.png';
const frogFrameCount = 6;
let frogFrame = 0;
let frogFrameTimer = 0;
const frogFrameIntervalIdle = 0.07; // Đứng yên: rất nhanh
const frogFrameIntervalMove = 0.12; // Di chuyển: bình thường
let idleFrameWidth = 100, idleFrameHeight = 100;
let leftFrameWidth = 100, leftFrameHeight = 100;
let rightFrameWidth = 100, rightFrameHeight = 100;
let frogState = 'idle'; // 'idle', 'left', 'right'
let prevFrogState = 'idle';
const idleFrameCount = 3; // chỉ dùng 3 frame đầu cho idle
function updateFrogFrameSize(sprite) {
if (!frogSizeInitialized) {
frogFrameWidth = sprite.width / frogFrameCount;
frogFrameHeight = sprite.height;
frog.width = frogFrameWidth;
frog.height = frogFrameHeight;
frogSizeInitialized = true;
}
}
frogRightSprite.onload = function() {
rightFrameWidth = frogRightSprite.width / frogFrameCount;
rightFrameHeight = frogRightSprite.height;
};
frogLeftSprite.onload = function() {
leftFrameWidth = frogLeftSprite.width / frogFrameCount;
leftFrameHeight = frogLeftSprite.height;
};
frogIdleSprite.onload = function() {
idleFrameWidth = frogIdleSprite.width / frogFrameCount;
idleFrameHeight = frogIdleSprite.height;
};
// Thêm load ảnh item vàng
const itemImg = new Image();
itemImg.src = '/assets/logoedge.png';
// Thêm load ảnh bigbot
const bigbotImg = new Image();
bigbotImg.src = '/assets/bigbot.png';
// Keyboard controls
document.addEventListener("keydown", (e) => {
if (!gameState.gameOver) {
if (e.key === "ArrowLeft" || e.key === "a") moveDirection = -1;
if (e.key === "ArrowRight" || e.key === "d") moveDirection = 1;
if ((e.key === " " || e.key === "ArrowUp") && frog.onGround && !isHolding) {
isHolding = true;
holdStartTime = Date.now();
}
} else if (e.key === "Enter") {
resetGame();
}
});
document.addEventListener("keyup", (e) => {
if (e.key === "ArrowLeft" || e.key === "a" || e.key === "ArrowRight" || e.key === "d") {
moveDirection = 0;
}
if ((e.key === " " || e.key === "ArrowUp") && isHolding) {
if (frog.onGround) {
const holdTime = (Date.now() - holdStartTime) / 1000; // không giới hạn
const jumpPower = minJump + (maxJump - minJump) * holdTime;
frog.vy = -jumpPower;
frog.isJumping = true;
frog.onGround = false;
if (!gameState.hasStarted) gameState.hasStarted = true;
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.hapticFeedback();
}
}
isHolding = false;
}
});
// Touch controls
canvas.addEventListener("touchstart", (e) => {
e.preventDefault();
const touchX = e.touches[0].clientX - canvas.getBoundingClientRect().left;
if (!gameState.gameOver) {
if (touchX < canvas.width / 3) {
moveDirection = -1; // Move left
} else if (touchX > (canvas.width * 2) / 3) {
moveDirection = 1; // Move right
} else if (frog.onGround && !isHolding) {
isHolding = true;
holdStartTime = Date.now();
}
} else {
resetGame();
}
});
canvas.addEventListener("touchend", (e) => {
e.preventDefault();
if (isHolding && frog.onGround) {
const holdTime = (Date.now() - holdStartTime) / 1000; // không giới hạn
const jumpPower = minJump + (maxJump - minJump) * holdTime;
frog.vy = -jumpPower;
frog.isJumping = true;
frog.onGround = false;
if (!gameState.hasStarted) gameState.hasStarted = true;
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.hapticFeedback();
}
}
isHolding = false;
moveDirection = 0;
});
// Platform generation (platform rộng hơn, nhiều chướng ngại vật hơn)
function generatePlatform(y) {
const minWidth = 140;
const maxWidth = 180;
const width = minWidth + Math.random() * (maxWidth - minWidth);
// Đảm bảo bậc không random quá sát mép
const margin = 20;
const x = margin + Math.random() * (canvas.width - width - 2 * margin);
// 70% bậc xanh, 30% bậc xám
const type = Math.random() < 0.7 ? "normal" : "falling";
platforms.push({
x: x,
y: y,
width: width,
height: 20,
fade: 1,
landed: false,
type: type,
fallTimer: type === "falling" ? null : undefined,
});
// Chỉ sinh collectibles là fly (item/logoedge)
collectibles.push({
x: x + width / 2 - 20,
y: y - 80,
width: 40,
height: 40,
type: "fly",
});
// Chỉ sinh nước (water) đi ngang với xác suất thấp
if (Math.random() < 0.02 * gameState.difficulty) {
obstacles.push({
x: 0,
y: y + 30,
width: canvas.width,
height: 50,
type: "water",
speed: 2 + gameState.difficulty * 1.2,
});
}
}
// Generate rushing water
function generateWater(y) {
obstacles.push({
x: 0,
y: y,
width: canvas.width,
height: 50,
type: "water",
speed: 2 + gameState.difficulty * 0.5,
});
}
// Initialize platforms
function initPlatforms() {
platforms = [];
for (let y = canvas.height - 50; y > -canvas.height; y -= 100) {
generatePlatform(y);
}
gameState.lastPlatformY = -canvas.height;
}
// Particle effects
function createSplash(x, y) {
for (let i = 0; i < 10; i++) {
particles.push({
x: x,
y: y,
vx: (Math.random() - 0.5) * 5,
vy: (Math.random() - 0.5) * 5,
size: 5 + Math.random() * 5,
life: 1,
});
}
// Haptic feedback on splash
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.hapticFeedback();
}
}
// Helper: lấy chiều rộng frame theo trạng thái
function getCurrentFrogFrameWidth() {
if (frogState === 'idle') return idleFrameWidth;
if (frogState === 'left') return leftFrameWidth;
if (frogState === 'right') return rightFrameWidth;
return idleFrameWidth;
}
// Helper: lấy chiều cao frame theo trạng thái
function getCurrentFrogFrameHeight() {
if (frogState === 'idle') return idleFrameHeight;
if (frogState === 'left') return leftFrameHeight;
if (frogState === 'right') return rightFrameHeight;
return idleFrameHeight;
}
// Update game
function update() {
if (gameState.gameOver) return;
// Tăng tốc độ di chuyển trái/phải và gravity theo difficulty
const moveSpeed = 5 + (gameState.difficulty - 1) * 2; // tăng dần
const gravity = 0.5 + (gameState.difficulty - 1) * 0.3; // tăng dần
if (!gameState.hasStarted) {
// Chưa nhảy: chỉ cho phép di chuyển trái/phải trên platform đầu tiên
if (platforms.length > 0) {
const firstPlatform = platforms.reduce((max, p) => (p.y > max.y ? p : max), platforms[0]);
if (moveDirection !== 0) {
frog.vx += moveDirection * moveAccel;
if (frog.vx > maxMoveSpeed) frog.vx = maxMoveSpeed;
if (frog.vx < -maxMoveSpeed) frog.vx = -maxMoveSpeed;
} else {
frog.vx *= friction;
if (Math.abs(frog.vx) < 0.1) frog.vx = 0;
}
frog.x += frog.vx;
// Giữ ếch trong platform đầu tiên
if (frog.x < firstPlatform.x) frog.x = firstPlatform.x;
if (frog.x + frog.width > firstPlatform.x + firstPlatform.width)
frog.x = firstPlatform.x + firstPlatform.width - frog.width;
frog.y = firstPlatform.y - getCurrentFrogFrameHeight();
frog.vy = 0;
}
frogState = 'idle';
return; // Không update gì thêm khi chưa nhảy
}
// Đã nhảy: update như bình thường
// Update frog (di chuyển ngang mượt)
if (moveDirection !== 0) {
frog.vx += moveDirection * moveAccel;
if (frog.vx > maxMoveSpeed) frog.vx = maxMoveSpeed;
if (frog.vx < -maxMoveSpeed) frog.vx = -maxMoveSpeed;
} else {
// Áp dụng ma sát khi không nhấn phím
frog.vx *= friction;
if (Math.abs(frog.vx) < 0.1) frog.vx = 0;
}
frog.x += frog.vx;
frog.y += frog.vy;
frog.vy += gravity; // Gravity tăng dần
// Keep frog in bounds
if (frog.x < 0) frog.x = 0;
if (frog.x + frog.width > canvas.width) frog.x = canvas.width - frog.width;
// Camera follow
if (frog.y < canvas.height / 3) {
gameState.cameraY = canvas.height / 3 - frog.y;
gameState.score = Math.max(gameState.score, Math.floor(-frog.y / 10));
gameState.difficulty = 1 + gameState.score / 500;
}
// Check collisions
let onPlatform = false;
let currentPlatform = null;
platforms.forEach((p) => {
const hitboxPadding = getCurrentFrogFrameWidth() * 0.15; // 15% mỗi bên
const hitboxLeft = frog.x + hitboxPadding;
const hitboxRight = frog.x + getCurrentFrogFrameWidth() - hitboxPadding;
if (
frog.vy > 0 &&
hitboxRight > p.x &&
hitboxLeft < p.x + p.width &&
Math.abs(frog.y + getCurrentFrogFrameHeight() - p.y) < 6
) {
frog.y = p.y - getCurrentFrogFrameHeight();
frog.vy = 0;
frog.onGround = true;
frog.isJumping = false;
onPlatform = true;
currentPlatform = p;
// Nếu là bậc xám thì bắt đầu đếm ngược 2s
if (p.type === "falling" && p.fallTimer == null) {
p.fallTimer = 2;
}
// Đánh dấu đã tiếp đất sau nhảy đầu
if (gameState.hasStarted) gameState.hasLanded = true;
// Haptic feedback on landing
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.hapticFeedback();
}
}
});
frog.onGround = onPlatform;
// Check obstacles
obstacles.forEach((o) => {
if (
frog.x + frog.width > o.x &&
frog.x < o.x + o.width &&
frog.y + frog.height > o.y &&
frog.y < o.y + o.height
) {
if ((o.type === "water" || o.type === "crocodile") && gameState.hasStarted && gameState.hasLanded) {
createSplash(frog.x + frog.width / 2, frog.y + frog.height);
gameOver();
}
}
if (o.type === "water") {
o.x += o.speed;
if (o.x > canvas.width) o.x = -o.width;
}
});
// Check collectibles
collectibles = collectibles.filter((c) => {
if (
frog.x + frog.width > c.x &&
frog.x < c.x + c.width &&
frog.y + frog.height > c.y &&
frog.y < c.y + c.height
) {
if (c.type === "fly") {
gameState.score += 50;
gameState.timeLeft += 2; // Cộng thêm 2 giây
// Haptic feedback on collecting fly
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.hapticFeedback();
}
return false;
}
}
return true;
});
// Platform fading
platforms = platforms.filter((p) => p.type !== "falling" || p.fade > 0);
// Giảm fallTimer cho các bậc xám đang chờ biến mất
const delta = 1 / 60; // giả định 60fps, hoặc lấy từ gameLoop nếu có
platforms.forEach((p) => {
if (p.type === "falling" && p.fallTimer != null) {
p.fallTimer -= delta;
}
});
// Xóa bậc xám khi fallTimer <= 0
platforms = platforms.filter((p) => p.type !== "falling" || p.fallTimer == null || p.fallTimer > 0);
// Generate new platforms vô hạn
while (gameState.lastPlatformY > frog.y - 2 * canvas.height) {
generatePlatform(gameState.lastPlatformY - 70);
gameState.lastPlatformY -= 70;
}
// Update particles
particles.forEach((p) => {
p.x += p.vx;
p.y += p.vy;
p.life -= 0.05;
p.size *= 0.95;
});
particles = particles.filter((p) => p.life > 0);
// Check for falling
if (frog.y > canvas.height + gameState.cameraY && gameState.hasStarted && gameState.hasLanded) {
createSplash(frog.x + frog.width / 2, canvas.height);
gameOver();
}
// Reset frame khi đổi trạng thái
if (frogState !== prevFrogState) {
frogFrame = 0;
frogFrameTimer = 0;
}
prevFrogState = frogState;
// Cập nhật hoạt ảnh
let currentFrameInterval = (frogState === 'idle') ? frogFrameIntervalIdle : frogFrameIntervalMove;
if (frog.onGround) {
frogFrameTimer += 1/60;
if (frogFrameTimer > currentFrameInterval) {
frogFrame = (frogFrame + 1) % frogFrameCount;
frogFrameTimer = 0;
}
} else {
frogFrame = 0;
}
// Xác định trạng thái frogState
if (!gameState.hasStarted) {
frogState = 'idle';
} else if (moveDirection === -1 && frog.onGround) {
frogState = 'left';
} else if (moveDirection === 1 && frog.onGround) {
frogState = 'right';
} else if (moveDirection === 0 && frog.onGround) {
frogState = 'idle';
}
}
// Lose game (game over)
function gameOver() {
gameState.gameOver = true;
if (gameState.score > gameState.highScore) {
gameState.highScore = gameState.score;
localStorage.setItem("highScore", gameState.highScore);
}
// Call Farcade gameOver with score
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.gameOver({ score: gameState.score });
}
}
// Reset game
function resetGame() {
gameState.score = 0;
gameState.gameOver = false;
gameState.cameraY = 0;
gameState.difficulty = 1;
gameState.lastPlatformY = canvas.height - 50;
gameState.hasStarted = false;
gameState.hasLanded = false;
gameState.timeLeft = 30;
platforms = [];
obstacles = [];
collectibles = [];
particles = [];
initPlatforms();
// Đặt ếch lên đúng platform đầu tiên
if (platforms.length > 0) {
const firstPlatform = platforms.reduce((max, p) => (p.y > max.y ? p : max), platforms[0]);
frog.x = firstPlatform.x + firstPlatform.width / 2 - idleFrameWidth / 2;
frog.y = firstPlatform.y - idleFrameHeight;
} else {
frog.x = canvas.width / 2;
frog.y = canvas.height - idleFrameHeight;
}
frog.vy = 0;
frog.onGround = true;
// Call ready again after reset
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.ready();
}
}
// Farcade SDK event handlers
if (window.FarcadeSDK) {
// Handle play again
window.FarcadeSDK.on("play_again", () => {
resetGame();
});
// Handle mute/unmute
window.FarcadeSDK.on("toggle_mute", (data) => {
gameState.isMuted = data.isMuted;
// Placeholder: Update audio state when audio is added
console.log(`Audio muted: ${gameState.isMuted}`);
});
}
// Draw game
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Apply camera
ctx.save();
ctx.translate(0, gameState.cameraY);
// Draw platforms
platforms.forEach((p) => {
if (p.type === "falling") {
ctx.fillStyle = `rgba(120, 120, 120, 1)`;
} else {
ctx.fillStyle = `rgba(34, 139, 34, 1)`;
}
ctx.fillRect(p.x, p.y, p.width, p.height);
});
// Draw obstacles
obstacles.forEach((o) => {
if (o.type === "crocodile") {
ctx.fillStyle = "#228B22";
ctx.fillRect(o.x, o.y, o.width, o.height);
} else if (o.type === "water") {
// Vẽ hình bigbot thay cho hình chữ nhật màu xanh
ctx.drawImage(bigbotImg, o.x, o.y, o.width, o.height);
}
});
// Draw collectibles
collectibles.forEach((c) => {
if (c.type === "fly") {
// Vẽ hình ảnh item/logoedge với kích thước lớn hơn
ctx.drawImage(itemImg, c.x, c.y, c.width, c.height);
}
});
// Draw frog
if (frogState === 'idle') {
ctx.drawImage(
frogIdleSprite,
frogFrame * idleFrameWidth, 0, idleFrameWidth, idleFrameHeight,
frog.x, frog.y, idleFrameWidth, idleFrameHeight
);
} else if (frogState === 'left') {
ctx.drawImage(
frogLeftSprite,
frogFrame * leftFrameWidth, 0, leftFrameWidth, leftFrameHeight,
frog.x, frog.y, leftFrameWidth, leftFrameHeight
);
} else if (frogState === 'right') {
ctx.drawImage(
frogRightSprite,
frogFrame * rightFrameWidth, 0, rightFrameWidth, rightFrameHeight,
frog.x, frog.y, rightFrameWidth, rightFrameHeight
);
}
// Draw particles
particles.forEach((p) => {
ctx.fillStyle = `rgba(0, 105, 148, ${p.life})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
ctx.fill();
});
ctx.restore();
// Draw HUD
ctx.save();
ctx.font = "12px 'Press Start 2P', monospace";
ctx.fillStyle = "#000";
ctx.textAlign = "left";
ctx.fillText(`Score: ${gameState.score}`, 10, 30);
ctx.fillText(`High: ${gameState.highScore}`, 10, 60);
ctx.textAlign = "right";
ctx.fillText(`Time: ${Math.ceil(gameState.timeLeft)}s`, canvas.width - 10, 30);
ctx.restore();
// Game over screen
if (gameState.gameOver) {
ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#FFF";
ctx.font = "40px Arial";
ctx.textAlign = "center";
ctx.fillText("Game Over", canvas.width / 2, canvas.height / 2 - 50);
ctx.font = "30px Arial";
ctx.fillText(`Score: ${gameState.score}`, canvas.width / 2, canvas.height / 2);
ctx.fillText(`High Score: ${gameState.highScore}`, canvas.width / 2, canvas.height / 2 + 50);
ctx.font = "20px Arial";
ctx.fillText("Tap or Press Enter to Restart", canvas.width / 2, canvas.height / 2 + 100);
ctx.textAlign = "left";
}
// Trang hướng dẫn và nút Start
const welcomeScreen = document.getElementById('welcomeScreen');
if (!gameState.hasStarted) {
welcomeScreen.style.display = 'flex';
} else {
welcomeScreen.style.display = 'none';
}
}
// Game loop
let lastTimestamp = null;
function gameLoop(timestamp) {
if (!lastTimestamp) lastTimestamp = timestamp;
const delta = (timestamp - lastTimestamp) / 1000; // giây
lastTimestamp = timestamp;
if (!gameState.gameOver && gameState.hasStarted) {
gameState.timeLeft -= delta;
if (gameState.timeLeft <= 0) {
gameState.timeLeft = 0;
gameOver();
}
}
update();
draw();
requestAnimationFrame(gameLoop);
}
// Initialize and start
initPlatforms();
// Call Farcade ready when game is loaded
if (window.FarcadeSDK) {
window.FarcadeSDK.singlePlayer.actions.ready();
}
// Trang hướng dẫn và nút Start
const welcomeScreen = document.getElementById('welcomeScreen');
const startBtn = document.getElementById('startBtn');
startBtn.onclick = function() {
gameState.hasStarted = true;
welcomeScreen.style.display = 'none';
};
gameLoop();
</script>
</body>
</html>