Developing Retro Games with Python and Pyxel - Part 1: Getting Started and the Game Lifecycle

If you have always wanted to create your own games, but felt that professional tools require configuring dozens of screens before you even write a single line of code, Pyxel is the ideal starting point.
In this series of articles, we will explore game development in Python from scratch. In this first part, you will set up your environment, understand what happens behind the scenes when a game runs, and put your first window on the screen.
What is Pyxel
Pyxel is a free and open-source game engine for Python, created by Takashi Kitao in 2018.
A game engine is a tool that takes care of the heavy lifting — opening the window, drawing on the screen, capturing keystrokes, playing sounds, and keeping the game running — so you can focus solely on the rules and fun of your game.
Pyxel's main differentiator is its inspiration from classic 8-bit era consoles. By design, it imposes well-defined creative limitations: a fixed 16-color palette and 4 sound channels. These constraints eliminate excessive complexity and allow you to focus on programming logic.
Installation and basic commands
Pyxel requires Python 3.11 or higher and can be installed directly via the pip package manager:
pip install pyxel The installation makes the pyxel command-line tool available in your terminal. To check all available options, simply run pyxel without arguments:
pyxel Pyxel 3.0.1, a retro game engine for Python usage: pyxel run PYTHON_SCRIPT_FILE(.py) pyxel watch WATCH_DIR PYTHON_SCRIPT_FILE(.py) pyxel play PYXEL_APP_FILE(.pyxapp) pyxel edit [PYXEL_RESOURCE_FILE(.pyxres)] pyxel package APP_DIR STARTUP_SCRIPT_FILE(.py) pyxel app2exe PYXEL_APP_FILE(.pyxapp) pyxel app2html PYXEL_APP_FILE(.pyxapp) pyxel copy_examples The most important commands to get started are:
-
pyxel copy_examples: creates a folder with several ready-made games for you to test and study the code. -
pyxel run app.py: runs your Pyxel script (you can also run it normally withpython app.py). -
pyxel watch . app.py: monitors the current directory (.) and automatically restarts the game every time you save a change to the code — ideal for speeding up testing.
Your first program (graphical "Hello, World!")
Before diving into theory, let's put something on the screen. Create a file named app.py and add the following code:
import pyxel # 1. Initializes the window with width 160 and height 120 pixels pyxel.init(width=160, height=120, title="My First Game") def update(): # Closes the game if the Q key is pressed if pyxel.btnp(pyxel.KEY_Q): pyxel.quit() def draw(): # Clears the screen by painting everything black pyxel.cls(pyxel.COLOR_BLACK) # Draws a lime-green rectangle at position (10, 10) pyxel.rect(x=10, y=10, w=20, h=20, col=pyxel.COLOR_LIME) # 2. Starts the main game loop pyxel.run(update, draw) Run the file in the terminal with pyxel run app.py. A dark window with a lime-green square will appear. Press Q (or ESC) to close it.
How does this code work?
In Pyxel, you don't write the loop that keeps the window open — the library itself manages that. The flow follows four simple steps:
- Import: we bring the
pyxelmodule into the script. - Initialization (
pyxel.init): defines the window dimensions (in pixels) and the title. - Loop definition (
updateanddraw): we create two functions that will be called automatically by the engine dozens of times per second. - Execution (
pyxel.run): we pass our functions to Pyxel so it can start the engine.
There is a convention in Pyxel that keeps your code organized and easy to debug:
- The
updatefunction only updates data (reads keys, calculates positions, checks collisions), and never draws. - The
drawfunction only draws on the screen based on the current data, and never alters the game state.
How games work
Now that you have seen the window working, it is much easier to understand the architecture that supports any digital game.
The Game Loop
A traditional program reads data, executes instructions from top to bottom, and terminates. A game, on the other hand, needs to run continuously while interacting with the player. This infinite cycle is called the game loop and repeats three steps endlessly:
- Reads input: checks if any key or mouse button was pressed.
- Updates state: modifies variables such as character position, score, or lives.
- Draws the screen: renders the new scene based on the updated data.
In our previous example, the flow follows this exact logic:
flowchart TD A([Start]) --> B[pyxel.init] B --> C[pyxel.run] C --> D["update()"] D --> E{Q key pressed?} E -->|No| F["draw()"] F --> D E -->|Yes| G([End]) The set of all variables that describe the current moment of the match (coordinates, points, energy) is called the game state.
Pixels, Resolution, and Frames (FPS)
Everything you see in the Pyxel window is composed of pixels — the smallest colored dots that form a digital image.
The resolution defines the size of this grid in width and height. In our code, we set 160x120, a purposely low resolution to highlight the pixel art style (Pyxel automatically scales up the window on your screen so it is comfortable to see).
Finally, the sensation of movement in games is an illusion created by rapidly displaying static images in sequence. Each individual image is called a frame, and the speed at which they pass is measured in FPS (Frames Per Second).
By default, Pyxel runs at 30 FPS. This means that every second, it calls update() 30 times and draw() 30 times. (If you need more fluidity, you can change this value by passing the fps=60 parameter inside pyxel.init).
Next steps
Now that you know how to set up the environment, run a project, and understand how the game loop coordinates state and rendering at 30 FPS, we are ready to go beyond a simple static square.
In the next article in the series, we will explore the draw() function in detail: how Pyxel's Cartesian plane works, how to use the 16-color palette, and how to draw geometric shapes and text on the screen.





