Game loop generator

Choose a window size, frame rate, background and options, and get a clean, valid Pygame starter file to download or run online.

Updated
main.py

Window

Options

Change any option; the code updates instantly.
On this page

How to use it#

  1. Set the window width, height, FPS and background colour, and type a title.
  2. Tick the options you want. The code updates as you type.
  3. Copy, Download .py, or Run online to open the code in Pygame Online.

Your settings are remembered in this browser.

What the options do#

OptionAdds
Browser-compatible loopWraps the loop in async def main() with await asyncio.sleep(0), so the same file runs on the desktop and in browsers.
Delta timedt = clock.tick(FPS) / 1000 for frame-rate independent movement.
Quit with EscapeCloses the game when Escape is pressed.
Show FPSPuts the measured frame rate in the window title.
Arrow-key playerA square that moves smoothly with normalised diagonal speed.
Draw mouse clicksRecords and draws click positions and prints them.
Resizable windowpygame.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.

↑ ↓ to moveEnter to openEsc to close