Pygame
import pygame

pygame.init()
screen = pygame.display.set_mode((480, 320))
clock = pygame.time.Clock()
x, y = 225, 145

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise SystemExit

    keys = pygame.key.get_pressed()
    x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4
    y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 4

    screen.fill((24, 24, 32))
    pygame.draw.rect(screen, (90, 200, 120), (x, y, 30, 30))
    pygame.display.flip()
    clock.tick(60)

Press Run or Ctrl ↵ to execute main.py.
Type program input here while it runs.

Pygame

Pygame Online

Write a normal pygame program, with a game loop and clock.tick(), and play it in the Game tab beside the editor. pygame-ce, the maintained community edition of pygame, loads in your browser with Python the first time you press Run.

Click the game to give it the keyboard. The same file runs on your computer after pip install pygame-ce.

Try it above

How a game runs here

A browser tab can’t run while True: without pausing, or the page would freeze. Before each run your code is rewritten so the loop hands the page a turn at every clock.tick() (or display.flip() when there is no clock). Line numbers in errors stay the same, and you write the code exactly as you would on a desktop.

  • Keyboard, mouse and pygame.key.get_pressed() work. On a phone, the keyboard button in the Game tab’s header shows arrow keys, Space and any other keys your program uses.
  • print() goes to the output under the game, and input() asks there.
  • Draw with pygame.draw and pygame.font. Image files and sound are not available yet.
  • The Examples menu opens finished games (Snake, Pong, Breakout, a space shooter) in a new tab and starts them.

Stopping a game

Stop ends the game at its next frame. A loop that never reaches clock.tick(), display.flip() or display.update() can’t be paused, so after two seconds the game is closed instead and the output says why.

Questions

Do I need to install pygame?

No. pygame-ce loads in your browser together with Python the first time you press Run.

Will my game also run on my own computer?

Yes. It is ordinary pygame code. Download the file, run pip install pygame-ce, and start it with Python.

Does it work on a phone?

Yes. Touch screens get on-screen arrow keys and a Space key, plus any other keys your program uses.

Can I use images and sound?

Not yet. Draw with pygame.draw and pygame.font for now. Sound from pygame.mixer does not play on this page.