Developing Retro Games with Python and Pyxel - Part 2: Basic Graphics, Coordinates, and Colors

In the previous article, we created our first Pyxel window, understood how the game loop works, and drew a simple green square on the screen. However, a game needs much more than a single rectangle to come to life.
In this second part of the series, we will explore Pyxel's visual universe. You will understand how the window is mathematically mapped, get to know the official 16-color palette, and learn how to draw geometric shapes and text inside the draw() function.
The Screen Coordinate System
Before drawing any element, we need to know how to tell Pyxel where to place it.
If you studied the Cartesian plane in school, you might remember that the point (0,0) is at the center and the Y axis grows upward. In computer graphics and 2D games, the logic is different:
- The origin
(0,0)is at the top-left corner of the window. - The
Xaxis grows to the right (from0towidth - 1). - The
Yaxis grows downward (from0toheight - 1).
For example, in a window configured with pyxel.init(9, 7):
- The top-leftmost pixel is
(0, 0). - The bottom-rightmost pixel is
(8, 6). - The exact center of the screen is at
(4, 3).
Tip: Whenever you want to position something relative to the window size without memorizing fixed numbers, you can use the
pyxel.widthandpyxel.heightproperties directly in your calculations.
The 16-Color Palette
One of Pyxel's trademarks is its fixed palette of 16 colors, numbered from 0 to 15. Instead of dealing with complex hexadecimal codes or RGB values, you simply pass the color number or use the library's own readable constants.
| Index | Pyxel Constant | Approximate Color |
|---|---|---|
0 | pyxel.COLOR_BLACK | Black |
1 | pyxel.COLOR_NAVY | Navy Blue |
2 | pyxel.COLOR_PURPLE | Purple |
3 | pyxel.COLOR_GREEN | Dark Green |
4 | pyxel.COLOR_BROWN | Brown |
5 | pyxel.COLOR_DARK_BLUE | Dark Slate Blue |
6 | pyxel.COLOR_LIGHT_BLUE | Light Blue |
7 | pyxel.COLOR_WHITE | White |
8 | pyxel.COLOR_RED | Red |
9 | pyxel.COLOR_ORANGE | Orange |
10 | pyxel.COLOR_YELLOW | Yellow |
11 | pyxel.COLOR_LIME | Lime Green |
12 | pyxel.COLOR_CYAN | Cyan |
13 | pyxel.COLOR_GRAY | Gray |
14 | pyxel.COLOR_PINK | Pink |
15 | pyxel.COLOR_PEACH | Peach / Beige |
Using col=8 or col=pyxel.COLOR_RED produces the exact same result, but constants make the code much easier to read months later.
Clearing the Screen (cls)
As we saw in Part 1, the draw() function runs 30 times per second. Pyxel does not automatically erase the previous frame before drawing the next one — it simply paints over it.
If an object moves across the screen and you don't clear the previous frame, it will leave an infinite trail (like a frozen window in old Windows). That's why the first line of your draw() function will almost always be:
pyxel.cls(pyxel.COLOR_WHITE) The cls (clear screen) function fills the entire window with the specified color, preparing a blank canvas for the new frame.
Geometric Primitives
Pyxel offers native functions for drawing basic shapes. Almost all of them follow the same parameter pattern: starting coordinates, dimensions, and the color (col).
1. Points (pset) and Lines (line)
-
pyxel.pset(x, y, col): lights up a single pixel at coordinate(x, y). -
pyxel.line(x1, y1, x2, y2, col): draws a straight line from point(x1, y1)to point(x2, y2).
2. Rectangles (rect and rectb)
-
pyxel.rect(x, y, w, h, col): draws a filled rectangle, starting at the top-left corner(x, y), with widthwand heighth. -
pyxel.rectb(x, y, w, h, col): draws only the border of the rectangle, leaving the inside transparent.
3. Circles (circ and circb)
-
pyxel.circ(x, y, r, col): draws a filled circle centered at(x, y)with radiusr. -
pyxel.circb(x, y, r, col): draws only the outline of a circle.
4. Triangles (tri and trib)
-
pyxel.tri(x1, y1, x2, y2, x3, y3, col): draws a filled triangle connecting three vertices. -
pyxel.trib(x1, y1, x2, y2, x3, y3, col): draws only the borders of the triangle.
Pay attention to drawing order (Layers): Pyxel draws in the order that the functions appear in the code. If you draw a blue circle and, on the next line, draw a red rectangle at the same position, the rectangle will appear on top of the circle.
Writing Text (text)
To display scores, lives, or messages on the screen, we use the pyxel.text() function:
pyxel.text(x=10, y=10, s="SCORE: 100", col=pyxel.COLOR_WHITE) Pyxel has a built-in pixelated font of 4x6 pixels per character (including spacing). This makes it very easy to calculate how to center text on the screen: since each letter takes up 4 pixels in width, a 10-letter word will have a total width of 40 pixels.
Putting It All Into Practice
Let's combine all these concepts to draw a complete little scene — a night landscape with stars, a moon, mountains, a house, and a scoreboard at the top.
Create a scene.py file and run it with pyxel run scene.py:
import pyxel pyxel.init(width=160, height=120, title="Geometric Landscape") def update(): if pyxel.btnp(pyxel.KEY_Q): pyxel.quit() def draw(): # 1. Night sky (clears the screen) pyxel.cls(col=pyxel.COLOR_NAVY) # 2. Stars (individual pixels) and Moon (circle) pyxel.pset(x=20, y=20, col=pyxel.COLOR_WHITE) pyxel.pset(x=40, y=30, col=pyxel.COLOR_YELLOW) pyxel.pset(x=60, y=20, col=pyxel.COLOR_WHITE) pyxel.circ(x=125, y=28, r=12, col=pyxel.COLOR_YELLOW) # 3. Mountains in the background (triangles) pyxel.tri(x1=10, y1=95, x2=50, y2=45, x3=90, y3=95, col=pyxel.COLOR_DARK_BLUE) pyxel.tri(x1=65, y1=95, x2=110, y2=50, x3=155, y3=95, col=pyxel.COLOR_PURPLE) # 4. Ground (rectangle at the bottom) pyxel.rect(x=0, y=95, w=160, h=25, col=pyxel.COLOR_GREEN) # 5. Simple house (rectangle + triangle + details) pyxel.rect(x=35, y=75, w=24, h=20, col=pyxel.COLOR_PEACH) # Wall pyxel.tri(x1=32, y1=75, x2=47, y2=62, x3=62, y3=75, col=pyxel.COLOR_RED) # Roof pyxel.rect(x=44, y=83, w=6, h=12, col=pyxel.COLOR_BROWN) # Door pyxel.rectb(x=38, y=80, w=5, h=5, col=pyxel.COLOR_LIGHT_BLUE) # Window # 6. Interface / Text pyxel.text(x=6, y=6, s="STAGE 1 - FOREST", col=pyxel.COLOR_WHITE) pyxel.line(x1=6, y1=14, x2=68, y2=14, col=pyxel.COLOR_CYAN) pyxel.run(update, draw) Final result:
Notice how the order of the lines in draw() automatically creates depth: the mountains are drawn before the ground and the house, placing them in the background without any extra effort.
Practical Challenge
Before moving on to the next article, try modifying the code above:
- Add a chimney to the house using
pyxel.rect(). - Try turning the full moon into a crescent moon by drawing a second, smaller circle of the same color as the sky (
COLOR_NAVY) slightly offset over the yellow circle.
Next Steps
Our scene now has color and shape, but it is still completely static — a digital painting.
In the next article of the series, we will bring the update() function to life: you will learn how to read keyboard and mouse input to move characters around the screen, handle border limits, and organize your code using Object-Oriented Programming.


