Start the Pyxel application with update and draw functions
pyxel.run(self.update, self.draw)
pyxel.quit()
Exit the Pyxel application
if pyxel.btnp(pyxel.KEY_Q): pyxel.quit()
pyxel.cls(col)
Clear the screen with specified color
pyxel.cls(0) # Clear with black
pyxel.frame_count
Get the number of frames since the application started
animation_frame = pyxel.frame_count % 30
49.2 Input Handling
Command
Description
Example
pyxel.btn(key)
Check if a button is being held down
if pyxel.btn(pyxel.KEY_RIGHT): player_x += 2
pyxel.btnp(key)
Check if a button was just pressed
if pyxel.btnp(pyxel.KEY_SPACE): fire_weapon()
pyxel.mouse_x
Get current mouse X position
cursor_x = pyxel.mouse_x
pyxel.mouse_y
Get current mouse Y position
cursor_y = pyxel.mouse_y
pyxel.mouse(visible)
Show or hide the mouse cursor
pyxel.mouse(True) # Show mouse cursor
49.3 Constants for Keys
Constant
Description
pyxel.KEY_UP
Up arrow key
pyxel.KEY_DOWN
Down arrow key
pyxel.KEY_LEFT
Left arrow key
pyxel.KEY_RIGHT
Right arrow key
pyxel.KEY_SPACE
Space key
pyxel.KEY_RETURN
Enter/Return key
pyxel.KEY_Q
Q key (commonly used to quit)
pyxel.MOUSE_BUTTON_LEFT
Left mouse button
pyxel.MOUSE_BUTTON_RIGHT
Right mouse button
49.4 Drawing Primitives
Command
Description
Example
pyxel.pset(x, y, col)
Draw a single pixel
pyxel.pset(10, 10, 7) # White pixel
pyxel.line(x1, y1, x2, y2, col)
Draw a line
pyxel.line(10, 10, 50, 50, 8) # Red line
pyxel.rect(x, y, w, h, col)
Draw a filled rectangle
pyxel.rect(10, 10, 40, 30, 3) # Dark green rect
pyxel.rectb(x, y, w, h, col)
Draw a rectangle outline
pyxel.rectb(10, 10, 40, 30, 7) # White outline
pyxel.circ(x, y, r, col)
Draw a filled circle
pyxel.circ(40, 40, 10, 12) # Light blue circle
pyxel.circb(x, y, r, col)
Draw a circle outline
pyxel.circb(40, 40, 10, 7) # White circle outline
pyxel.tri(x1, y1, x2, y2, x3, y3, col)
Draw a filled triangle
pyxel.tri(30, 10, 50, 50, 10, 50, 11) # Green triangle
pyxel.trib(x1, y1, x2, y2, x3, y3, col)
Draw a triangle outline
pyxel.trib(30, 10, 50, 50, 10, 50, 7) # White outline
pyxel.text(x, y, text, col)
Draw text
pyxel.text(10, 10, "Hello Pyxel!", 7) # White text
49.5 Sprite and Image Handling
Command
Description
Example
pyxel.blt(x, y, img, u, v, w, h, [colkey])
Draw a sprite from the image bank
pyxel.blt(10, 10, 0, 0, 0, 16, 16, 0) # 16x16 sprite with black transparent
pyxel.load(filename)
Load resources from a .pyxres file
pyxel.load("game_resources.pyxres")
pyxel.images[bank].load(x, y, filename)
Load an image into the image bank
pyxel.images[0].load(0, 0, "character.png")
pyxel.images[bank].pset(x, y, col)
Set a pixel color in the image bank
pyxel.images[0].pset(5, 5, 8) # Red pixel in bank 0
49.6 Image Bank Structure
pyxel.images[0] # First image bank page (0)
pyxel.images[1] # Second image bank page (1)
pyxel.images[2] # Third image bank page (2)
Each image bank is a 256x256 pixel area where you can store sprites and other graphical assets.
49.7 Colors
Pyxel has a fixed 16-color palette (0-15):
Color Number
Color Name
0
Black
1
Dark Blue
2
Purple
3
Dark Green
4
Brown
5
Dark Gray
6
Light Gray
7
White
8
Red
9
Orange
10
Yellow
11
Light Green
12
Light Blue
13
Gray
14
Pink
15
Peach
49.8 Game Development Patterns
49.8.1 Basic Game Structure
import pyxelclass Game:def__init__(self): pyxel.init(160, 120, title="My Pyxel Game")self.player_x =80self.player_y =60 pyxel.run(self.update, self.draw)def update(self):# Handle quittingif pyxel.btnp(pyxel.KEY_Q): pyxel.quit()# Update game state hereif pyxel.btn(pyxel.KEY_RIGHT):self.player_x +=2def draw(self):# Clear screen pyxel.cls(0)# Draw game elements pyxel.circ(self.player_x, self.player_y, 8, 11)# Start the gameGame()
49.8.2 Boundary Management
def keep_in_bounds(x, y, width, height, screen_width, screen_height):"""Keep an object within screen boundaries.""" x =max(0, min(x, screen_width - width)) y =max(0, min(y, screen_height - height))return x, y
49.8.3 Sprite Atlas Pattern
# Sprite atlas dictionaryatlas = {"player": (0, 0, 0, 16, 16, 0), # bank, x, y, width, height, colorkey"enemy": (0, 16, 0, 16, 16, 0),"item": (0, 0, 16, 8, 8, 0)}def draw_sprite(name, x, y):if name in atlas: bank, u, v, w, h, colorkey = atlas[name] pyxel.blt(x, y, bank, u, v, w, h, colorkey)
49.9 Tips and Best Practices
Organization: Group related sprites together in the image bank
Transparency: Use color 0 (black) as the transparent color for sprites
Coordinate System: (0,0) is at the top-left corner; x increases right, y increases down
Performance: Minimize drawing operations for better performance
Input: Use btn() for continuous actions (movement) and btnp() for one-time actions (shooting)