This tutorial provides a working example of using Pygame with WebAssembly to run and render a Gobblet environment locally in the browser.
The script adapts the Greedy Agent tutorial code to run in the web, displaying games between two greedy agents.
Features such as loading trained models or using interactive environments may be added in the future.
It can be deployed to github-pages via a workflow (see .github/workflows/pygbag.yml
).
WebAssembly (WASM) allows many languages to run locally in the browser with near-native performance. The python package Pygbag allows us to run python scripts in the web using WebAssembly--including Pygame, which is used for rendering PettingZoo environments.
For more information and example scripts, see pygame-web
Packages may not all work natively using Pygbag, so any issues can be reported to pkg-porting-wasm
Pygbag works via a virtual environment, and all dependencies must be downloaded locally as wheels and unzipped. install.sh
script does this and moves the unzipped packages into /modules
which needs to be added to the system path before importing. Pygbag only allows a single file main.py
to be used, and requires a specific structure using asyncio
(see pygame-web)
- (Optional) Create a virtual environment:
conda create -n wasm python=3.10
- (Optional) Activate the virtual environment:
conda activate wasm
- Install requirements:
pip install -r requirements.txt
- Run
bash install.sh
in order to download and unzip dependencies to be used in the WASM virtual machine. - Change directory to parent of root project directory:
cd ../../..
- Run pygbag on this directory: pygbag PygameWASM `
- Open your browser and go to
http://localhost:8000/
(for debugging info: http://localhost:8000/#debug)
Dependencies and versions can be changed in install.sh
, which can be re-run (although it is safest to delete /modules
and ensure there are not multiple versions of the same package)
-
Python 3.11 is recommended for WASM, but is currently not supported by PettingZoo, which may lead to errors.
-
Calling certain sub-modules (e.g.,
package.submodule
) without explicitly importing them can also cause errors. For example, inconnect_four.py
, line 281observation = np.array(pygame.surfarray.pixels3d(self.screen))
raises an error.- This can be solved by explicitly importing the sub-module: add the line
import pygame.surfarray
tomain.py
(no need to modify the original file it was called in)
- This can be solved by explicitly importing the sub-module: add the line