On this page
How to use the editor#
- Pick an example from the list, or start typing in the editor.
- Press Run (or Ctrl + Enter). The first run downloads the Python runtime, from the pinned Pyodide distribution, which your browser then caches.
- Click the game area so it receives keyboard input, then play.
- Press Stop to end the program, or Restart to start a fresh Python runtime.
Download saves your code as a .py file that runs unchanged on your computer with pip install pygame-ce or pip install pygame. Projects keeps named copies in your browser's local storage, and Open loads a .py file from your computer without uploading it.
What runs in the browser#
The editor uses Pyodide, a build of CPython for WebAssembly, together with its build of pygame-ce, the Pygame Community Edition. The versions are shown under the editor. Most of the pygame API works the same way it does on the desktop: pygame.display, pygame.draw, pygame.Rect, sprites and groups, fonts, keyboard and mouse events, pygame.Vector2 and pygame.transform.
Some things are different because a web page is not a desktop computer:
| Works on the desktop | In the browser editor |
|---|---|
| Loading images and sounds from files | No access to your files. Draw graphics in code or run the program locally. |
input() in the terminal | Not available; use pygame key events. |
tkinter, turtle, subprocess, threads that block | Not available. |
pip install any package | Only the standard library, pygame-ce and packages built for Pyodide. |
Sound with pygame.mixer | May be silent, depending on the browser's audio rules. |
Why game loops need await asyncio.sleep(0)#
A desktop Pygame program usually loops forever with while running: and clock.tick(60). In a browser, the page can only redraw when your code hands control back. The examples on this site are written so they run in both places:
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) # lets the browser draw this frame
pygame.quit()
asyncio.run(main())If you paste a classic desktop program without asyncio, the editor tries to adapt it automatically: it finds loops that call pygame.display.flip(), pygame.display.update(), clock.tick() or pygame.event.get() and adds the yield for you, then prints a note in the console. Your code in the editor is not changed. If a program still freezes, add the async/await lines yourself as shown above.
A loop that runs without ever reaching await (for example while True: pass) cannot be interrupted from inside the page. After three seconds without a response the editor offers Force stop, which throws away the runtime and starts a new one.
Privacy and security#
Your code runs only in your browser. The runtime sits in a sandboxed frame that cannot read this site's cookies, storage or page content, and the editor never sends your code to the server. Saved projects stay in your browser's local storage until you delete them or clear your browsing data.
Troubleshooting#
- Nothing happens when I press keys. Click the game area first so it has keyboard focus.
- "Could not download pygame-ce". The package comes from a content delivery network on first use. Check your connection or any content blocker, then press Run again.
- The game runs slowly. WebAssembly is fast, but drawing thousands of shapes every frame costs more in a browser than on the desktop. Draw static scenery once onto a
Surfaceand blit it each frame. - My file uses images. Upload support for assets isn't available; run the program locally after following the installation guide.