Pygame code generator

Pick components such as a keyboard-controlled player, sprites to collect, text, shapes, mouse input and timers. The generator combines them into one runnable program.

Updated
game.py

Components

Properties

Pick components; dependencies are handled for you.
On this page

How to use it#

  1. Tick the components you want. Dependencies are handled automatically: collecting sprites needs both a player and sprites.
  2. Adjust properties such as window size, colours, speed, sprite count and timer interval.
  3. Copy, Download .py, or Run online in Pygame Online.

Components#

ComponentWhat it generates
Keyboard-controlled playerA Player sprite moved with the arrow keys or WASD, kept inside the window.
Sprites in a groupA Collectible sprite class and a group of them at random positions.
Collect spritesspritecollide between the player and collectibles, removing them and counting a score.
TextA font and a label showing the title or the score.
Background shapesA draw_scenery() function with a ground strip, a sun and a line.
Mouse clicksRecords click positions, draws markers and prints the coordinates.
Timer eventA custom event posted with pygame.time.set_timer, which spawns new collectibles.
Browser-compatible loopThe asyncio structure needed for browsers; otherwise a classic main() function.

Default output#

The default settings generate a small game: move the yellow square to collect blue circles while the score counts up. It's deliberately simple so you can read all of it in a few minutes, and it uses the same structure as the tutorials: constants at the top, sprite classes, then a single loop with events, updates and drawing.

Every combination of components (256 of them) was generated and run with pygame-ce and pygame during testing, with scripted keyboard and mouse input.

Next steps#

↑ ↓ to moveEnter to openEsc to close