Introduction to Retro Game Development with Pyxel

Introduction to Retro Game Development with Pyxel

Pyxel

Pyxel is a tribute to gaming consoles of the golden age, such as the Game Boy Color: featuring large pixels, a limited color palette, and minimalistic sound (supported on 4 channels).

This minimalist design allows you to build fluid games quickly. Thanks to its Rust backend, the game will run smoothly on almost any machine, even within a web browser (via WASM).

Setup

Pyxel has been tested across Linux, Mac, and Windows operating systems.

Development must be done from the terminal by typing commands.

Text Editor

You will need a suitable text editor, such as VSCode, Sublime Text, or any editor you prefer. You can download these from their official websites or through your OS app stores.

On Mac, you can install VSCode using Homebrew:

brew install vscode

On Ubuntu, use Snap:

sudo snap install code

Python

First, verify that you have a recent version of Python installed (>= 3.10):

python3 -V

If you do not have Python, or if you are running an outdated version, you must install a fresh version. On Mac, use brew install python. On Linux, use sudo apt install python3-dev python3-venv python3-pip. For Windows users, please download the installer from python.org.

Installation

Pyxel is not a standalone application; it is a Python framework supplemented by a media editor.

Therefore, to use it, you need Python installed, you must create a project environment with a virtual environment (venv), and then install the pyxel library using pip.

To keep things organized, we will place all code into a dedicated directory.

mkdir pyxel-demo
cd pyxel-demo

Next, create a virtual environment for Python. This ensures that project-specific libraries are installed locally, preventing pollution of your global Python environment.

python3 -m venv .pyxel-env

Shells like zsh and bash (the defaults on Linux and macOS) use the source command to activate the venv:

source .pyxel-env/bin/activate

For users of the fish shell, activation is handled like this:

. .pyxel-env/bin/activate.fish

On Windows, the activation command is different.

Now, we install the necessary libraries. First, we update pip to ensure smooth operations, and then we install pyxel.

pip install -U pip
pip install pyxel

Pyxel Examples

You can download the official examples provided by Pyxel:

pyxel copy_examples

Pyxel will download its example directory and create a folder named pyxel_examples.

The pyxel run command allows you to launch these demos:

pyxel run pyxel_examples/02_jump_game.py

The Jump Game demo uses the right/left arrow keys to accelerate or decelerate the character.

Pyxel Media Editor

Pyxel provides a comprehensive media editor for working with sprites, tilemaps, sound, and music.

Pyxel uses traditional (and some vintage) file formats.

  • Images: PNG format is used (transparency is supported). Game design traditionally places the origin point (0,0) in the top-left corner. You can drag and drop PNG, GIF, or JPG images into the editor window.
  • Fonts: Fonts are pixel-based and use the BDF format, which can be edited with FontForge.
  • Maps: Maps use the TMX format, which is editable using Tiled. (More formats are available on Github).
  • Res/Assets: The Pyxel editor compiles assets into pyxres archives—essentially ZIP files containing a large TOML file, with binaries listed as integers.
  • Sound: Sound is handled via a specific format.

To access the editor:

pyxel edit

Demo

Space ship

Demo party!

Once this initial test is successful, and after some initial experimentation to familiarize yourself with the tools, you should read the official documentation.

The code is fully typed but not commented, allowing the IDE to provide some helpful hints.

Media Editor

pyxel edit initiation.pyxres &

Design two spaceships, every 16 pixels wide and tall. This is your asset; feel free to design whatever you wish, as long as you adhere to the conventions. The first ship should face right, and the second should face left.

By convention, the first color in the palette represents transparency, corresponding to the value 0 in the code.

pyxel

If you encounter difficulties, you can download the sample file: initiation.pyxres.

Code

Create and edit a file named initiation.py, then adapt the example code below to your needs.

# Only for UNIX-like systems
touch initiation.py

A Static Spaceship

The application is encapsulated within a class.

When creating the class, you define the screen dimensions and the window title. Assets are loaded within the constructor.

The update method handles user input (keyboard events in this case).

The draw method is responsible for redrawing the entire screen every frame.

import pyxel


class App:
    def __init__(self):
        pyxel.init(160, 120, title="UFO static")  # width, height, title
        pyxel.load("initiation.pyxres")
        self.player_x = 72  # 🛸 X position
        self.player_y = 72  # 🛸 Y position
        self.ship = (0, 0, 16, 16, 0)  # x, y, width, height, transparent color

        pyxel.run(self.update, self.draw)

    def update(self):
        if pyxel.btnp(pyxel.KEY_Q):
            pyxel.quit()

    def draw(self):
        pyxel.cls(0)  # clear screen with color 0

        # Draw 🛸
        pyxel.blt(self.player_x, self.player_y, 0, *self.ship)


App()

Ask Pyxel to watch the source of your game.

Ask Pyxel to monitor your game source for changes.

pyxel watch . initiation.py

The spaceship will be displayed in the empty void of space, and you can exit the application using the Q key.

The Spaceship Is Shaken

With a little trigonometry—a simple cosine function is used to make the spaceship gently oscillate.

import pyxel


class App:
    def __init__(self):
        pyxel.init(160, 120, title="UFO shaken")  # width, height, title
        pyxel.load("initiation.pyxres")
        self.player_x = 72  # 🛸 X position
        self.player_y = 72  # 🛸 Y position
        self.ship = (0, 0, 16, 16, 0)  # x, y, width, height, transparent color

        pyxel.run(self.update, self.draw)

    def update(self):
        if pyxel.btnp(pyxel.KEY_Q):
            pyxel.quit()

        # 🛸 oscillates slowly 1/16 per tick starting at y = 72
        self.player_y = pyxel.cos(pyxel.frame_count * 360 / 16) + 72

    def draw(self):
        pyxel.cls(0)  # clear screen with color 0

App()

The Spaceship Can Face Left or Right

The spaceship can face left or right by using two different sprites.

Using the arrow keys, you can move the character forward or backward by a step of two pixels, ensuring you stay within the screen boundaries.

Here it is; you have a wiggling spaceship, and you can drive it with your keyboard.

Sources of Spaceship are hosted on Github.

What’s Next?

Sound design, a starry background, laser attacks, or antagonists?

blogroll

social