Python Pygame playground: make games in your browser
This free Pygame playground runs real pygame code online. Write a game in the editor above, press Run, and play it right beside your code. There is nothing to install: no Python, no pip install pygame, no account.
It is a quick way to learn game programming, follow a pygame tutorial on a school or work computer, try an idea before building a bigger game, or show a friend what you made with a link.
How to run pygame online
- Type or paste your pygame code into the editor, or choose one of the examples.
- Press Run (Ctrl+Enter, Cmd+Enter on a Mac). The first run downloads pygame, which takes a few seconds once.
- Click the game to give it the keyboard, and play. Anything you
print()shows under the game. - Press Stop to end the game, change your code, and run it again.
Your first pygame program
Every pygame game has the same shape: set up a window, then loop forever, handling events, updating the game and drawing a frame. This one moves a ball across the screen.
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
clock = pygame.time.Clock()
x = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
x = (x + 3) % 400
screen.fill((20, 20, 40))
pygame.draw.circle(screen, (255, 200, 0), (x, 150), 30)
pygame.display.flip()
clock.tick(60)
pygame.quit()pygame.display.set_mode((400, 300))opens the game window, 400 pixels wide and 300 high.pygame.event.get()collects key presses, clicks and the quit event.pygame.display.flip()shows the frame you just drew, andclock.tick(60)keeps the game at 60 frames a second.
Example games to start from
- Bouncing ballsAnimation and mouse clicks
- Coin collectorMove with the arrow keys, collect coins
- SnakeThe classic game, with score and restart
- PaintDraw with the mouse, change colour with keys
- Starfield3D star animation, mouse controls speed
What works
- Real pygame
pygame-ce, the actively maintained edition of pygame, running on Python 3.12 in your browser.
- Your normal game loop
Write while running: loops with clock.tick(60) as you would on a computer. The playground keeps the page responsive for you.
- Keyboard and mouse
Key presses, pygame.key.get_pressed(), mouse clicks and movement all work. Click the game first to give it the keyboard.
- Images, sounds and fonts
Add your own PNG, JPG, WAV or OGG files from the ⋯ menu and load them by name, e.g. pygame.image.load("player.png").
- Errors with line numbers
If your game crashes, the traceback appears under the game, pointing to the line in your code.
- Share and download
Copy a link that opens your game for anyone, or download main.py to run it on your own computer.
Differences from pygame on a computer
- The game draws inside the page instead of in its own window, scaled to fit the space.
- There is no window close button: the Stop button sends your game the same pygame.QUIT event.
- Timing is a little less exact than on a computer, so use clock.tick() rather than counting on exact frame times.
- Files your game needs must be added from the ⋯ menu first; your computer’s own files cannot be read.
Frequently asked questions
What is a Pygame playground?
It is a place to write and run pygame programs without installing Python or pygame. You type your game on the left, press Run, and it plays on the right, in your browser.
Is it free?
Yes. It is free, with no sign-up and no limits.
Which version of pygame does it use?
pygame-ce 2.4 (pygame Community Edition) on Python 3.12, through Pyodide. pygame-ce is a drop-in replacement for pygame: you still write import pygame.
Do I need to change my pygame code?
Usually not. Ordinary pygame programs with a while loop, pygame.event.get(), pygame.display.flip() and clock.tick() run as they are. Code written for pygbag, with async def main() and await asyncio.sleep(0), works too.
How do I quit the game?
Press Stop. Your game receives a pygame.QUIT event, as when a window is closed on a computer, so it can end normally. A browser tab has no window close button, so Stop does that job.
Why don't the arrow keys work?
The game only gets key presses while it has focus, so typing in the editor never moves your player. Click the game, then use the keyboard.
Can I use my own images and sounds?
Yes. Open the ⋯ menu next to Run, choose Add images or sounds, then load them by file name in your code. Added files stay until you reload the page.
Does it work on a phone?
Yes, the games run on phones and tablets, and a tap works like a mouse click. Games that need a keyboard are best played on a computer.
Can I install other packages?
This page is made for pygame. For numpy, pandas and other packages, use the Python playground, which has a pip install box.
New to Python?
Games are a fun way to learn, but they use loops, functions and lists everywhere. If those are new to you, start with the free Python course, practise with the Python challenges, and use the Python playground for programs that print text.