DEV Community

Cover image for Developing Retro Games with Python and Pyxel - Part 4: Sprites, Maps, and Audio with the Native Editor
Joelson Andrade
Joelson Andrade

Posted on Edited on AI-assisted

Developing Retro Games with Python and Pyxel - Part 4: Sprites, Maps, and Audio with the Native Editor

In the previous parts, we built a solid foundation: we learned how to open a window, draw geometric shapes, move objects, and organize our code with Object-Oriented Programming. However, drawing a complex character pixel by pixel using dozens of pyxel.rect() and pyxel.pset() calls in code would be impractical.

This is where one of Pyxel's most complete features shines: its built-in visual and audio editor. In this fourth part, you will learn how to create pixel art (sprites), build levels with tilemaps, compose retro sound effects, and load all of this into your Python game.

Getting to Know the Built-in Editor (pyxel edit)

Unlike other libraries where you need to install external programs to draw and edit audio, Pyxel already includes everything ready to use in the terminal.

To open the editor and create a resource file named resources.pyxres, run the following in your project directory:

pyxel edit resources.pyxres
Enter fullscreen mode Exit fullscreen mode

A retro window will open. In the top-left corner, there are four icons that switch between the internal tools:

  1. Image Editor: a grid canvas (divided into 3 banks of 256x256 pixels) where you draw your characters, items, and scenery blocks using the 16-color palette.
  2. Tilemap Editor: allows you to build entire levels by fitting pieces (tiles) of your drawings onto a giant grid, without having to position each block manually via code.
  3. Sound Editor: a chiptune synthesizer where you draw musical notes, choose waveforms (square, triangle, noise for explosions), and create effects like jumps, coins, and lasers.
  4. Music Editor: combines up to 4 simultaneous sound channels to compose the background soundtrack for your game.

When you finish editing, press Ctrl + S (or Cmd + S on Mac) to save the changes to the .pyxres file and ESC to close the editor.

For more details, check the Pyxel Editor Manual.

Loading Resources in Python (pyxel.load)

With the resources.pyxres file saved in the same folder as your Python script, just load it once inside the __init__ method of your App class, right after pyxel.init():

pyxel.init(width=160, height=120, title="Game with Sprites")
pyxel.load(filename="resources.pyxres")
Enter fullscreen mode Exit fullscreen mode

The pyxel.load() function loads all images, maps, and sounds into the engine's memory, ready to be used in update() and draw().

Drawing Sprites on the Screen (pyxel.blt)

In computer graphics, a sprite is a small 2D image (such as a spaceship, a coin, or a character animation frame) cropped from a larger sheet (sprite sheet).

To draw a sprite on the screen inside draw(), we use the pyxel.blt() (block transfer) function:

pyxel.blt(x, y, img, u, v, w, h, [colkey])
Enter fullscreen mode Exit fullscreen mode

Since it takes several parameters, it is worth understanding each one carefully:

  • x, y: where the sprite will be drawn in the game window.
  • img: which image bank to use (0, 1, or 2 in the editor).
  • u, v: the (x, y) coordinates where the crop starts inside the Image Editor.
  • w, h: the width and height of the crop (for example, 8 and 8, or 16 and 16).
  • colkey (optional): the color that should be treated as transparent. Since every sprite is a rectangle, if you drew your character on a black background (COLOR_BLACK or 0), passing colkey=0 ensures the black background doesn't cover the scenery behind it.

Flipping trick: If you pass a negative value for the width w (e.g., w=-16), Pyxel flips the sprite horizontally on the fly! This saves you from having to draw the same character twice (facing right and facing left) in the editor.

Drawing Scenery with Tilemaps (pyxel.bltm)

If you built an entire level in the Tilemap Editor, you don't need to call blt() hundreds of times. The pyxel.bltm() (block transfer map) function draws an entire region of the map at once:

pyxel.bltm(x, y, tm, u, v, w, h, [colkey])
Enter fullscreen mode Exit fullscreen mode
  • x, y: where the map starts being drawn on the screen (usually 0, 0).
  • tm: tilemap index (0 to 7).
  • u, v: starting read coordinates inside the tilemap editor (in pixels).
  • w, h: width and height of the map area to display (e.g., 160, 120 to fill the entire window).

Changing the values of u and v as the player walks allows you to create scrolling camera effects with just two variables.

Playing Sound Effects and Music (play and playm)

Audio provides the immediate feedback that makes any action satisfying. In Pyxel, we have two main functions for sound, called inside update():

1. Sound Effects (pyxel.play)

pyxel.play(ch, snd, [loop])
Enter fullscreen mode Exit fullscreen mode
  • ch: audio channel (0 to 3).
  • snd: number of the sound created in the Sound Editor (0 to 63).
  • loop: optional (True or False), in case you want the sound to repeat.

For example, inside an if pyxel.btnp(pyxel.KEY_SPACE):, you call pyxel.play(ch=0, snd=0) to trigger the jump or shoot sound on channel 0.

2. Background Music (pyxel.playm)

pyxel.playm(msc, [loop])
Enter fullscreen mode Exit fullscreen mode

msc: number of the music track created in the Music Editor (0 to 7).
loop=True: makes the soundtrack play continuously while the player is in the level.

To stop the sound at any time (such as on the Game Over screen), just use pyxel.stop().

Putting It All into Practice

Before jumping into the code, here is an essential tip from the Pyxel Editor manual: always keep the first position (0, 0) of the image bank empty (filled only with the background/transparency color).

This is because, in the Tilemap Editor, all cells of a new map point to tile (0, 0) by default (and the same applies to empty cells when importing .tmx files). If you draw your character right at (0, 0), the entire background of your map will appear filled with dozens of copies of your character as soon as you open the map editor. Therefore, let's position our character's sprite right next to it, at coordinate (8, 0).

The example below shows the complete structure of a game in a 128x128 window that loads a resources.pyxres file (with a background on tilemap 0, an 8x8 character at position (8, 0) of image 0 with a transparent black background, music 0, and sound effect 1 when pressing SPACE):

import pyxel

class App:
    def __init__(self):
        pyxel.init(width=128, height=128, title="Sprites and Audio")

        # Load images, maps, and sounds from the .pyxres file
        pyxel.load("resources.pyxres")

        self.x = 60
        self.y = 96
        self.direction = 1  # 1 = right, -1 = left

        # Start music 0 in a continuous loop
        pyxel.playm(msc=0, loop=True)

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

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

        # Horizontal movement, also updating the sprite's direction
        if pyxel.btn(pyxel.KEY_LEFT):
            self.x -= 2
            self.direction = -1
        if pyxel.btn(pyxel.KEY_RIGHT):
            self.x += 2
            self.direction = 1

        # Play sound effect 1 on channel 3 when SPACE is pressed
        if pyxel.btnp(pyxel.KEY_SPACE):
            pyxel.play(ch=3, snd=1)

        # Keep the 8-pixel sprite within the screen bounds
        self.x = pyxel.clamp(x=self.x, lower=0, upper=pyxel.width - 8)

    def draw(self):
        pyxel.cls(pyxel.COLOR_BLACK)

        # 1. Draw the background (Tilemap 0) covering the 128x128 window
        pyxel.bltm(x=0, y=0, tm=0, u=0, v=0, w=128, h=128)

        # 2. Draw the character (8x8) cropped from position u=8, v=0
        # colkey=0 makes the black color (index 0) transparent
        pyxel.blt(
            x=self.x,
            y=self.y,
            img=0,
            u=8,
            v=0,
            w=8 * self.direction,
            h=8,
            colkey=pyxel.COLOR_BLACK,
        )

if __name__ == "__main__":
    App()
Enter fullscreen mode Exit fullscreen mode

resources.pyxres file:

Final result:

Tip: If you haven't drawn your own .pyxres file yet, remember the pyxel copy_examples command from Part 1! It copies several ready-made .pyxres files that you can open with pyxel edit to study how sprites and sounds are built in practice.

Practical Challenge

Open the editor with pyxel edit resources.pyxres and try:

  1. Drawing two different animation frames for your character (for example, idle at (16, 0) and walking at (24, 0)).

Next Steps

Now you master all the fundamental pieces of the engine: lifecycle, vector drawing, controls, Object-Oriented Programming, sprites, maps, and audio.

In the fifth and final article of this series, we will bring all of this together to build a complete, playable game from scratch — with collision detection, a scoring system, start and Game Over screens, and packaging to share your game with anyone!

Top comments (0)