This is the CDN root used by Pygbag.
If you like it, please star the repository!
Also, read the page on making your code compatible with browser game loop. You will probably have to change some of your code.
main.py
and put it in the root folder of your game.asyncio.sleep(0)
every iteration to give control back to the main thread.--template noctx.tmpl
to pygbag command line if using 3D/WebGL.main.py
.favicon.png
in your game’s root folder to make Pygbag use it as the web package’s favicon.Avoid raw formats like BMP for your image assets, they are too big for web use; use PNG/WEBP or JPG instead.
*-pygbag.*
that pattern is reserved for pygbag internal use (optimizing pass).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"
py --list
command./
instead of \
as a path separator (e.g. img/my_image.png
instead of img\my_image.png
). The path should still be valid on newer Windows versions.Install Certificates.command
in Applications/Python 3.XX
.sudo rmmod ftdi_sio
after plugging devices.There is actually nothing specific for projects except naming entry point main.py, 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
But there are templates to customize runtime startup for 2D and 3D, see templates
controlling pygbag packing and options from pygbag.ini
When importing non-stdlib 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",
# ]
# ///
import asyncio
by import pygbag.aio as asyncio
at top of main.py and run the program from the folder containing it.There’s number of command line options : read Pygbag’s project description for a more detailed overview.
Visit the wiki to get started!
Work in progress, pull requests welcomed. Feel free to propose links to games or tutorials. Please contribute!!!