| name | pyxel-patterns |
| description | Pyxel retro game engine patterns - pixel art, game loops, sprite/tilemap, MML audio, resource management, and web deployment |
Pyxel Patterns
Patterns and best practices for building retro-style games with Pyxel — a Python game engine with deliberate retro constraints.
When to Use
- Building retro/pixel art games with Python
- Prototyping game mechanics quickly
- Creating browser-playable games (WASM export)
- Teaching game development fundamentals
Retro Constraints
Pyxel enforces retro limitations by design:
| Constraint | Limit |
|---|
| Colors | 16-color palette (customizable) |
| Screen | Default 256x256 (configurable) |
| Image banks | 3 banks (0-2), 256x256 each |
| Tilemaps | 8 maps, 256x256 tiles each |
| Sound channels | 4 simultaneous |
| Sound/Music | 64 user-definable sounds, 8 musics |
| Input | Keyboard + Mouse + Gamepad (up to 2) |
These constraints are features, not bugs. They force creative solutions and authentic retro aesthetics.
Game Loop Pattern
import pyxel
class App:
def __init__(self):
pyxel.init(160, 120, title="My Game")
pyxel.load("assets.pyxres")
self.player_x = 72
self.player_y = 56
self.score = 0
pyxel.run(self.update, self.draw)
def update(self):
"""Called every frame - handle input and game logic"""
if pyxel.btnp(pyxel.KEY_Q):
pyxel.quit()
if pyxel.btn(pyxel.KEY_LEFT):
self.player_x = max(self.player_x - 2, 0)
if pyxel.btn(pyxel.KEY_RIGHT):
self.player_x = min(self.player_x + 2, pyxel.width - 16)
def draw(self):
"""Called every frame - render everything"""
pyxel.cls(0)
pyxel.blt(self.player_x, self.player_y, 0, 0, 0, 16, 16, 0)
pyxel.text(5, 4, f"SCORE: {self.score}", 7)
App()
Input Handling
pyxel.btn(key)
pyxel.btnp(key)
pyxel.btnr(key)
pyxel.btnv(key)
pyxel.mouse_x
pyxel.mouse_y
pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT)
dx = pyxel.btn(pyxel.KEY_RIGHT) - pyxel.btn(pyxel.KEY_LEFT)
dy = pyxel.btn(pyxel.KEY_DOWN) - pyxel.btn(pyxel.KEY_UP)
Drawing API
pyxel.cls(col)
pyxel.pset(x, y, col)
pyxel.line(x1, y1, x2, y2, col)
pyxel.rect(x, y, w, h, col)
pyxel.rectb(x, y, w, h, col)
pyxel.circ(x, y, r, col)
pyxel.circb(x, y, r, col)
pyxel.blt(x, y, img, u, v, w, h, colkey)
pyxel.bltm(x, y, tm, u, v, w, h, colkey)
pyxel.text(x, y, string, col)
Sprite Animation
class AnimatedSprite:
def __init__(self, frames, speed=5):
self.frames = frames
self.speed = speed
self.frame_index = 0
self.counter = 0
def update(self):
self.counter += 1
if self.counter >= self.speed:
self.counter = 0
self.frame_index = (self.frame_index + 1) % len(self.frames)
def draw(self, x, y, img=0, colkey=0):
u, v, w, h = self.frames[self.frame_index]
pyxel.blt(x, y, img, u, v, w, h, colkey)
Collision Detection
def aabb_collision(x1, y1, w1, h1, x2, y2, w2, h2):
"""Axis-aligned bounding box collision"""
return (x1 < x2 + w2 and x1 + w1 > x2 and
y1 < y2 + h2 and y1 + h1 > y2)
def point_in_rect(px, py, rx, ry, rw, rh):
"""Point inside rectangle"""
return rx <= px < rx + rw and ry <= py < ry + rh
Sound & Music (MML)
pyxel.sounds[0].set(
"e2e2c2g1 g1g1c2e2 d2d2d2g2 e2e2e2c2",
"p",
"6",
"nnnf",
25
)
pyxel.play(ch, snd)
pyxel.playm(msc)
pyxel.stop(ch)
Resource Management
pyxel.load("assets.pyxres")
pyxel.images[0].load(0, 0, "sprite_sheet.png")
Scene Management
class SceneManager:
def __init__(self):
self.scenes = {}
self.current = None
def add(self, name, scene):
self.scenes[name] = scene
def switch(self, name):
self.current = self.scenes[name]
if hasattr(self.current, 'enter'):
self.current.enter()
def update(self):
if self.current:
self.current.update()
def draw(self):
if self.current:
self.current.draw()
class TitleScene:
def update(self):
if pyxel.btnp(pyxel.KEY_RETURN):
scene_mgr.switch("game")
def draw(self):
pyxel.cls(0)
pyxel.text(50, 50, "PRESS ENTER", pyxel.frame_count % )
Packaging & Distribution
pyxel package APP_DIR STARTUP_SCRIPT
pyxel app2exe APP.pyxapp
pyxel app2html APP.pyxapp
Performance Tips
- Keep
update() and draw() fast (target 30fps default)
- Use tilemaps for static backgrounds instead of drawing each tile
- Pool objects (bullets, particles) instead of creating/destroying
- Minimize Python object creation in the game loop
- Use
pyxel.frame_count for timing instead of tracking your own counter
- Pre-calculate values that don't change per frame
Common Game Patterns
Particle System
class Particle:
__slots__ = ['x', 'y', 'vx', 'vy', 'life', 'col']
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = pyxel.rndf(-1, 1)
self.vy = pyxel.rndf(-2, 0)
self.life = pyxel.rndi(10, 30)
self.col = pyxel.rndi(8, 10)
particles = []
Camera Scrolling
class Camera:
def __init__(self):
self.x = 0
self.y = 0
def follow(self, target_x, target_y):
self.x = target_x - pyxel.width // 2
self.y = target_y - pyxel.height // 2