Nafiul Huda commited on
Commit
6f8b5de
·
verified ·
1 Parent(s): 5670a39

Update public/index.html

Browse files
Files changed (1) hide show
  1. public/index.html +627 -541
public/index.html CHANGED
@@ -1,660 +1,746 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
6
- <title>Jump Unlimited</title>
7
- <style>
8
- *{margin:0;padding:0;box-sizing:border-box;touch-action:none;user-select:none}
9
- html,body{width:100%;height:100%;overflow:hidden;background:#111;font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif}
10
- #C{position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);box-shadow: 0 0 50px rgba(0,0,0,0.5)}
11
- .menu{position:fixed;top:0;left:0;width:100%;height:100%;display:flex;justify-content:center;align-items:center;background:rgba(0,0,0,0.85);z-index:10;backdrop-filter:blur(5px)}
12
- .menu.hidden{display:none}
13
- .box{background:#1e1e2f;border:1px solid #444;border-radius:20px;padding:30px;text-align:center;color:#fff;width:90%;max-width:380px;box-shadow:0 10px 40px rgba(0,0,0,0.5)}
14
- .box h1{font-size:42px;margin-bottom:20px;background:linear-gradient(to right,#00ffa3,#00d4ff);-webkit-background-clip:text;-webkit-text-fill-color:transparent;font-weight:900;letter-spacing:-1px}
15
- .box input{width:100%;padding:15px;font-size:18px;border:2px solid #333;border-radius:12px;background:#151525;color:#fff;margin-bottom:15px;text-align:center;transition:0.3s}
16
- .box input:focus{outline:none;border-color:#00d4ff;box-shadow:0 0 15px rgba(0,212,255,0.2)}
17
- .box button{width:100%;padding:16px;font-size:20px;font-weight:800;border:none;border-radius:12px;background:linear-gradient(45deg,#00ffa3,#00d4ff);color:#00332a;cursor:pointer;transition:transform 0.1s}
18
- .box button:active{transform:scale(0.96)}
19
- .lb{margin-top:20px;text-align:left;max-height:150px;overflow-y:auto;background:#151525;border-radius:10px;padding:10px}
20
- .lb::-webkit-scrollbar{width:6px}
21
- .lb::-webkit-scrollbar-thumb{background:#444;border-radius:3px}
22
- .lb-row{display:flex;justify-content:space-between;padding:8px 10px;border-bottom:1px solid #2a2a3a;font-size:14px;color:#ccc}
23
- .lb-row:nth-child(1){color:#ffd700;font-weight:bold}
24
- .lb-row:nth-child(2){color:#c0c0c0}
25
- .lb-row:nth-child(3){color:#cd7f32}
26
- .score-display{font-size:48px;font-weight:800;margin:15px 0;color:#fff;text-shadow:0 0 20px rgba(0,255,163,0.3)}
27
- .sub{font-size:13px;color:#666;margin-top:15px}
28
- </style>
 
 
 
29
  </head>
30
  <body>
31
- <canvas id="C"></canvas>
32
-
33
- <div id="menuScreen" class="menu">
34
- <div class="box">
35
- <h1>🚀 SKY JUMP</h1>
36
- <input type="text" id="nameInput" placeholder="Enter Nickname" maxlength="12">
37
- <button id="playBtn">PLAY GAME</button>
38
- <div class="sub">⬅️ Arrows / Touch ➡️<br>Avoid Spikes & Monsters!</div>
39
- <div class="lb" id="menuLB"></div>
40
- </div>
41
- </div>
42
-
43
- <div id="overScreen" class="menu hidden">
44
- <div class="box">
45
- <h1 style="font-size:32px">GAME OVER</h1>
46
- <div class="score-display" id="finalScore">0</div>
47
- <div id="newRecord" style="color:#ffd700;font-weight:bold;margin-bottom:15px;height:20px"></div>
48
- <button id="retryBtn">TRY AGAIN</button>
49
- <div class="lb" id="overLB"></div>
50
- </div>
51
- </div>
52
-
53
- <script src="/socket.io/socket.io.js"></script>
54
- <script>
55
- // === CONFIG ===
56
- const W = 540, H = 960;
57
- const GRAVITY = 0.45;
58
- const JUMP_FORCE = -15.5;
59
- const SPRING_FORCE = -24;
60
- const PLAYER_W = 36, PLAYER_H = 44;
61
- const PLAT_W = 75, PLAT_H = 16;
62
- const SPEED = 7;
63
- const MAX_JUMP_HEIGHT = (JUMP_FORCE * JUMP_FORCE) / (2 * GRAVITY); // Approx ~266px
64
-
65
- // === AUDIO ===
66
- const AudioCtx = window.AudioContext || window.webkitAudioContext;
67
- let actx;
68
-
69
- function initAudio() {
70
- if (!actx) actx = new AudioCtx();
71
- if (actx.state === 'suspended') actx.resume();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  }
73
 
74
- const sfx = {
75
- jump: () => playTone(300, 600, 0.1, 'sine'),
76
- spring: () => playTone(200, 800, 0.3, 'square', 0.1),
77
- break: () => playTone(100, 50, 0.15, 'sawtooth'),
78
- die: () => { playTone(400, 100, 0.3, 'sawtooth'); setTimeout(()=>playTone(300,50,0.4,'sawtooth'), 200); },
79
- record: () => playTone(500, 1000, 0.5, 'sine')
80
- };
81
-
82
- function playTone(freqStart, freqEnd, dur, type, vol=0.05) {
83
- if (!actx) return;
84
- const osc = actx.createOscillator();
85
- const gain = actx.createGain();
86
- osc.type = type;
87
- osc.frequency.setValueAtTime(freqStart, actx.currentTime);
88
- osc.frequency.exponentialRampToValueAtTime(freqEnd, actx.currentTime + dur);
89
- gain.gain.setValueAtTime(vol, actx.currentTime);
90
- gain.gain.exponentialRampToValueAtTime(0.01, actx.currentTime + dur);
91
- osc.connect(gain);
92
- gain.connect(actx.destination);
93
- osc.start();
94
- osc.stop(actx.currentTime + dur);
95
- }
96
 
97
- // === SETUP ===
98
- const canvas = document.getElementById('C');
99
- const ctx = canvas.getContext('2d', { alpha: false });
100
- canvas.width = W;
101
- canvas.height = H;
102
 
103
- function resize() {
104
- const s = Math.min(innerWidth / W, innerHeight / H);
105
- canvas.style.width = W * s + 'px';
106
- canvas.style.height = H * s + 'px';
107
- }
108
- resize();
109
- onresize = resize;
110
-
111
- // === ELEMENTS ===
112
- const menuScreen = document.getElementById('menuScreen');
113
- const overScreen = document.getElementById('overScreen');
114
- const nameInput = document.getElementById('nameInput');
115
- const finalScoreEl = document.getElementById('finalScore');
116
- const newRecordEl = document.getElementById('newRecord');
117
- const menuLB = document.getElementById('menuLB');
118
- const overLB = document.getElementById('overLB');
119
-
120
- // === STATE ===
121
- let socket, myId;
122
  let state = 'menu';
123
- let leaderboard = [];
124
- let others = new Map(); // Use Map for better management
125
- let highScore = parseInt(localStorage.getItem('hs')) || 0;
126
- let savedName = localStorage.getItem('name') || '';
127
- nameInput.value = savedName;
128
-
129
- // Game Objects
130
- let player = {};
131
- let platforms = [];
132
- let monsters = [];
133
- let particles = [];
134
- let camY = 0;
135
- let seed = Date.now();
136
- let highestGenY = 0;
 
 
 
 
 
 
 
 
137
 
138
  // Input
139
- let keys = { left: false, right: false };
140
-
141
- // === GENERATION LOGIC (NO SOFT LOCKS) ===
142
- function rand(min, max) { return Math.random() * (max - min) + min; }
143
-
144
- function genPlatforms(reset = false) {
145
- if (reset) {
146
- platforms = [];
147
- monsters = [];
148
- // Start platform
149
- platforms.push({ x: W/2 - PLAT_W/2, y: H - 100, type: 0, w: PLAT_W });
150
- highestGenY = H - 100;
151
- }
152
 
153
- // Generate upwards as long as the highest platform is near the camera view limit
154
- // We generate way ahead to be safe
155
- while (highestGenY > camY - H * 1.5) {
156
- // Gap ensures playability.
157
- // Min gap 60 (don't overlap), Max gap is 85% of physics jump height to be safe.
158
- const gap = rand(70, MAX_JUMP_HEIGHT * 0.85);
159
- highestGenY -= gap;
160
-
161
- const typeRoll = Math.random();
162
- let type = 0; // 0: Normal, 1: Move, 2: Break, 3: Spring, 4: Spike
163
- let width = PLAT_W;
 
 
164
 
165
- if (Math.abs(highestGenY) < 1000) {
166
- // Easy start
167
- type = 0;
168
- } else {
169
- // Difficulty curve
170
- const diff = Math.min(Math.abs(highestGenY) / 50000, 1); // 0 to 1
171
-
172
- if (Math.random() < 0.1 + (diff * 0.2)) type = 1; // Moving
173
- else if (Math.random() < 0.1) type = 2; // Breakable
174
- else if (Math.random() < 0.08) type = 3; // Spring
175
- else if (Math.random() < 0.05 + (diff * 0.15)) type = 4; // Spike
176
-
177
- // Randomly spawn monster
178
- if (Math.random() < 0.02 + (diff * 0.05)) {
179
- monsters.push({
180
- x: rand(50, W - 50),
181
- y: highestGenY - rand(40, 100),
182
- vx: rand(1, 3) * (Math.random() < 0.5 ? 1 : -1),
183
- angle: 0
184
- });
185
- }
186
- }
187
 
188
- // Ensure X is reachable? With screen wrap, X is always reachable given enough fall time,
189
- // but we keep it somewhat centered to flow better.
190
- let x = rand(0, W - width);
 
191
 
192
- platforms.push({
193
- x, y: highestGenY, type, w: width,
194
- vx: type === 1 ? rand(2, 5) * (Math.random()<.5?1:-1) : 0,
195
- broken: false,
196
- springAnim: 0
197
- });
198
  }
 
199
 
200
- // cleanup old platforms to save memory
201
- platforms = platforms.filter(p => p.y < camY + H + 200);
202
- monsters = monsters.filter(m => m.y < camY + H + 200);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  }
204
 
205
- // === INIT PLAYER ===
206
- function initPlayer(saved) {
207
- player = {
208
- x: saved?.x ?? W/2 - PLAYER_W/2,
209
- y: saved?.y ?? H - 150,
210
- vx: 0,
211
- vy: 0,
212
- score: 0,
213
- maxY: saved?.y ?? H - 150,
214
- alive: true,
215
- color: saved?.color ?? `hsl(${Math.random()*360},80%,60%)`,
216
- facingRight: true
217
- };
218
- camY = Math.min(0, player.y - H * 0.5);
219
- genPlatforms(true);
220
  }
221
 
222
- function spawnParticles(x, y, color, n) {
223
- for (let i = 0; i < n; i++) {
224
  particles.push({
225
  x, y,
226
- vx: (Math.random() - 0.5) * 10,
227
- vy: (Math.random() - 0.5) * 10,
228
  life: 1,
229
- color,
230
- size: rand(3, 6)
231
  });
232
  }
233
  }
234
 
235
- // === GAME LOOP ===
236
- function update() {
237
- if (state !== 'playing' || !player.alive) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
- // Controls
240
- if (keys.left) { player.vx -= 0.8; player.facingRight = false; }
241
- else if (keys.right) { player.vx += 0.8; player.facingRight = true; }
242
- else player.vx *= 0.8;
 
 
 
 
 
 
 
243
 
244
- // Max Speed
245
- player.vx = Math.max(-SPEED, Math.min(SPEED, player.vx));
 
 
246
 
247
- // Physics
248
- player.vy += GRAVITY;
249
- player.x += player.vx;
250
- player.y += player.vy;
251
 
252
  // Wrap
253
- if (player.x < -PLAYER_W) player.x = W;
254
- if (player.x > W) player.x = -PLAYER_W;
255
 
256
- // Camera
257
- const targetCam = Math.min(0, player.y - H * 0.5);
258
- camY += (targetCam - camY) * 0.15; // Smooth cam
259
-
260
- // Collisions
261
- const pBottom = player.y + PLAYER_H;
262
-
263
- if (player.vy > 0) { // Falling only
264
- for (const p of platforms) {
265
- if (p.broken) continue;
266
- // Check bounds
267
- if (player.x + PLAYER_W * 0.7 > p.x && player.x + PLAYER_W * 0.3 < p.x + p.w &&
268
- pBottom >= p.y && pBottom <= p.y + p.w/2 + player.vy) { // relaxed y collision
269
-
270
- // SPIKE DEATH
271
- if (p.type === 4) {
272
- die();
273
- return;
274
- }
275
-
276
- // BREAKABLE
277
- if (p.type === 2) {
278
- p.broken = true;
279
- sfx.break();
280
- spawnParticles(p.x + p.w/2, p.y, '#a64', 8);
281
- continue;
282
  }
283
 
284
- // BOUNCE
285
- player.y = p.y - PLAYER_H;
286
- player.vy = JUMP_FORCE;
287
- sfx.jump();
288
 
289
- // SPRING
290
- if (p.type === 3) {
291
- player.vy = SPRING_FORCE;
292
- p.springAnim = 5;
293
- sfx.spring();
294
- spawnParticles(player.x+PLAYER_W/2, p.y, '#ffd700', 10);
 
 
295
  } else {
296
- spawnParticles(player.x+PLAYER_W/2, p.y, '#fff', 2);
 
297
  }
 
298
  }
299
  }
300
  }
301
-
302
- // Entities Update
303
- for (const p of platforms) {
304
- if (p.type === 1) {
305
- p.x += p.vx;
306
- if (p.x < 0 || p.x > W - p.w) p.vx *= -1;
307
- }
308
- if (p.springAnim > 0) p.springAnim--;
309
- }
310
-
311
- for (const m of monsters) {
312
- m.x += m.vx;
313
- if (m.x < 0 || m.x > W - 40) m.vx *= -1;
314
- m.angle += 0.1;
315
-
316
- // Monster Collision
317
- const dx = (player.x + PLAYER_W/2) - (m.x);
318
- const dy = (player.y + PLAYER_H/2) - (m.y);
319
- const dist = Math.sqrt(dx*dx + dy*dy);
320
- if (dist < 30) {
321
- die();
322
- return;
323
  }
324
  }
325
-
326
  // Score
327
- if (player.y < player.maxY) {
328
- player.maxY = player.y;
329
- player.score = Math.floor((H - 150 - player.y) / 10);
330
- // Endless generation trigger
331
- genPlatforms();
332
  }
333
 
334
- // Death by fall
335
- if (player.y > camY + H + 100) die();
 
 
336
 
337
- // Particle Logic
338
- for (let i = particles.length - 1; i >= 0; i--) {
339
- const p = particles[i];
340
- p.x += p.vx; p.y += p.vy;
341
- p.life -= 0.05;
342
- if (p.life <= 0) particles.splice(i, 1);
343
  }
 
 
344
 
345
- // Socket Update (20fps limit to save bandwidth)
346
- if (socket?.connected && Date.now() % 50 < 20) {
347
- socket.emit('update', {
348
- name: savedName,
349
- x: Math.round(player.x),
350
- y: Math.round(player.y),
351
- vx: player.vx,
352
- vy: player.vy,
353
- score: player.score,
354
- color: player.color,
355
- alive: player.alive,
356
- facingRight: player.facingRight
357
- });
358
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  }
360
 
361
- function die() {
362
- player.alive = false;
363
- sfx.die();
364
- spawnParticles(player.x, player.y, '#f00', 30);
365
- gameOver();
366
- }
367
-
368
- // === RENDER ===
369
- function draw() {
370
- // BG
371
- ctx.fillStyle = '#111';
372
- ctx.fillRect(0, 0, W, H);
373
-
374
- // Parallax Stars
375
- ctx.fillStyle = 'rgba(255,255,255,0.3)';
376
- for(let i=0; i<50; i++) {
377
- // pseudo random stars based on i
378
- let x = (i * 137) % W;
379
- let y = ((i * 311) - camY * (0.1 + (i%5)/10)) % H;
380
- if (y < 0) y += H;
381
  ctx.beginPath();
382
- ctx.arc(x, y, (i%3)+1, 0, Math.PI*2);
383
  ctx.fill();
384
  }
385
-
386
- // Platforms
387
- for (const p of platforms) {
388
- if (p.broken || p.y < camY - 50 || p.y > camY + H + 50) continue;
389
-
390
- const sy = p.y - camY;
391
-
392
- // Colors
393
- let col = '#0f8'; // normal
394
- if (p.type === 1) col = '#0af'; // move
395
- if (p.type === 2) col = '#a64'; // break
396
- if (p.type === 4) col = '#666'; // spike platform base
397
-
398
- // Shadow
399
- ctx.fillStyle = 'rgba(0,0,0,0.5)';
400
- ctx.fillRect(p.x + 4, sy + 4, p.w, PLAT_H);
401
 
402
- // Body
403
- ctx.fillStyle = col;
404
- ctx.fillRect(p.x, sy, p.w, PLAT_H);
405
 
406
- // Detail
407
- ctx.fillStyle = 'rgba(255,255,255,0.2)';
408
- ctx.fillRect(p.x, sy, p.w, 4);
409
-
410
- // Spike Visuals
411
- if (p.type === 4) {
412
- ctx.fillStyle = '#f33';
413
- const spikes = 4;
414
- const sw = p.w / spikes;
 
 
415
  ctx.beginPath();
416
- for(let i=0; i<spikes; i++) {
417
- ctx.moveTo(p.x + i*sw, sy);
418
- ctx.lineTo(p.x + i*sw + sw/2, sy - 12);
419
- ctx.lineTo(p.x + (i+1)*sw, sy);
 
 
 
 
420
  }
421
  ctx.fill();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  }
423
 
424
- // Spring Visuals
425
- if (p.type === 3) {
426
- ctx.fillStyle = '#fd0';
427
- ctx.fillRect(p.x + p.w/2 - 10, sy - 6 - p.springAnim, 20, 6 + p.springAnim);
428
- }
429
  }
430
-
431
- // Monsters
432
- for (const m of monsters) {
433
- const sy = m.y - camY;
434
- if (sy < -50 || sy > H + 50) continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435
 
436
- // Float anim
437
- const bob = Math.sin(Date.now() / 200) * 5;
 
 
438
 
439
- ctx.fillStyle = '#f24';
440
  ctx.beginPath();
441
- ctx.arc(m.x, sy + bob, 18, 0, Math.PI*2);
442
  ctx.fill();
443
- // Eye
 
 
 
 
 
 
 
 
 
 
 
 
444
  ctx.fillStyle = '#fff';
 
445
  ctx.beginPath();
446
- ctx.arc(m.x, sy + bob - 4, 8, 0, Math.PI*2);
 
447
  ctx.fill();
 
448
  ctx.fillStyle = '#000';
 
449
  ctx.beginPath();
450
- ctx.arc(m.x + Math.sin(m.angle)*3, sy + bob - 4, 3, 0, Math.PI*2);
451
- ctx.fill();
452
- // Wings
453
- ctx.strokeStyle = '#f24';
454
- ctx.lineWidth = 3;
455
- ctx.beginPath();
456
- ctx.moveTo(m.x-10, sy+bob); ctx.lineTo(m.x-28, sy+bob-10);
457
- ctx.moveTo(m.x+10, sy+bob); ctx.lineTo(m.x+28, sy+bob-10);
458
- ctx.stroke();
459
- }
460
-
461
- // Particles
462
- for (const p of particles) {
463
- ctx.globalAlpha = p.life;
464
- ctx.fillStyle = p.color;
465
- ctx.beginPath();
466
- ctx.arc(p.x, p.y - camY, p.size, 0, Math.PI*2);
467
  ctx.fill();
 
 
468
  }
469
- ctx.globalAlpha = 1;
470
 
471
- // Draw Others
472
- others.forEach(o => {
473
- // Interpolate visual position
474
- o.dx += (o.x - o.dx) * 0.2; // Smooth Lerp
475
- o.dy += (o.y - o.dy) * 0.2;
476
- if (o.dy - camY > -100 && o.dy - camY < H+100) {
477
- drawPlayer(o.dx, o.dy - camY, o.c, o.f, o.n, false);
478
- }
479
- });
480
 
481
- // Draw Self
482
- if (player.alive) {
483
- drawPlayer(player.x, player.y - camY, player.color, player.facingRight, 'YOU', true);
484
- }
485
 
486
- // HUD
487
- if (state === 'playing') {
488
- ctx.fillStyle = '#fff';
489
- ctx.font = '900 40px sans-serif';
490
- ctx.strokeStyle = '#000';
491
- ctx.lineWidth = 4;
492
- ctx.textAlign = 'left';
493
- ctx.strokeText(player.score, 20, 50);
494
- ctx.fillText(player.score, 20, 50);
495
- }
496
- }
497
-
498
- function drawPlayer(x, y, color, facingRight, name, isMe) {
499
- const cx = x + PLAYER_W/2;
500
- const cy = y + PLAYER_H/2;
501
 
502
- // Shadow
503
- ctx.fillStyle = 'rgba(0,0,0,0.3)';
504
  ctx.beginPath();
505
- ctx.ellipse(cx, y + PLAYER_H, 14, 4, 0, 0, Math.PI*2);
506
- ctx.fill();
507
-
508
- // Body
509
- ctx.fillStyle = color;
510
- ctx.beginPath();
511
- // Squish effect on landing could go here
512
- ctx.roundRect(x, y, PLAYER_W, PLAYER_H, 10);
513
  ctx.fill();
514
 
515
- // Goggles
516
- ctx.fillStyle = '#333';
517
- const faceX = facingRight ? 4 : -4;
 
518
  ctx.beginPath();
519
- ctx.roundRect(cx - 14 + faceX, cy - 10, 28, 12, 4);
 
520
  ctx.fill();
521
 
522
- ctx.fillStyle = '#0ff'; // Glass
523
  ctx.beginPath();
524
- ctx.rect(cx - 10 + faceX, cy - 8, 8, 8);
525
- ctx.rect(cx + 2 + faceX, cy - 8, 8, 8);
526
  ctx.fill();
527
 
528
- // Jetpack/Backpack
529
- ctx.fillStyle = '#555';
530
- ctx.fillRect(cx - 6 - faceX*2.5, y + 10, 12, 20);
531
-
532
  // Name
533
- if (!isMe) {
534
  ctx.fillStyle = '#fff';
535
- ctx.font = '10px sans-serif';
536
  ctx.textAlign = 'center';
537
- ctx.fillText(name, cx, y - 10);
 
 
538
  }
 
 
539
  }
540
 
541
- function gameOver() {
542
- state = 'gameover';
543
- finalScoreEl.innerText = player.score;
544
-
545
- if (player.score > highScore) {
546
- highScore = player.score;
547
- localStorage.setItem('hs', highScore);
548
- newRecordEl.innerText = "🏆 NEW HIGH SCORE!";
549
- sfx.record();
550
- } else {
551
- newRecordEl.innerText = `Best: ${highScore}`;
 
 
 
 
 
 
 
 
 
 
 
552
  }
553
-
554
- if (socket?.connected) {
555
- socket.emit('gameover', { name: savedName, score: player.score });
 
 
 
 
 
556
  }
557
-
558
- menuScreen.classList.add('hidden');
559
- overScreen.classList.remove('hidden');
560
  }
561
 
562
- // === CONTROLS ===
563
- function startGame() {
564
- initAudio();
565
- savedName = nameInput.value.trim() || 'Jumper';
566
- localStorage.setItem('name', savedName);
 
 
567
 
568
- initPlayer(null);
569
- particles = [];
570
- state = 'playing';
 
571
 
572
- menuScreen.classList.add('hidden');
573
- overScreen.classList.add('hidden');
 
 
 
 
 
 
 
 
574
  }
575
 
576
- window.addEventListener('keydown', e => {
577
- if(state !== 'playing') return;
578
- if(e.code === 'ArrowLeft' || e.code === 'KeyA') keys.left = true;
579
- if(e.code === 'ArrowRight' || e.code === 'KeyD') keys.right = true;
580
- });
581
- window.addEventListener('keyup', e => {
582
- if(e.code === 'ArrowLeft' || e.code === 'KeyA') keys.left = false;
583
- if(e.code === 'ArrowRight' || e.code === 'KeyD') keys.right = false;
584
- });
 
 
585
 
586
- // Touch
587
- const handleTouch = (e) => {
588
- if(state !== 'playing') return;
589
- e.preventDefault();
590
- const touch = e.touches[0];
591
- if (!touch) { keys.left = keys.right = false; return; }
592
-
593
- const rect = canvas.getBoundingClientRect();
594
- const x = touch.clientX - rect.left;
595
- const w = rect.width;
596
-
597
- if (x < w/2) { keys.left = true; keys.right = false; }
598
- else { keys.right = true; keys.left = false; }
599
- };
600
- canvas.addEventListener('touchstart', handleTouch, {passive: false});
601
- canvas.addEventListener('touchmove', handleTouch, {passive: false});
602
- canvas.addEventListener('touchend', () => { keys.left = keys.right = false; });
603
-
604
- document.getElementById('playBtn').onclick = startGame;
605
- document.getElementById('retryBtn').onclick = startGame;
606
-
607
- // === SOCKET ===
608
- socket = io();
609
- socket.on('init', d => {
610
- renderLB(d.leaderboard, menuLB);
611
- });
612
 
613
- socket.on('players', list => {
614
- const currentIds = new Set();
615
- list.forEach(p => {
616
- if (p.id === socket.id) return;
617
- currentIds.add(p.id);
 
618
 
619
- if (!others.has(p.id)) {
620
- // New player
621
- others.set(p.id, {
622
- dx: p.x, dy: p.y, // interpolation vars
623
- ...p
624
- });
625
  } else {
626
- // Update
627
- const o = others.get(p.id);
628
- o.x = p.x; o.y = p.y;
629
- o.n = p.n; o.c = p.c; o.f = p.f;
630
  }
631
- });
632
- // Remove disconnected
633
- for (const id of others.keys()) {
634
- if (!currentIds.has(id)) others.delete(id);
635
  }
636
  });
637
 
638
- socket.on('leaderboard', l => {
639
- renderLB(l, menuLB);
640
- renderLB(l, overLB);
 
 
 
 
 
 
 
 
 
 
 
 
 
641
  });
642
 
643
- function renderLB(list, el) {
644
- let h = '';
645
- list.forEach((e, i) => {
646
- h += `<div class="lb-row"><span>#${i+1} ${e.name}</span><span>${e.score}</span></div>`;
647
- });
648
- el.innerHTML = h;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649
  }
650
 
651
- // === LOOP ===
652
- function loop() {
653
- requestAnimationFrame(loop);
654
- update();
655
- draw();
 
 
 
 
 
 
 
 
 
 
 
656
  }
 
 
 
657
  loop();
658
- </script>
 
 
659
  </body>
660
  </html>
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
6
+ <title>Neon Jump</title>
7
+ <style>
8
+ *{margin:0;padding:0;box-sizing:border-box;touch-action:manipulation}
9
+ body{background:#05050a;display:flex;justify-content:center;align-items:center;min-height:100vh;font-family:system-ui,sans-serif;overflow:hidden}
10
+ #wrap{position:relative}
11
+ canvas{display:block;border:2px solid #0ff;box-shadow:0 0 30px #0ff4}
12
+ .ui{position:absolute;top:0;left:0;right:0;bottom:0;display:flex;flex-direction:column;justify-content:center;align-items:center;background:rgba(5,5,15,.9);color:#fff;text-align:center;transition:opacity .3s}
13
+ .ui.hidden{opacity:0;pointer-events:none}
14
+ h1{font-size:2.5em;color:#0ff;text-shadow:0 0 20px #0ff;margin-bottom:20px}
15
+ h1 span{color:#f0f}
16
+ input{padding:12px 20px;font-size:1.1em;background:rgba(0,255,255,.1);border:2px solid #0ff;color:#fff;border-radius:8px;outline:none;width:200px;margin-bottom:15px;text-align:center}
17
+ input::placeholder{color:#888}
18
+ button{padding:14px 40px;font-size:1.2em;background:linear-gradient(135deg,#0ff,#08f);border:none;color:#000;border-radius:8px;cursor:pointer;font-weight:700;transition:transform .2s,box-shadow .2s}
19
+ button:hover{transform:scale(1.05);box-shadow:0 0 25px #0ff}
20
+ .lb{margin-top:25px;background:rgba(0,0,0,.4);padding:15px 25px;border-radius:10px;border:1px solid #0ff4;max-height:200px;overflow-y:auto}
21
+ .lb h2{color:#0ff;font-size:1.1em;margin-bottom:10px}
22
+ .lb-row{display:flex;justify-content:space-between;padding:4px 0;color:#aaa;font-size:.9em}
23
+ .lb-row:nth-child(2){color:gold}
24
+ .lb-row:nth-child(3){color:#c0c0c0}
25
+ .lb-row:nth-child(4){color:#cd7f32}
26
+ #go h1{color:#f44;text-shadow:0 0 20px #f44}
27
+ #score{font-size:2em;color:#0ff;margin:15px 0}
28
+ #record{color:gold;font-size:1.3em;animation:pulse .4s infinite alternate}
29
+ @keyframes pulse{to{opacity:.5}}
30
+ .hidden-el{display:none!important}
31
+ </style>
32
  </head>
33
  <body>
34
+ <div id="wrap">
35
+ <canvas id="c"></canvas>
36
+ <div id="menu" class="ui">
37
+ <h1>🚀 NEON<span>JUMP</span></h1>
38
+ <input id="name" placeholder="Enter name" maxlength="12" autocomplete="off">
39
+ <button id="play">PLAY</button>
40
+ <div class="lb"><h2>🏆 LEADERBOARD</h2><div id="lb1"></div></div>
41
+ </div>
42
+ <div id="go" class="ui hidden">
43
+ <h1>GAME OVER</h1>
44
+ <div id="score">0</div>
45
+ <div id="record" class="hidden-el">🎉 NEW RECORD!</div>
46
+ <button id="retry">RETRY</button>
47
+ <div class="lb"><h2>🏆 LEADERBOARD</h2><div id="lb2"></div></div>
48
+ </div>
49
+ </div>
50
+ <script src="/socket.io/socket.io.js"></script>
51
+ <script>
52
+ (function(){
53
+ "use strict";
54
+
55
+ const $ = s => document.querySelector(s);
56
+ const canvas = $('#c'), ctx = canvas.getContext('2d');
57
+ const W = 400, H = 650;
58
+ canvas.width = W; canvas.height = H;
59
+
60
+ const socket = io();
61
+
62
+ // Audio
63
+ let audio;
64
+ function initAudio(){ if(!audio) audio = new (window.AudioContext||window.webkitAudioContext)(); }
65
+
66
+ function sound(type){
67
+ if(!audio) return;
68
+ const o = audio.createOscillator();
69
+ const g = audio.createGain();
70
+ o.connect(g); g.connect(audio.destination);
71
+ const t = audio.currentTime;
72
+
73
+ if(type==='jump'){
74
+ o.type='sine'; o.frequency.setValueAtTime(350,t);
75
+ o.frequency.exponentialRampToValueAtTime(700,t+.08);
76
+ g.gain.setValueAtTime(.25,t); g.gain.exponentialRampToValueAtTime(.01,t+.1);
77
+ o.start(t); o.stop(t+.1);
78
+ } else if(type==='spring'){
79
+ o.type='sine'; o.frequency.setValueAtTime(500,t);
80
+ o.frequency.exponentialRampToValueAtTime(1400,t+.15);
81
+ g.gain.setValueAtTime(.3,t); g.gain.exponentialRampToValueAtTime(.01,t+.2);
82
+ o.start(t); o.stop(t+.2);
83
+ } else if(type==='break'){
84
+ o.type='sawtooth'; o.frequency.setValueAtTime(180,t);
85
+ o.frequency.exponentialRampToValueAtTime(40,t+.15);
86
+ g.gain.setValueAtTime(.15,t); g.gain.exponentialRampToValueAtTime(.01,t+.15);
87
+ o.start(t); o.stop(t+.15);
88
+ } else if(type==='die'){
89
+ o.type='sawtooth'; o.frequency.setValueAtTime(250,t);
90
+ o.frequency.exponentialRampToValueAtTime(30,t+.4);
91
+ g.gain.setValueAtTime(.35,t); g.gain.exponentialRampToValueAtTime(.01,t+.4);
92
+ o.start(t); o.stop(t+.4);
93
+ } else if(type==='record'){
94
+ o.type='sine';
95
+ [523,659,784,1047].forEach((f,i)=>{ o.frequency.setValueAtTime(f,t+i*.12); });
96
+ g.gain.setValueAtTime(.25,t); g.gain.setValueAtTime(.25,t+.4);
97
+ g.gain.exponentialRampToValueAtTime(.01,t+.55);
98
+ o.start(t); o.stop(t+.55);
99
+ }
100
  }
101
 
102
+ // Constants
103
+ const GRAVITY = 0.45;
104
+ const JUMP = -13;
105
+ const SPRING = -21;
106
+ const SPEED = 6;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ // Types
109
+ const T_NORM=0, T_MOVE=1, T_BREAK=2, T_SPRING=3, T_SPIKE=4;
 
 
 
110
 
111
+ // State
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  let state = 'menu';
113
+ let pName = '', pColor = '#0ff';
114
+ let best = 0, lb = [];
115
+
116
+ // Player
117
+ let P = { x:0, y:0, vx:0, vy:0, w:28, h:28, score:0, maxY:0, alive:true };
118
+
119
+ // World
120
+ let camY = 0, targetCam = 0;
121
+ let platforms = [], enemies = [], particles = [];
122
+ let platId = 0, enemyId = 0;
123
+
124
+ // Stars
125
+ const stars = Array.from({length:80}, () => ({
126
+ x: Math.random()*W,
127
+ y: Math.random()*8000,
128
+ sz: Math.random()*2+.5,
129
+ sp: Math.random()*.4+.1,
130
+ br: Math.random()*.5+.3
131
+ }));
132
+
133
+ // Others
134
+ let others = {};
135
 
136
  // Input
137
+ let keys = {l:false, r:false};
138
+ let touch = {l:false, r:false};
139
+
140
+ // Platform creation
141
+ function mkPlat(x, y, type){
142
+ return {
143
+ id: platId++, x, y, w: type===T_SPRING?55:75, h:14,
144
+ type, broken:false, dir:1, sx:x, range: 60+Math.random()*40
145
+ };
146
+ }
 
 
 
147
 
148
+ function genPlat(baseY){
149
+ const diff = Math.min(1, Math.abs(baseY)/6000);
150
+ const x = 30 + Math.random()*(W-130);
151
+ let type = T_NORM;
152
+ const r = Math.random();
153
+
154
+ if(r < .12*diff) type = T_SPIKE;
155
+ else if(r < .12*diff+.14) type = T_BREAK;
156
+ else if(r < .12*diff+.28) type = T_MOVE;
157
+ else if(r < .12*diff+.38) type = T_SPRING;
158
+
159
+ return mkPlat(x, baseY, type);
160
+ }
161
 
162
+ function initPlats(){
163
+ platforms = [];
164
+ platId = 0;
165
+
166
+ // Start platform
167
+ platforms.push(mkPlat(W/2-37, H-60, T_NORM));
168
+
169
+ let y = H - 140;
170
+ let lastX = W/2;
171
+
172
+ while(y > -600){
173
+ // Ensure reachability: max horizontal jump ~120px, vertical ~200px
174
+ const gap = 70 + Math.random()*50;
175
+ y -= gap;
 
 
 
 
 
 
 
 
176
 
177
+ // Constrain x to be reachable
178
+ const maxDx = 130;
179
+ const minX = Math.max(20, lastX - maxDx);
180
+ const maxX = Math.min(W - 95, lastX + maxDx);
181
 
182
+ const p = genPlat(y);
183
+ p.x = minX + Math.random()*(maxX - minX);
184
+ lastX = p.x + p.w/2;
185
+
186
+ platforms.push(p);
 
187
  }
188
+ }
189
 
190
+ function genPlatsAbove(){
191
+ let minY = Math.min(...platforms.map(p=>p.y));
192
+ const target = camY - 700;
193
+
194
+ let lastPlat = platforms.find(p=>p.y===minY);
195
+ let lastX = lastPlat ? lastPlat.x + lastPlat.w/2 : W/2;
196
+
197
+ while(minY > target){
198
+ const gap = 70 + Math.random()*50;
199
+ minY -= gap;
200
+
201
+ const maxDx = 130;
202
+ const mnX = Math.max(20, lastX - maxDx);
203
+ const mxX = Math.min(W - 95, lastX + maxDx);
204
+
205
+ const p = genPlat(minY);
206
+ p.x = mnX + Math.random()*(mxX - mnX);
207
+ lastX = p.x + p.w/2;
208
+
209
+ platforms.push(p);
210
+ }
211
  }
212
 
213
+ function genEnemies(){
214
+ if(enemies.length > 8) return;
215
+ const minE = enemies.length ? Math.min(...enemies.map(e=>e.y)) : camY;
216
+ const diff = Math.min(1, Math.abs(camY)/4000);
217
+
218
+ if(minE > camY - 400 && Math.random() < .25*diff){
219
+ enemies.push({
220
+ id: enemyId++,
221
+ x: Math.random()*(W-40),
222
+ y: camY - 500 - Math.random()*300,
223
+ w: 38, h: 38,
224
+ vx: (1 + Math.random()*2) * (Math.random()<.5?1:-1),
225
+ ph: Math.random()*Math.PI*2
226
+ });
227
+ }
228
  }
229
 
230
+ function spawnParts(x, y, col, n){
231
+ for(let i=0; i<n; i++){
232
  particles.push({
233
  x, y,
234
+ vx: (Math.random()-.5)*10,
235
+ vy: (Math.random()-.5)*10 - 3,
236
  life: 1,
237
+ col
 
238
  });
239
  }
240
  }
241
 
242
+ function initGame(){
243
+ P = { x:W/2-14, y:H-100, vx:0, vy:0, w:28, h:28, score:0, maxY:0, alive:true };
244
+ camY = 0; targetCam = 0;
245
+ enemies = []; particles = [];
246
+ initPlats();
247
+ }
248
+
249
+ function die(){
250
+ if(!P.alive) return;
251
+ P.alive = false;
252
+ sound('die');
253
+ spawnParts(P.x+P.w/2, P.y+P.h/2, '#0ff', 20);
254
+
255
+ const isRecord = P.score > best;
256
+ if(isRecord){ best = P.score; sound('record'); }
257
+
258
+ socket.emit('s', {n:pName, s:P.score});
259
 
260
+ setTimeout(()=>{
261
+ state = 'over';
262
+ $('#go').classList.remove('hidden');
263
+ $('#score').textContent = P.score;
264
+ $('#record').classList.toggle('hidden-el', !isRecord);
265
+ renderLB('lb2');
266
+ }, 400);
267
+ }
268
+
269
+ function update(){
270
+ if(state !== 'play') return;
271
 
272
+ // Input
273
+ let dir = 0;
274
+ if(keys.l || touch.l) dir = -1;
275
+ if(keys.r || touch.r) dir = 1;
276
 
277
+ P.vx = dir * SPEED;
278
+ P.vy += GRAVITY;
279
+ P.x += P.vx;
280
+ P.y += P.vy;
281
 
282
  // Wrap
283
+ if(P.x < -P.w) P.x = W;
284
+ if(P.x > W) P.x = -P.w;
285
 
286
+ // Platform collision
287
+ if(P.vy > 0){
288
+ for(const pl of platforms){
289
+ if(pl.broken) continue;
290
+
291
+ const feet = P.y + P.h;
292
+ const wasAbove = feet - P.vy <= pl.y;
293
+
294
+ if(P.x+P.w > pl.x && P.x < pl.x+pl.w && feet >= pl.y && feet <= pl.y+pl.h+10 && wasAbove){
295
+ if(pl.type === T_SPIKE){
296
+ die(); return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  }
298
 
299
+ P.y = pl.y - P.h;
 
 
 
300
 
301
+ if(pl.type === T_SPRING){
302
+ P.vy = SPRING;
303
+ sound('spring');
304
+ } else if(pl.type === T_BREAK){
305
+ P.vy = JUMP;
306
+ pl.broken = true;
307
+ sound('break');
308
+ spawnParts(pl.x+pl.w/2, pl.y, '#964B00', 8);
309
  } else {
310
+ P.vy = JUMP;
311
+ sound('jump');
312
  }
313
+ break;
314
  }
315
  }
316
  }
317
+
318
+ // Enemy collision
319
+ for(const e of enemies){
320
+ if(P.x+P.w > e.x+5 && P.x < e.x+e.w-5 && P.y+P.h > e.y+5 && P.y < e.y+e.h-5){
321
+ die(); return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
  }
323
  }
324
+
325
  // Score
326
+ const worldY = -P.y;
327
+ if(worldY > P.maxY){
328
+ P.maxY = worldY;
329
+ P.score = Math.floor(worldY/10);
 
330
  }
331
 
332
+ // Fall death
333
+ if(P.y > camY + H + 150){
334
+ die(); return;
335
+ }
336
 
337
+ // Update platforms
338
+ for(const pl of platforms){
339
+ if(pl.type === T_MOVE && !pl.broken){
340
+ pl.x += pl.dir * 2.5;
341
+ if(pl.x > pl.sx + pl.range || pl.x < pl.sx - pl.range) pl.dir *= -1;
342
+ }
343
  }
344
+ platforms = platforms.filter(p => p.y < camY + H + 100);
345
+ genPlatsAbove();
346
 
347
+ // Update enemies
348
+ for(const e of enemies){
349
+ e.x += e.vx;
350
+ if(e.x < -e.w) e.x = W;
351
+ if(e.x > W) e.x = -e.w;
 
 
 
 
 
 
 
 
352
  }
353
+ enemies = enemies.filter(e => e.y < camY + H + 100 && e.y > camY - 800);
354
+ genEnemies();
355
+
356
+ // Particles
357
+ for(const p of particles){
358
+ p.x += p.vx;
359
+ p.y += p.vy;
360
+ p.vy += .25;
361
+ p.life -= .025;
362
+ }
363
+ particles = particles.filter(p => p.life > 0);
364
+
365
+ // Camera
366
+ if(P.y < camY + H*.35){
367
+ targetCam = P.y - H*.35;
368
+ }
369
+ camY += (targetCam - camY) * .12;
370
+
371
+ // Network
372
+ socket.emit('u', {
373
+ x: Math.round(P.x),
374
+ y: Math.round(P.y),
375
+ vx: Math.round(P.vx*10)/10,
376
+ vy: Math.round(P.vy*10)/10,
377
+ s: P.score,
378
+ a: P.alive
379
+ });
380
  }
381
 
382
+ function drawBG(){
383
+ const grd = ctx.createLinearGradient(0,0,0,H);
384
+ grd.addColorStop(0,'#08081a');
385
+ grd.addColorStop(1,'#180828');
386
+ ctx.fillStyle = grd;
387
+ ctx.fillRect(0,0,W,H);
388
+
389
+ // Stars parallax
390
+ for(const s of stars){
391
+ const sy = ((s.y - camY*s.sp) % (H+100)) - 50;
392
+ ctx.globalAlpha = s.br;
393
+ ctx.fillStyle = '#fff';
 
 
 
 
 
 
 
 
394
  ctx.beginPath();
395
+ ctx.arc(s.x, sy, s.sz, 0, Math.PI*2);
396
  ctx.fill();
397
  }
398
+ ctx.globalAlpha = 1;
399
+ }
400
+
401
+ function drawPlats(){
402
+ for(const pl of platforms){
403
+ if(pl.broken) continue;
404
+ const sy = pl.y - camY;
405
+ if(sy < -30 || sy > H+30) continue;
 
 
 
 
 
 
 
 
406
 
407
+ ctx.save();
 
 
408
 
409
+ if(pl.type === T_NORM){
410
+ ctx.fillStyle = '#0f0';
411
+ ctx.shadowColor = '#0f0';
412
+ ctx.shadowBlur = 12;
413
+ roundRect(pl.x, sy, pl.w, pl.h, 4);
414
+ } else if(pl.type === T_MOVE){
415
+ ctx.fillStyle = '#00bfff';
416
+ ctx.shadowColor = '#00bfff';
417
+ ctx.shadowBlur = 12;
418
+ roundRect(pl.x, sy, pl.w, pl.h, 4);
419
+ ctx.fillStyle = '#fff';
420
  ctx.beginPath();
421
+ if(pl.dir > 0){
422
+ ctx.moveTo(pl.x+pl.w-8, sy+pl.h/2);
423
+ ctx.lineTo(pl.x+pl.w-14, sy+4);
424
+ ctx.lineTo(pl.x+pl.w-14, sy+pl.h-4);
425
+ } else {
426
+ ctx.moveTo(pl.x+8, sy+pl.h/2);
427
+ ctx.lineTo(pl.x+14, sy+4);
428
+ ctx.lineTo(pl.x+14, sy+pl.h-4);
429
  }
430
  ctx.fill();
431
+ } else if(pl.type === T_BREAK){
432
+ ctx.fillStyle = '#8B4513';
433
+ ctx.shadowColor = '#8B4513';
434
+ ctx.shadowBlur = 6;
435
+ roundRect(pl.x, sy, pl.w, pl.h, 3);
436
+ ctx.strokeStyle = '#5a3010';
437
+ ctx.lineWidth = 1.5;
438
+ ctx.beginPath();
439
+ ctx.moveTo(pl.x+15, sy); ctx.lineTo(pl.x+25, sy+pl.h);
440
+ ctx.moveTo(pl.x+45, sy); ctx.lineTo(pl.x+35, sy+pl.h);
441
+ ctx.stroke();
442
+ } else if(pl.type === T_SPRING){
443
+ ctx.fillStyle = '#0f0';
444
+ roundRect(pl.x, sy, pl.w, pl.h, 3);
445
+ ctx.fillStyle = '#ff0';
446
+ ctx.shadowColor = '#ff0';
447
+ ctx.shadowBlur = 10;
448
+ const sw = 22, sh = 16;
449
+ ctx.fillRect(pl.x+pl.w/2-sw/2, sy-sh+2, sw, sh);
450
+ ctx.fillStyle = '#aa0';
451
+ for(let i=0; i<3; i++){
452
+ ctx.fillRect(pl.x+pl.w/2-sw/2+2, sy-sh+4+i*5, sw-4, 2);
453
+ }
454
+ } else if(pl.type === T_SPIKE){
455
+ ctx.fillStyle = '#333';
456
+ roundRect(pl.x, sy, pl.w, pl.h, 2);
457
+ ctx.fillStyle = '#f00';
458
+ ctx.shadowColor = '#f00';
459
+ ctx.shadowBlur = 8;
460
+ const spikes = 5;
461
+ const sw = pl.w/spikes;
462
+ for(let i=0; i<spikes; i++){
463
+ ctx.beginPath();
464
+ ctx.moveTo(pl.x + i*sw + sw/2, sy-10);
465
+ ctx.lineTo(pl.x + i*sw + 2, sy);
466
+ ctx.lineTo(pl.x + i*sw + sw - 2, sy);
467
+ ctx.closePath();
468
+ ctx.fill();
469
+ }
470
  }
471
 
472
+ ctx.restore();
 
 
 
 
473
  }
474
+ }
475
+
476
+ function roundRect(x,y,w,h,r){
477
+ ctx.beginPath();
478
+ ctx.moveTo(x+r,y);
479
+ ctx.lineTo(x+w-r,y);
480
+ ctx.quadraticCurveTo(x+w,y,x+w,y+r);
481
+ ctx.lineTo(x+w,y+h-r);
482
+ ctx.quadraticCurveTo(x+w,y+h,x+w-r,y+h);
483
+ ctx.lineTo(x+r,y+h);
484
+ ctx.quadraticCurveTo(x,y+h,x,y+h-r);
485
+ ctx.lineTo(x,y+r);
486
+ ctx.quadraticCurveTo(x,y,x+r,y);
487
+ ctx.closePath();
488
+ ctx.fill();
489
+ }
490
+
491
+ function drawEnemies(){
492
+ const t = Date.now()/1000;
493
+ for(const e of enemies){
494
+ const sy = e.y - camY + Math.sin(t*3 + e.ph)*5;
495
+ if(sy < -50 || sy > H+50) continue;
496
 
497
+ ctx.save();
498
+ ctx.fillStyle = '#f0f';
499
+ ctx.shadowColor = '#f0f';
500
+ ctx.shadowBlur = 15;
501
 
 
502
  ctx.beginPath();
503
+ ctx.arc(e.x+e.w/2, sy+e.h/2, e.w/2, 0, Math.PI*2);
504
  ctx.fill();
505
+
506
+ // Tentacles
507
+ ctx.strokeStyle = '#f0f';
508
+ ctx.lineWidth = 3;
509
+ for(let i=0; i<3; i++){
510
+ const tx = e.x + 8 + i*11;
511
+ ctx.beginPath();
512
+ ctx.moveTo(tx, sy+e.h-5);
513
+ ctx.quadraticCurveTo(tx + Math.sin(t*4+i)*5, sy+e.h+10, tx, sy+e.h+15);
514
+ ctx.stroke();
515
+ }
516
+
517
+ // Eyes
518
  ctx.fillStyle = '#fff';
519
+ ctx.shadowBlur = 0;
520
  ctx.beginPath();
521
+ ctx.arc(e.x+12, sy+14, 7, 0, Math.PI*2);
522
+ ctx.arc(e.x+26, sy+14, 7, 0, Math.PI*2);
523
  ctx.fill();
524
+
525
  ctx.fillStyle = '#000';
526
+ const ex = e.vx > 0 ? 2 : -2;
527
  ctx.beginPath();
528
+ ctx.arc(e.x+12+ex, sy+15, 3, 0, Math.PI*2);
529
+ ctx.arc(e.x+26+ex, sy+15, 3, 0, Math.PI*2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
530
  ctx.fill();
531
+
532
+ ctx.restore();
533
  }
534
+ }
535
 
536
+ function drawPlayer(p, local){
537
+ const sy = p.y - camY;
538
+ if(sy < -50 || sy > H+50) return;
 
 
 
 
 
 
539
 
540
+ const col = local ? '#0ff' : (p.c || '#f80');
 
 
 
541
 
542
+ ctx.save();
543
+ ctx.fillStyle = col;
544
+ ctx.shadowColor = col;
545
+ ctx.shadowBlur = 18;
 
 
 
 
 
 
 
 
 
 
 
546
 
 
 
547
  ctx.beginPath();
548
+ ctx.arc(p.x + p.w/2, sy + p.h/2, p.w/2, 0, Math.PI*2);
 
 
 
 
 
 
 
549
  ctx.fill();
550
 
551
+ // Face
552
+ ctx.fillStyle = '#fff';
553
+ ctx.shadowBlur = 0;
554
+ const ex = (p.vx||0) > 0 ? 4 : ((p.vx||0) < 0 ? -4 : 0);
555
  ctx.beginPath();
556
+ ctx.arc(p.x + p.w/2 - 4 + ex, sy + p.h/2 - 3, 4, 0, Math.PI*2);
557
+ ctx.arc(p.x + p.w/2 + 4 + ex, sy + p.h/2 - 3, 4, 0, Math.PI*2);
558
  ctx.fill();
559
 
560
+ ctx.fillStyle = '#000';
561
  ctx.beginPath();
562
+ ctx.arc(p.x + p.w/2 - 4 + ex, sy + p.h/2 - 2, 2, 0, Math.PI*2);
563
+ ctx.arc(p.x + p.w/2 + 4 + ex, sy + p.h/2 - 2, 2, 0, Math.PI*2);
564
  ctx.fill();
565
 
 
 
 
 
566
  // Name
567
+ if(!local && p.n){
568
  ctx.fillStyle = '#fff';
569
+ ctx.font = '11px system-ui';
570
  ctx.textAlign = 'center';
571
+ ctx.shadowColor = '#000';
572
+ ctx.shadowBlur = 4;
573
+ ctx.fillText(p.n, p.x+p.w/2, sy-8);
574
  }
575
+
576
+ ctx.restore();
577
  }
578
 
579
+ function drawOthers(){
580
+ const now = Date.now();
581
+ for(const id in others){
582
+ const o = others[id];
583
+ if(!o.a) continue;
584
+
585
+ // Client-side prediction with velocity
586
+ const dt = (now - o.t) / 1000;
587
+ const predX = o.x + o.vx * dt * 60;
588
+ const predY = o.y + o.vy * dt * 60;
589
+
590
+ // Lerp render position
591
+ o.rx = o.rx || o.x;
592
+ o.ry = o.ry || o.y;
593
+ o.rx += (predX - o.rx) * .15;
594
+ o.ry += (predY - o.ry) * .15;
595
+
596
+ // Wrap
597
+ if(o.rx < -30) o.rx = W;
598
+ if(o.rx > W) o.rx = -30;
599
+
600
+ drawPlayer({x:o.rx, y:o.ry, w:28, h:28, vx:o.vx, n:o.n, c:o.c}, false);
601
  }
602
+ }
603
+
604
+ function drawParticles(){
605
+ for(const p of particles){
606
+ const sy = p.y - camY;
607
+ ctx.globalAlpha = p.life;
608
+ ctx.fillStyle = p.col;
609
+ ctx.fillRect(p.x-3, sy-3, 6, 6);
610
  }
611
+ ctx.globalAlpha = 1;
 
 
612
  }
613
 
614
+ function drawUI(){
615
+ ctx.fillStyle = '#fff';
616
+ ctx.font = 'bold 22px system-ui';
617
+ ctx.textAlign = 'left';
618
+ ctx.shadowColor = '#0ff';
619
+ ctx.shadowBlur = 8;
620
+ ctx.fillText(P.score, 12, 30);
621
 
622
+ ctx.font = '13px system-ui';
623
+ ctx.fillStyle = '#888';
624
+ ctx.shadowBlur = 0;
625
+ ctx.fillText(`Best: ${best}`, 12, 48);
626
 
627
+ // Touch zones indicator
628
+ ctx.globalAlpha = .15;
629
+ ctx.strokeStyle = '#fff';
630
+ ctx.setLineDash([5,5]);
631
+ ctx.beginPath();
632
+ ctx.moveTo(W/2, H-100);
633
+ ctx.lineTo(W/2, H);
634
+ ctx.stroke();
635
+ ctx.setLineDash([]);
636
+ ctx.globalAlpha = 1;
637
  }
638
 
639
+ function draw(){
640
+ drawBG();
641
+ drawPlats();
642
+ drawEnemies();
643
+ drawParticles();
644
+ drawOthers();
645
+ if(P.alive && state==='play'){
646
+ drawPlayer(P, true);
647
+ drawUI();
648
+ }
649
+ }
650
 
651
+ // Loop
652
+ function loop(){
653
+ update();
654
+ draw();
655
+ requestAnimationFrame(loop);
656
+ }
657
+
658
+ // Socket
659
+ socket.on('l', d => { lb = d || []; renderLB('lb1'); renderLB('lb2'); });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
660
 
661
+ socket.on('p', list => {
662
+ const now = Date.now();
663
+ const ids = new Set();
664
+ for(const p of list){
665
+ ids.add(p.i);
666
+ if(p.i === socket.id) continue;
667
 
668
+ if(!others[p.i]){
669
+ others[p.i] = {...p, rx:p.x, ry:p.y, t:now};
 
 
 
 
670
  } else {
671
+ Object.assign(others[p.i], p);
672
+ others[p.i].t = now;
 
 
673
  }
674
+ }
675
+ for(const id in others){
676
+ if(!ids.has(id)) delete others[id];
 
677
  }
678
  });
679
 
680
+ function renderLB(id){
681
+ const el = $('#'+id);
682
+ if(!el) return;
683
+ el.innerHTML = lb.length ? lb.map((e,i) =>
684
+ `<div class="lb-row"><span>${i+1}. ${e.n}</span><span>${e.s}</span></div>`
685
+ ).join('') : '<div style="color:#555">No scores yet</div>';
686
+ }
687
+
688
+ // Controls
689
+ document.addEventListener('keydown', e => {
690
+ if(e.key==='ArrowLeft'||e.key==='a') keys.l = true;
691
+ if(e.key==='ArrowRight'||e.key==='d') keys.r = true;
692
+ });
693
+ document.addEventListener('keyup', e => {
694
+ if(e.key==='ArrowLeft'||e.key==='a') keys.l = false;
695
+ if(e.key==='ArrowRight'||e.key==='d') keys.r = false;
696
  });
697
 
698
+ canvas.addEventListener('touchstart', e => {
699
+ e.preventDefault(); initAudio();
700
+ handleTouch(e.touches);
701
+ }, {passive:false});
702
+ canvas.addEventListener('touchmove', e => {
703
+ e.preventDefault();
704
+ handleTouch(e.touches);
705
+ }, {passive:false});
706
+ canvas.addEventListener('touchend', e => {
707
+ e.preventDefault();
708
+ handleTouch(e.touches);
709
+ }, {passive:false});
710
+
711
+ function handleTouch(touches){
712
+ touch.l = false; touch.r = false;
713
+ const rect = canvas.getBoundingClientRect();
714
+ for(const t of touches){
715
+ const x = (t.clientX - rect.left) / rect.width * W;
716
+ if(x < W/2) touch.l = true;
717
+ else touch.r = true;
718
+ }
719
  }
720
 
721
+ // Start
722
+ $('#play').onclick = () => { initAudio(); startGame(); };
723
+ $('#retry').onclick = () => { $('#go').classList.add('hidden'); startGame(); };
724
+ $('#name').onkeypress = e => { if(e.key==='Enter'){ initAudio(); startGame(); } };
725
+
726
+ function startGame(){
727
+ pName = $('#name').value.trim() || 'Player';
728
+ pColor = `hsl(${Math.random()*360},80%,60%)`;
729
+
730
+ $('#menu').classList.add('hidden');
731
+ $('#go').classList.add('hidden');
732
+
733
+ initGame();
734
+ state = 'play';
735
+
736
+ socket.emit('j', {n:pName, c:pColor, x:P.x, y:P.y});
737
  }
738
+
739
+ renderLB('lb1');
740
+ draw();
741
  loop();
742
+
743
+ })();
744
+ </script>
745
  </body>
746
  </html>