Skip to content
harukaappscreatePublic

About

OPGFM is a starter kit for building small two-player turn-based online games with Pyxel.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

OPGFM — Online Play Game Flow Maker

OPGFM is a starter kit for building small two-player turn-based online games with Pyxel.

It uses PeerJS (WebRTC) so games run peer-to-peer in the browser — no game server to host, no accounts, no cost. You copy a small template into your game, implement three methods, and your Pyxel Web export becomes an online game.

Japanese README: README.ja.md

Quick Start: Play Tic-Tac-Toe Online in Two Tabs

git clone https://github.com/harukaappscreate/opgfm.git
cd opgfm
python3 -m venv .venv    # Windows: py -m venv .venv
source .venv/bin/activate    # Windows: .venv\Scripts\activate
pip install -r requirements.txt
python tools/build_tic_tac_toe_web.py
cd build/tic_tac_toe_web
python -m http.server 8000

Open http://localhost:8000/ in two browser tabs:

  1. First tab: press O (online lobby), then H to create a room. Note the room number.
  2. Second tab: press O, then J to join, and enter the room number.

When both tabs are connected, the match starts. Details: Tic-Tac-Toe online starter.

The sample's online lobby showing a room code, waiting for an opponent

The sample's online lobby: a room has been created serverlessly and is waiting for the second player.

How It Works

Online play runs in the browser (Pyxel Web / Pyodide). Python code talks to a thin JavaScript bridge, which talks to PeerJS. Synchronization is host-authoritative: the guest sends a pending_move, the host validates it and publishes the full game_state.

guest move
  -> pending_move
  -> host validates (apply_move)
  -> host publishes game_state (serialize)
  -> guest applies it (apply_state)

What OPGFM Expects From Your Game

Your game logic needs to expose three small methods:

serialize()      # return the current game state as a dict
apply_state()    # apply a received state dict
apply_move()     # validate and apply one player move

OPGFM provides the networking glue around that shape:

from multiplayer import MultiplayerManager, TurnBasedHostSync

self.multi = MultiplayerManager()
self.sync = TurnBasedHostSync(self.multi, self.logic)

self.sync.update()                        # every frame
self.sync.send_move({"index": index, "player": my_mark})   # on player input

See How OPGFM connects to an existing Pyxel game.

Starter Template

The canonical starter files live in template/:

template/
  multiplayer.py         # MultiplayerManager + TurnBasedHostSync
  peerjs_bridge.js       # thin PeerJS bridge for Pyxel Web exports
  index.html             # reference HTML wiring
  online_config.json     # starter settings
  INTEGRATION_STEPS.md   # step-by-step integration guide

PeerJS itself is not vendored. Browser builds reference a fixed CDN version:

<script src="https://cdn.jsdelivr.net/npm/peerjs@1.5.5/dist/peerjs.min.js"></script>

Local (Solo) Run

The normal Python run is for checking the title, lobby UI, board, and solo play. Online play only works in a browser build.

python examples/tic_tac_toe/main.py

OPGFM App (Flow Visualizer / Generator)

A Pyxel app that visualizes the online game flow (lobby, roles, turns, sync, disconnect, rematch) and copies the starter template into generated/:

python app/main.py

Scope

Designed for two-player, turn-based games (board games, card games). Not designed for realtime action synchronization or 3+ players.

Known limitation: the free public PeerJS signaling server can occasionally fail to connect. Reloading the page and retrying usually resolves it.

Showcase

Ultimate Tic-Tac-Toe is a published Pyxel game built on the same networking approach (host-authoritative sync over a PeerJS bridge). You can play it online in your browser right now.

Ultimate Tic-Tac-Toe online match in progress

Ultimate Tic-Tac-Toe: an online match with turn timer and emotes, running on this architecture.

Documentation

Credits

Created by haruka_apps.

License

MIT License. See LICENSE.

About

OPGFM is a starter kit for building small two-player turn-based online games with Pyxel.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages