惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
博客园 - 三生石上(FineUI控件)
V
V2EX
有赞技术团队
有赞技术团队
V
Visual Studio Blog
小众软件
小众软件
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
量子位
T
Tailwind CSS Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Cisco Talos Blog
Cisco Talos Blog
I
Intezer
Project Zero
Project Zero
A
Arctic Wolf
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
L
Lohrmann on Cybersecurity
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tor Project blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Security @ Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 热门话题
G
GRAHAM CLULEY
Help Net Security
Help Net Security
N
News | PayPal Newsroom
W
WeLiveSecurity
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
60 FPS with 600 Snakes: How I Got the Browser Client to Survive a Room Full of Snakes
monkeymore s · 2026-05-11 · via DEV Community

The server took me one week. The client took another. Phaser 3 + React 19 + Vite. I know there are magic numbers, TODO comments, and obviously cleaner ways to write half of it — but it runs at a steady 60 FPS on a regular laptop, and that's good enough for a side project.

This post is about how I kept the browser from dying.


1. Why Mix React and Phaser?

I first tried building the UI entirely inside Phaser. Bad idea — Phaser's UI system is painful for anything beyond a score counter. I ended up with React 19 + Tailwind CSS for menus, HUD, and leaderboard, and Phaser 3 for the game canvas. They communicate through callbacks: React sends commands to Phaser, Phaser pushes score and leaderboard data back to React.

The upside is React's reconciliation never touches the game render loop because the Phaser canvas and React DOM live in separate worlds.

┌─────────────────────────────────────┐
│  React DOM (Tailwind CSS)           │
│  - Main menu, HUD, leaderboard      │
│  - Skin selector, i18n              │
└──────────────┬──────────────────────┘
               │ props / callbacks
┌──────────────▼──────────────────────┐
│  Phaser 3 Game                      │
│  - NetworkedGameScene               │
│  - WebSocket input / state sync     │
│  - 60 FPS fixed-timestep loop       │
└─────────────────────────────────────┘

Enter fullscreen mode Exit fullscreen mode

Client Architecture Flow

My targets were modest but tricky:

  • 60 FPS while rendering 100 visible snakes and ~200 food items.
  • Input latency < 50 ms — the mouse must feel instant.
  • Remote snakes must move smoothly, never snap to position.

2. Viewport Culling: The Foundation of Scale

The single most important optimization is don't render what the player cannot see. On a 16,000×16,000 map with 600 snakes, creating every Phaser game object every frame would obliterate the frame rate.

2.1 ViewportManager

class ViewportManager {
    viewRadius: number = 1000;   // strict visible radius
    bufferRadius: number = 100;  // hysteresis buffer

    isInViewport(x, y, centerX, centerY): boolean {
        const dist = sqrt((x - centerX)² + (y - centerY)²);
        return dist < (viewRadius + bufferRadius);
    }

    updateViewRadius() {
        const w = scene.cameras.main.width;
        const h = scene.cameras.main.height;
        // Cap at 1200 px to limit max render load regardless of monitor size
        this.viewRadius = min(sqrt(w² + h²) / 2 + 100, 1200);
    }
}

Enter fullscreen mode Exit fullscreen mode

The buffer radius prevents objects at the screen edge from flickering in and out as the camera moves. An object must move 100 px beyond the visible edge before its render objects are destroyed.

2.2 Lazy Render-Object Lifecycle

Each snake has two layers:

Layer Cost Lifetime
LocalSnake (data) Cheap (numbers + arrays) Permanent while snake exists in server state
Phaser objects (head, eyes, body circles, text) Expensive (GPU textures, draw calls) Only while snake is in viewport
// On server state update
for each snake in state:
    const inViewport = viewportManager.isInViewport(snake.x, snake.y, cameraCenter);

    if (inViewport && !snake.isRendered):
        snake.createRenderObjects();   // Allocate Phaser objects
    else if (!inViewport && snake.isRendered):
        // 3-second grace period before destroying (anti-flicker)
        if (now - snake.lastVisibleTime > 3000):
            snake.destroyRenderObjects(); // Free GPU resources

Enter fullscreen mode Exit fullscreen mode

Why destroy instead of hide? I originally used setVisible(false). Frame rate barely improved. Then I read the Phaser docs and realized GameObjects still participate in internal updates — tweens, transforms, culling checks — even when invisible. Only destroy() removes them from the scene graph entirely. That debugging session ate my entire afternoon.

Lazy Render Lifecycle Flow

2.3 Food Culling

Food uses the same pattern:

for each food in serverState:
    const inViewport = viewportManager.isInViewport(food.x, food.y, centerX, centerY);

    if (inViewport && !foods.has(foodId)):
        createFoodSprite(food);      // Spawn new circle
    else if (!inViewport && foods.has(foodId)):
        destroyFoodSprite(foodId);   // Immediate cleanup
    else if (inViewport && foods.has(foodId)):
        updateFoodPosition(food);    // Magnetic attraction movement

Enter fullscreen mode Exit fullscreen mode

With 1,600 total food items, typically only 80–150 are visible at once. That 90% reduction is the single biggest reason I can hold 60 FPS.


3. Client-Side Prediction & Server Reconciliation

My first attempt was pure server-authoritative: send input, wait for the server, then move. Even 50 ms of latency felt like dragging through mud. I switched to client prediction + server reconciliation.

3.1 Prediction (Local Player)

Every frame I simulate the local snake forward using the exact same physics constants as the server:

predict(inputAngle: number, isBoosting: boolean, deltaTime: number) {
    const speed = isBoosting ? BOOST_SPEED : BASE_SPEED;
    const dt = deltaTime / 1000;

    const vx = cos(inputAngle) * speed * dt;
    const vy = sin(inputAngle) * speed * dt;

    this.x += vx;
    this.y += vy;
    this.angle = inputAngle;

    this.updateTrail(this.x, this.y);   // Same trail algorithm as server
}

Enter fullscreen mode Exit fullscreen mode

This runs at 60 Hz inside the fixed-tick loop. The player feels zero input latency.

3.2 Interpolation (Remote Players)

Other players cannot be predicted — I don't know their inputs. Instead I interpolate toward the latest server position:

interpolate(deltaTime: number) {
    // Lerp toward server state at 20% per frame
    this.x = lerp(this.x, this.serverX, 0.2);
    this.y = lerp(this.y, this.serverY, 0.2);

    // Angle wrap-around for shortest path
    let angleDiff = this.serverAngle - this.angle;
    while (angleDiff > PI)  angleDiff -= 2*PI;
    while (angleDiff < -PI) angleDiff += 2*PI;
    this.angle += angleDiff * 0.2;

    this.updateTrail(this.x, this.y);
}

Enter fullscreen mode Exit fullscreen mode

0.2 is a value I tuned by hand.

  • Above 0.5: jerky, snaps to server state.
  • Below 0.1: sluggish, feels underwater.
  • 0.2 felt right after an evening of tweaking.

3.3 Reconciliation

Prediction inevitably drifts from the server. I reconcile in two layers:

reconcile() {
    const diffX = this.serverX - this.x;
    const diffY = this.serverY - this.y;

    // Small drift: invisible 5% nudge per frame
    if (abs(diffX) > 1 || abs(diffY) > 1) {
        this.x += diffX * 0.05;
        this.y += diffY * 0.05;
    }

    // Large drift (> 100 px): teleport and reset trail
    if (abs(diffX) > 100 || abs(diffY) > 100) {
        this.x = this.serverX;
        this.y = this.serverY;
        this.initTrail();   // Rebuild from new position
    }
}

Enter fullscreen mode Exit fullscreen mode

The 5% nudge absorbs normal tick drift. Players never notice it. The 100 px hard-sync handles respawns, lag spikes, or anything else where smooth correction would look weirder than a snap.

Prediction & Reconciliation Flow


4. Trail-Based Snake Rendering

I use the same trail-following algorithm on the client that I built for the server. My first attempt simulated every body segment with independent physics. Frame rate died instantly.

4.1 Algorithm

TRAIL_STEP = 2;           // pixels between trail points
SEGMENT_DISTANCE = 10;    // pixels between body segments

updateTrail(headX: number, headY: number) {
    const dist = distance(lastHeadPosition, {x: headX, y: headY});

    if (dist >= TRAIL_STEP) {
        const steps = floor(dist / TRAIL_STEP);
        for (let i = 1; i <= steps; i++) {
            const t = i / steps;
            trail.unshift({
                x: lerp(lastHeadPosition.x, headX, t),
                y: lerp(lastHeadPosition.y, headY, t)
            });
        }
        lastHeadPosition = {x: headX, y: headY};

        // Prevent unbounded growth
        const maxPoints = (bodySegments + 2) * (SEGMENT_DISTANCE / TRAIL_STEP);
        if (trail.length > maxPoints) trail.splice(maxPoints);
    }
}

getSegmentPositions(): Vector2[] {
    const pointsPerSegment = SEGMENT_DISTANCE / TRAIL_STEP;  // = 5
    const positions = [];
    for (let i = 0; i < bodySegments; i++) {
        const idx = floor((i + 1) * pointsPerSegment);
        if (trail[idx]) positions.push({...trail[idx]});
    }
    return positions;
}

Enter fullscreen mode Exit fullscreen mode

4.2 Why This Is Fast

  • One head simulation per snake, not N body segments.
  • Trail is a flat array — no linked lists, no complex structures.
  • Segment positions are pure lookups into the trail array, zero physics.
  • Memory is bounded: max 400 segments × 5 points/segment ≈ 2,000 vectors per snake.

Trail Update Flow


5. Render Optimizations in Detail

5.1 Body-Part Culling

Even when a snake is in viewport, not all its body segments need to be visible:

for (let i = 0; i < bodyParts.length; i++) {
    const partInViewport = viewportManager.isInViewportStrict(
        positions[i].x, positions[i].y, centerX, centerY
    );
    bodyParts[i].setVisible(partInViewport || this.isLocalPlayer);
}

Enter fullscreen mode Exit fullscreen mode

The local player's entire body is always rendered (so the tail is visible when wrapping around), but remote snakes hide off-screen segments.

5.2 Dynamic Body-Part Allocation

I do not pre-allocate all 400 possible segments. I grow and shrink dynamically:

// Grow
while (bodyParts.length < positions.length) {
    const part = scene.add.circle(0, 0, radius * 0.9, 0xffffff);
    bodyParts.push(part);
}

// Shrink
while (bodyParts.length > positions.length) {
    bodyParts.pop()?.destroy();
}

Enter fullscreen mode Exit fullscreen mode

This prevents allocating hundreds of invisible circles for every newly spawned snake.

5.3 Visual Tapering

To make long snakes look organic, the radius tapers toward the tail:

const taperFactor = 1 - (i / bodyParts.length) * 0.3;
bodyParts[i].setRadius(radius * 0.9 * taperFactor);

Enter fullscreen mode Exit fullscreen mode

Computationally trivial, but the visual improvement is huge without extra sprites or textures.

5.4 Skin Caching

Custom skins read from localStorage. I originally read it every frame and noticed occasional frame drops. Now I read once on skin change and cache:

loadCustomSkin() {
    if (skinId === 'custom') {
        const cfg = loadCustomSkinConfig();   // Reads localStorage once
        this.customHeadColor = hexToNumber(cfg.headColor);
        this.customBodyColors = cfg.bodyColors.map(hexToNumber);
    }
}

Enter fullscreen mode Exit fullscreen mode

localStorage is only accessed when the skin changes, never during the render loop.

5.5 Lightweight Food Effects

I skipped post-processing shaders — Phaser's postFX is expensive on low-end machines. I use simple tweens:

// Normal food: gentle pulse
tweens.add({
    targets: food,
    alpha: { from: 0.6, to: 1 },
    scale: { from: 0.9, to: 1.1 },
    duration: 800 + random() * 400,
    yoyo: true,
    repeat: -1,
    ease: 'Sine.easeInOut'
});

Enter fullscreen mode Exit fullscreen mode

No postFX, no particle emitters — just alpha and scale that the GPU handles for free.


6. Network Efficiency

6.1 Input Throttling

My first attempt sent a WebSocket message on every pointermove. With fast mouse movement that hit hundreds of messages per second and choked the connection. I switched to a fixed 60 Hz sample rate:

this.time.addEvent({
    delay: 1000 / 60,      // 16.67 ms
    callback: sendInput,
    loop: true
});

sendInput() {
    ws.send(JSON.stringify({
        type: 'input',
        angle: this.inputAngle,
        boosting: this.isBoosting
    }));
}

Enter fullscreen mode Exit fullscreen mode

Upload bandwidth is now hard-capped at ~60 small JSON messages/second, no matter how fast the mouse moves.

6.2 Client-Authoritative Food Eating

To hide network latency, the client predicts food collisions locally:

private checkLocalPlayerFoodCollision() {
    const eatRadius = snake.radius + 20;  // Looser than server

    foods.forEach((foodObj, foodId) => {
        const dist = distance(snake, foodObj);
        if (dist < eatRadius) {
            // 1. Apply locally immediately
            snake.score += foodValue;
            snake.bodySegments = calculateBodySegments(snake.score);
            snake.radius = calculateSnakeRadius(snake.bodySegments);

            // 2. Notify server (fire-and-forget)
            ws.send(JSON.stringify({ type: 'eat_food', foodId }));

            // 3. Remove from scene immediately
            foodObj.destroy();
            foods.delete(foodId);
        }
    });
}

Enter fullscreen mode Exit fullscreen mode

The player sees the score increase instantly. If the server rejects the eat (rare because of lenient validation), the next state sync silently corrects it. The 20 px extra radius absorbs client-server drift.

Client Network Flow


7. Minimap & Leaderboard

7.1 Minimap

The minimap is drawn with raw Graphics primitives, not sprites:

drawMinimap() {
    minimapGraphics.clear();
    // Background circle
    minimapGraphics.fillStyle(0x000000, 0.5);
    minimapGraphics.fillCircle(cx, cy, MINIMAP_SIZE / 2);

    const scale = MINIMAP_SIZE / WORLD_SIZE;

    // One draw call per snake: fillCircle
    snakes.forEach(snake => {
        if (snake.isAlive) {
            minimapGraphics.fillStyle(color, opacity);
            minimapGraphics.fillCircle(
                x + snake.x * scale,
                y + snake.y * scale,
                radius
            );
        }
    });
}

Enter fullscreen mode Exit fullscreen mode

Using Graphics avoids the scene-graph overhead of hundreds of individual game objects.

7.2 Leaderboard Throttling

Sorting all snakes by score is expensive. I run it only once per second, not every frame:

this.time.addEvent({
    delay: 1000,
    callback: updateLeaderboard,
    loop: true
});

Enter fullscreen mode Exit fullscreen mode


8. Fixed-Timestep Game Loop

The client uses the same fixed-timestep strategy as the server for deterministic physics:

update(time: number, delta: number) {
    this.elapsedTime += delta;
    while (this.elapsedTime >= this.fixedTimeStep) {
        this.elapsedTime -= this.fixedTimeStep;
        this.fixedTick(time, this.fixedTimeStep);
    }

    // Reconciliation and rendering happen every display frame
    if (this.currentPlayer) this.currentPlayer.reconcile();
    this.renderVisibleEntities();
}

Enter fullscreen mode Exit fullscreen mode

  • Logic (prediction, interpolation) runs at 60 Hz.
  • Rendering runs at the display refresh rate (60 Hz, 120 Hz, 144 Hz).
  • If the display drops a frame, logic catches up by running multiple fixed ticks in one render frame.

Main Update Loop Flow


9. Performance Budget

System Max Count Optimization Typical Active
Snakes (data) 600 Always stored 600
Snakes (rendered) 600 Viewport culling 20–60
Body segments 400 per snake Dynamic allocation + per-segment culling 2,000–5,000
Food (total) 1,600 Viewport culling 80–150
Food effects 1,600 Simple tweens only 80–150
Minimap dots 600 Graphics primitives 600 (one batch)
Input messages 60 Hz throttle 60/s
State broadcasts Server sends 20 Hz 20/s

10. One Week Later: Honest Thoughts

It works. Looking back, plenty I would rewrite:

  1. Viewport culling saved the project. Without it, 600 snakes × 400 segments = 240,000 Phaser circles. Chrome would die.
  2. Destroy, don't hide. I learned this the hard way. Phaser's scene graph is not free. destroy() gave me back 20 FPS compared to visible = false.
  3. Predict locally, reconcile gently. The 5% per-frame nudge is invisible to players but keeps server and client aligned. Without it, drift becomes visible after a few seconds.
  4. Mirror server physics exactly. BASE_SPEED, BOOST_SPEED, TRAIL_STEP — these numbers must match on both sides or reconciliation triggers hard-syncs constantly.
  5. Throttle the expensive stuff. Leaderboard sorting, invisible object cleanup, input sending — none of this needs to run every frame.

All in all, going from zero to a playable multiplayer game in two weeks (one for server, one for client) feels pretty good. The code is rough, but it ships. If you want to borrow ideas or fork it, go ahead.

Want to try it yourself? Head over to Hi! Snake and see how long you can survive against 500 bots.