pygame wasm

pygame-web.github.io

This is the CDN root used by Pygbag (Source code/Old runtimes) and the site of its wiki.

Pygbag does not track usage at all, not even for statistical purposes. If you like it, please star the repository!

Check out some demos before you start!

Important points

Read Pygbag’s project description for a more detailed overview. A full packaging guide can be found here.

Also, read the page on making your code compatible with browser game loop. You will probably have to change some of your code.

All operating systems

Before packaging, adapt your code this way if you still want WAV/MP3 format on desktop:

if sys.platform == "emscripten":
    snd = pygame.mixer.Sound("sound.ogg")
else:
    snd = pygame.mixer.Sound("sound.wav") # or .WAV, .mp3, .MP3, etc.

if you have heightmaps in your assets use --no_opt to prevent png recompression.

if you want to keep pixelated look whatever the device screen size is use:

import sys, platform
if sys.platform == "emscripten":
    platform.window.canvas.style.imageRendering = "pixelated"

Windows

MacOS

Linux

Template

There is actually none, because Python-WASM is just a web-friendly version of CPython REPL with some added facilities. Most desktop code will run (and continue to run) with only a few changes.

Basic structure of a game (available here):

test
├── img
│   ├── pygc.bmp
│   ├── pygc.png
│   └── tiger.svg
├── main.py
└── sfx
    └── beep.ogg

Useful .gitignore additions:

*.wav
*.mp3
*.pyc
*.egg-info
*-pygbag.???
/build
/dist

Coding

Adding modules

When importing complex packages (for example, numpy or matplotlib), you must put their import statements at top of main.py. You should also add a metadata header as specified by PEP 723, for example:

# /// script
# dependencies = [
#  "six",
#  "bs4",
#  "markdown-it-py",
#  "pygments",
#  "rich",
#  "mdurl",
#  "textual",
# ]
# ///

If using pygame-zero (mostly untested), put #!pgzrun near the top of main.py. (2nd line is perfect if the file already has a shebang)

Debugging / Desktop Simulator

Running

Publishing

Demos

Demos on itch.io

Demos on Github Pages

These are provided for testing purposes only, and might not always work since they use development versions of Pygbag.

Heavy CPU load, not for low-end devices

Light CPU load

Source code for these games can be found here. You can tag your Github repositories with [pygame-wasm].

Script demos

The code is read-only, so you should right-click then open in a new window.

Technology

Early demos from above talk, may not work as intended :)

Python WebAssembly at PyCon FR 2023 (in French): Pour quoi, pour qui et comment

Status

Support

Connect

Thanks for reading and supporting pygame-ce and pygbag. These tools could not have existed without your support.

Work in progress, pull requests welcomed. Feel free to propose links to games or tutorials. Please contribute!!!

Edit this page