Publish your Pygame game on the web with pygbag
pygbag packages a pygame-ce game as a web page that runs in the browser through WebAssembly. Players click a link instead of downloading anything.
On this page
pygbag turns a folder containing a Pygame game into a set of web files. The browser downloads a WebAssembly build of Python and pygame-ce, then runs your unmodified Python code. It's the most common way to share Pygame games on sites such as itch.io. pygbag supports pygame-ce only, so develop with pygame-ce for the fewest surprises (comparison).
Step 1: Make the game loop async#
Browsers can't run an endless loop; your code must hand control back every frame. Put your loop in an async function, add await asyncio.sleep(0) at the end of each frame, and start it with asyncio.run():
import asyncio
import pygame
async def main():
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("navy")
pygame.display.flip()
clock.tick(60)
await asyncio.sleep(0) # keep it 0
asyncio.run(main())The program still runs normally on the desktop. pygbag's documentation asks you to keep the sleep at 0 and to avoid code after asyncio.run(main()), such as sys.exit(). All game projects on this site already follow this structure.
Step 2: Name the entry file main.py#
pygbag looks for main.py in your game folder. Put your assets in the same folder (or subfolders) and load them with relative paths.
Step 3: Build and test locally#
python -m pip install pygbag
pygbag my_game_folderpygbag builds the web version and starts a local test server at http://localhost:8000. Open it in a browser and click to start. The first load is slower while files are downloaded and cached. Build output goes to build/web inside your game folder.
Step 4: Upload to itch.io#
pygbag --archive my_game_folderThis creates build/web.zip. On itch.io, create a new project, set its kind to HTML, upload the zip, and tick "This file will be played in the browser".
Assets and audio#
- Prefer OGG for sounds and music in web builds.
- Keep file sizes small; everything is downloaded before or during play.
- pygbag has options for handling unsupported audio formats and for disabling its asset optimiser; run
pygbag --helpto see them for your version.
Limitations to plan for#
- No direct file system: saving uses browser storage, not files next to the game.
- No threads that block, no
input(), and no packages that need native code unless they're available for WebAssembly. - Audio starts after a click because of browser autoplay rules.
- Performance is lower than on the desktop. Pre-render static backgrounds and keep the number of draw calls reasonable (performance tips).
Try code in the browser first#
Pygame Online on this site uses the same idea, Python and pygame-ce compiled to WebAssembly, so it's a quick way to check whether a piece of code behaves in a browser before you set up pygbag.