On this page
How to use it#
- Set the window width, height, FPS and background colour, and type a title.
- Tick the options you want. The code updates as you type.
- Copy, Download .py, or Run online to open the code in Pygame Online.
Your settings are remembered in this browser.
What the options do#
| Option | Adds |
|---|---|
| Browser-compatible loop | Wraps the loop in async def main() with await asyncio.sleep(0), so the same file runs on the desktop and in browsers. |
| Delta time | dt = clock.tick(FPS) / 1000 for frame-rate independent movement. |
| Quit with Escape | Closes the game when Escape is pressed. |
| Show FPS | Puts the measured frame rate in the window title. |
| Arrow-key player | A square that moves smoothly with normalised diagonal speed. |
| Draw mouse clicks | Records and draws click positions and prints them. |
| Resizable window | pygame.RESIZABLE and handling of VIDEORESIZE. |
Default template#
With the default settings the generator produces this program:
Python
"""My Pygame Game - generated with the Pygame.net game loop generator."""
import asyncio
import pygame
WIDTH, HEIGHT = 800, 600
FPS = 60
BACKGROUND = (30, 42, 74)
async def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Pygame Game")
clock = pygame.time.Clock()
running = True
while running:
dt = clock.tick(FPS) / 1000 # seconds since the last frame
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
# Update game state here
# Draw everything
screen.fill(BACKGROUND)
pygame.display.flip()
await asyncio.sleep(0) # lets the browser draw the frame (harmless on desktop)
pygame.quit()
asyncio.run(main())Every combination of options was generated and executed with pygame-ce and pygame as part of this site's tests. The game loop tutorial explains each part.