Playertwo

Playertwo is a multiplayer game SDK built around host-authoritative state sync. Define your game logic once and Playertwo handles networking, state replication, and transport negotiation across local, P2P, and server-based topologies.

  • Host-authoritative — one peer owns canonical state; others send inputs
  • Engine-agnostic — plain JS game logic, render with Phaser, PixiJS, Canvas, or anything
  • Transport-swappable — local, Trystero P2P, WebSocket, Colyseus, or iframe bridge
  • Lobby system — built-in ready-up, min/max players, and phase management
  • Browser IDE — live editor with multiplayer simulation and state inspector
import { defineGame } from '@playertwo/core';

export const pong = defineGame({
  setup({ playerIds }) {
    return {
      ball: { x: 400, y: 300, vx: 4, vy: 3 },
      paddles: Object.fromEntries(
        playerIds.map((id, i) => [id, { y: 300, side: i === 0 ? 'left' : 'right' }])
      ),
      scores: Object.fromEntries(playerIds.map(id => [id, 0])),
    };
  },
  actions: {
    movePaddle: {
      apply(state, context, input) {
        const paddle = state.paddles[context.targetId];
        if (paddle) paddle.y = Math.max(0, Math.min(600, input.y));
      },
    },
  },
});