Skip to main content

A retro game engine for Python

Project description

Downloads GitHub Repo stars GitHub forks GitHub Sponsors

ko-fi

[ English | 中文 | Deutsch | Español | Français | Italiano | 日本語 | 한국어 | Português | Русский ]

Pyxel is a retro game engine for Python.

Thanks to its simple specifications inspired by retro gaming consoles, such as only 16 colors can be displayed and only 4 sounds can be played back at the same time, you can feel free to enjoy making pixel art style games.

The motivation for the development of Pyxel is the feedback from users. Please give Pyxel a star on GitHub!

Pyxel's specifications and APIs are inspired by PICO-8 and TIC-80.

Pyxel is open source and free to use. Let's start making a retro game with Pyxel!

Specifications

  • Runs on Windows, Mac, Linux, and Web
  • Programming with Python
  • 16 color palette
  • 256x256 sized 3 image banks
  • 256x256 sized 8 tilemaps
  • 4 channels with 64 definable sounds
  • 8 musics which can combine arbitrary sounds
  • Keyboard, mouse, and gamepad inputs
  • Image and sound editor

Color Palette

How to Install

Windows

After installing Python3 (version 3.7 or higher), run the following command:

pip install -U pyxel

If you install Python using the official installer, please check the Add Python 3.x to PATH checkbox to enable pyxel command.

Mac

After installing Python3 (version 3.7 or higher), run the following command:

python3 -m pip install -U pyxel

If you use Python3, which is installed by default on Mac, please add sudo to the beginning of the above command to enable pyxel command.

Linux

After installing the SDL2 package (libsdl2-dev for Ubuntu), Python3 (version 3.7 or higher), and python3-pip, run the following command:

sudo pip3 install -U pyxel

If the above doesn't work, try self-building according to the instructions in Makefile.

Web

The web version of Pyxel does not require Python or Pyxel installation and runs on PCs as well as smartphones and tablets with supported web browsers.

For specific instructions, please refer to this page.

Try Pyxel Examples

After installing Pyxel, the examples of Pyxel will be copied to the current directory with the following command:

pyxel copy_examples

The examples to be copied are as follows:

01_hello_pyxel.py Simplest application Demo Code
02_jump_game.py Jump game with Pyxel resource file Demo Code
03_draw_api.py Demonstration of drawing APIs Demo Code
04_sound_api.py Demonstration of sound APIs Demo Code
05_color_palette.py Color palette list Demo Code
06_click_game.py Mouse click game Demo Code
07_snake.py Snake game with BGM Demo Code
08_triangle_api.py Demonstration of triangle drawing APIs Demo Code
09_shooter.py Shoot'em up game with screen transition Demo Code
10_platformer.py Side-scrolling platform game with map Demo Code
11_offscreen.py Offscreen rendering with Image class Demo Code
12_perlin_noise.py Perlin noise animation Demo Code
13_bitmap_font.py Drawing a bitmap font Demo Code
99_flip_animation.py Animation with flip function (non-web platforms only) Demo Code
30SecondsOfDaylight.pyxapp 1st Pyxel Jam winning game by Adam Demo Code
megaball.pyxapp Arcade ball physics game by Adam Demo Code
8bit-bgm-gen.pyxapp Background music generator by frenchbread Demo Code

An examples can be executed with the following commands:

cd pyxel_examples
pyxel run 01_hello_pyxel.py
pyxel play 30SecondsOfDaylight.pyxapp

How to Use

Create Pyxel Application

After importing the Pyxel module in your python script, specify the window size with init function first, then starts the Pyxel application with run function.

import pyxel

pyxel.init(160, 120)

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

def draw():
    pyxel.cls(0)
    pyxel.rect(10, 10, 20, 20, 11)

pyxel.run(update, draw)

The arguments of run function are update function to update each frame and draw function to draw screen when necessary.

In an actual application, it is recommended to wrap pyxel code in a class as below:

import pyxel

class App:
    def __init__(self):
        pyxel.init(160, 120)
        self.x = 0
        pyxel.run(self.update, self.draw)

    def update(self):
        self.x = (self.x + 1) % pyxel.width

    def draw(self):
        pyxel.cls(0)
        pyxel.rect(self.x, 0, 8, 8, 9)

App()

When creating simple graphics without animation, show function can be used to make the code more concise.

import pyxel

pyxel.init(120, 120)
pyxel.cls(1)
pyxel.circb(60, 60, 40, 7)
pyxel.show()

Run Pyxel Application

The created Python script can be executed with the following command:

pyxel run PYTHON_SCRIPT_FILE

It can also be executed like a normal Python script:

python3 PYTHON_SCRIPT_FILE

(For Windows, type python instead of python3)

Special Controls

The following special controls can be performed while a Pyxel application is running:

  • Esc
    Quit the application
  • Alt(Option)+1
    Save the screenshot to the desktop
  • Alt(Option)+2
    Reset the recording start time of the screen capture video
  • Alt(Option)+3
    Save the screen capture video to the desktop (up to 10 seconds)
  • Alt(Option)+0
    Toggle the performance monitor (fps, update time, and draw time)
  • Alt(Option)+Enter
    Toggle full screen

How to Create Resources

Pyxel Editor can create images and sounds used in a Pyxel application.

It starts with the following command:

pyxel edit PYXEL_RESOURCE_FILE

If the specified Pyxel resource file (.pyxres) exists, the file is loaded, and if it does not exist, a new file is created with the specified name. If the resource file is omitted, the name is my_resource.pyxres.

After starting Pyxel Editor, the file can be switched by dragging and dropping another resource file. If the resource file is dragged and dropped while holding down Ctrl(Cmd) key, only the resource type (Image/Tilemap/Sound/Music) that is currently being edited will be loaded. This operation enables to combine multiple resource files into one.

The created resource file can be loaded with load function.

Pyxel Editor has the following edit modes.

Image Editor:

The mode to edit the image banks.

By dragging and dropping an image file (png/gif/jpeg) onto the Image Editor screen, the image can be loaded into the currently selected image bank.

Tilemap Editor:

The mode to edit tilemaps in which images of the image banks are arranged in a tile pattern.

Sound Editor:

The mode to edit sounds.

Music Editor:

The mode to edit musics in which the sounds are arranged in order of playback.

Other Resource Creation Methods

Pyxel images and tilemaps can also be created by the following methods:

  • Create an image from a list of strings with Image.set function or Tilemap.set function
  • Load an image file (png/gif/jpeg) in Pyxel palette with Image.load function

Pyxel sounds can also be created in the following method:

  • Create a sound from strings with Sound.set function or Music.set function

Please refer to the API reference for usage of these functions.

How to Distribute Applications

Pyxel supports a dedicated application distribution file format (Pyxel application file) that works across platforms.

Create the Pyxel application file (.pyxapp) with the following command:

pyxel package APP_DIR STARTUP_SCRIPT_FILE

If the application should include resources or additional modules, place them in the application directory.

The created application file can be executed with the following command:

pyxel play PYXEL_APP_FILE

Pyxel application file also can be converted to an executable or an HTML file with the pyxel app2exe or pyxel app2html commands.

API Reference

System

  • width, height
    The width and height of the screen

  • frame_count
    The number of the elapsed frames

  • init(width, height, [title], [fps], [quit_key], [display_scale], [capture_scale], [capture_sec])
    Initialize the Pyxel application with screen size (width, height). The following can be specified as options: the window title with title, the frame rate with fps, the key to quit the application with quit_key, the scale of the display with display_scale, the scale of the screen capture with capture_scale, and the maximum recording time of the screen capture video with capture_sec.
    e.g. pyxel.init(160, 120, title="My Pyxel App", fps=60, quit_key=pyxel.KEY_NONE, capture_scale=3, capture_sec=0)

  • run(update, draw)
    Start the Pyxel application and call update function for frame update and draw function for drawing.

  • show()
    Show the screen and wait until the Esc key is pressed.

  • flip()
    Refrech the screen by one frame. The application exits when the Esc key is pressed. This function only works on non-web platforms.

  • quit()
    Quit the Pyxel application.

Resource

  • load(filename, [image], [tilemap], [sound], [music])
    Load the resource file (.pyxres). If False is specified for the resource type (image/tilemap/sound/music), the resource will not be loaded. If a palette file (.pyxpal) of the same name exists in the same location as the resource file, the palette display color will also be changed. The palette file is a hexadecimal entry of the display colors, separated by newlines. The palette file can also be used to change the colors displayed in Pyxel Editor.

Input

  • mouse_x, mouse_y
    The current position of the mouse cursor

  • mouse_wheel
    The current value of the mouse wheel

  • btn(key)
    Return True if key is pressed, otherwise return False. (Key definition list)

  • btnp(key, [hold], [repeat])
    Return True if key is pressed at that frame, otherwise return False. When hold and repeat are specified, True will be returned at the repeat frame interval when the key is held down for more than hold frames.

  • btnr(key)
    Return True if key is released at that frame, otherwise return False.

  • mouse(visible)
    If visible is True, show the mouse cursor. If False, hide it. Even if the mouse cursor is not displayed, its position is updated.

Graphics

  • colors
    List of the palette display colors. The display color is specified by a 24-bit numerical value. Use colors.from_list and colors.to_list to directly assign and retrieve Python lists.
    e.g. old_colors = pyxel.colors.to_list(); pyxel.colors.from_list([0x111111, 0x222222, 0x333333]); pyxel.colors[15] = 0x112233

  • image(img)
    Operate the image bank img (0-2). (See the Image class)
    e.g. pyxel.image(0).load(0, 0, "title.png")

  • tilemap(tm)
    Operate the tilemap tm (0-7). (See the Tilemap class)

  • clip(x, y, w, h)
    Set the drawing area of the screen from (x, y) to width w and height h. Reset the drawing area to full screen with clip().

  • camera(x, y)
    Change the upper left corner coordinates of the screen to (x, y). Reset the upper left corner coordinates to (0, 0) with camera().

  • pal(col1, col2)
    Replace color col1 with col2 at drawing. pal() to reset to the initial palette.

  • cls(col)
    Clear screen with color col.

  • pget(x, y)
    Get the color of the pixel at (x, y).

  • pset(x, y, col)
    Draw a pixel of color col at (x, y).

  • line(x1, y1, x2, y2, col)
    Draw a line of color col from (x1, y1) to (x2, y2).

  • rect(x, y, w, h, col)
    Draw a rectangle of width w, height h and color col from (x, y).

  • rectb(x, y, w, h, col)
    Draw the outline of a rectangle of width w, height h and color col from (x, y).

  • circ(x, y, r, col)
    Draw a circle of radius r and color col at (x, y).

  • circb(x, y, r, col)
    Draw the outline of a circle of radius r and color col at (x, y).

  • elli(x, y, w, h, col)
    Draw an ellipse of width w, height h and color col from (x, y).

  • ellib(x, y, w, h, col)
    Draw the outline of an ellipse of width w, height h and color col from (x, y).

  • tri(x1, y1, x2, y2, x3, y3, col)
    Draw a triangle with vertices (x1, y1), (x2, y2), (x3, y3) and color col.

  • trib(x1, y1, x2, y2, x3, y3, col)
    Draw the outline of a triangle with vertices (x1, y1), (x2, y2), (x3, y3) and color col.

  • fill(x, y, col)
    Fill the area connected with the same color as (x, y) with color col.

  • blt(x, y, img, u, v, w, h, [colkey])
    Copy the region of size (w, h) from (u, v) of the image bank img (0-2) to (x, y). If negative value is set for w and/or h, it will reverse horizontally and/or vertically. If colkey is specified, treated as transparent color.

  • bltm(x, y, tm, u, v, w, h, [colkey])
    Copy the region of size (w, h) from (u, v) of the tilemap tm (0-7) to (x, y). If negative value is set for w and/or h, it will reverse horizontally and/or vertically. If colkey is specified, treated as transparent color. The size of a tile is 8x8 pixels and is stored in a tilemap as a tuple of (tile_x, tile_y).
  • text(x, y, s, col)
    Draw a string s of color col at (x, y).

Audio

  • sound(snd)
    Operate the sound snd (0-63). (See the Sound class)
    e.g. pyxel.sound(0).speed = 60

  • music(msc)
    Operate the music msc (0-7). (See the Music class)

  • play_pos(ch)
    Get the sound playback position of channel ch (0-3) as a tuple of (sound no, note no). Returns None when playback is stopped.

  • play(ch, snd, [tick], [loop])
    Play the sound snd (0-63) on channel ch (0-3). If snd is a list, it will be played in order. The playback start position can be specified by tick (1 tick = 1/120 seconds). If True is specified for loop, loop playback is performed.

  • playm(msc, [tick], [loop])
    Play the music msc (0-7). The playback start position can be specified by tick (1 tick = 1/120 seconds). If True is specified for loop, loop playback is performed.

  • stop([ch])
    Stops playback of the specified channel ch (0-3). stop() to stop playing all channels.

Math

  • ceil(x)
    Returns the smallest integer greater than or equal to x.

  • floor(x)
    Returns the largest integer less than or equal to x.

  • sgn(x)
    Returns 1 when x is positive, 0 when it is zero, and -1 when it is negative.

  • sqrt(x)
    Returns the square root of x.

  • sin(deg)
    Returns the sine of deg degrees.

  • cos(deg)
    Returns the cosine of deg degrees.

  • atan2(y, x)
    Returns the arctangent of y/x in degrees.

  • rseed(seed: int)
    Sets the seed of the random number generator.

  • rndi(a, b)
    Returns an random integer greater than or equal to a and less than or equal to b.

  • rndf(a, b)
    Returns a random decimal greater than or equal to a and less than or equal to b.

  • nseed(seed)
    Sets the seed of Perlin noise.

  • noise(x, [y], [z])
    Returns the Perlin noise value for the specified coordinates.

Image Class

  • width, height
    The width and height of the image

  • set(x, y, data)
    Set the image at (x, y) by a list of strings.
    e.g. pyxel.image(0).set(10, 10, ["0123", "4567", "89ab", "cdef"])

  • load(x, y, filename)
    Load the image file (png/gif/jpeg) at (x, y).

  • pget(x, y)
    Get the pixel color at (x, y).

  • pset(x, y, col)
    Draw a pixel of color col at (x, y).

Tilemap Class

  • width, height
    The width and height of the tilemap

  • refimg
    The image bank (0-2) referenced by the tilemap

  • set(x, y, data)
    Set the tilemap at (x, y) by a list of strings.
    e.g. pyxel.tilemap(0).set(0, 0, ["0000 0100 a0b0", "0001 0101 a1b1"])

  • pget(x, y)
    Get the tile at (x, y). A tile is a tuple of (tile_x, tile_y).

  • pset(x, y, tile)
    Draw a tile at (x, y). A tile is a tuple of (tile_x, tile_y).

Sound Class

  • notes
    List of notes (0-127). The higher the number, the higher the pitch, and at 33 it becomes 'A2'(440Hz). The rest is -1.

  • tones
    List of tones (0:Triangle / 1:Square / 2:Pulse / 3:Noise)

  • volumes
    List of volumes (0-7)

  • effects
    List of effects (0:None / 1:Slide / 2:Vibrato / 3:FadeOut)

  • speed
    Playback speed. 1 is the fastest, and the larger the number, the slower the playback speed. At 120, the length of one note becomes 1 second.

  • set(notes, tones, volumes, effects, speed)
    Set notes, tones, volumes, and effects with a string. If the tones, volumes, and effects length are shorter than the notes, it is repeated from the beginning.

  • set_notes(notes)
    Set the notes with a string made of 'CDEFGAB'+'#-'+'0123' or 'R'. Case-insensitive and whitespace is ignored.
    e.g. pyxel.sound(0).set_notes("G2B-2D3R RF3F3F3")

  • set_tones(tones)
    Set the tones with a string made of 'TSPN'. Case-insensitive and whitespace is ignored.
    e.g. pyxel.sound(0).set_tones("TTSS PPPN")

  • set_volumes(volumes)
    Set the volumes with a string made of '01234567'. Case-insensitive and whitespace is ignored.
    e.g. pyxel.sound(0).set_volumes("7777 7531")

  • set_effects(effects)
    Set the effects with a string made of 'NSVF'. Case-insensitive and whitespace is ignored.
    e.g. pyxel.sound(0).set_effects("NFNF NVVS")

Music Class

  • snds_list
    Two-dimensional list of sounds (0-63) with the number of channels

  • set(snds0, snds1, snds2, snds3)
    Set the lists of sound (0-63) of all channels. If an empty list is specified, that channel is not used for playback.
    e.g. pyxel.music(0).set([0, 1], [2, 3], [4], [])

Advanced APIs

Pyxel has "advanced APIs" that are not mentioned in this reference because they "may confuse users" or "need specialized knowledge to use".

If you are familiar with your skills, try to create amazing works with this as a clue!

How to Contribute

Submitting Issues

Use the Issue Tracker to submit bug reports and feature/enhancement requests. Before submitting a new issue, ensure that there is no similar open issue.

Manual Testing

Anyone manually testing the code and reporting bugs or suggestions for enhancements in the Issue Tracker are very welcome!

Submitting Pull Requests

Patches/fixes are accepted in form of pull requests (PRs). Make sure the issue the pull request addresses is open in the Issue Tracker.

Submitted pull request is deemed to have agreed to publish under MIT License.

Other Information

License

Pyxel is under MIT License. It can be reused within proprietary software, provided that all copies of the software or its substantial portions include a copy of the terms of the MIT License and also a copyright notice.

Recruiting Sponsors

Pyxel is looking for sponsors on GitHub Sponsors. Consider sponsoring Pyxel for continued maintenance and feature additions. Sponsors can consult about Pyxel as a benefit. Please see here for details.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pyxel-1.9.16-cp37-abi3-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.7+ Windows x86-64

pyxel-1.9.16-cp37-abi3-win32.whl (2.4 MB view details)

Uploaded CPython 3.7+ Windows x86

pyxel-1.9.16-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

pyxel-1.9.16-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (3.5 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ i686

pyxel-1.9.16-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.3 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

pyxel-1.9.16-cp37-abi3-macosx_11_0_arm64.whl (2.7 MB view details)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

pyxel-1.9.16-cp37-abi3-macosx_10_12_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.7+ macOS 10.12+ x86-64

File details

Details for the file pyxel-1.9.16-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: pyxel-1.9.16-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for pyxel-1.9.16-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9e078f43b6909c82d6582655395be13b1eac4087228c5728ca11b936bc69d94c
MD5 6598bb650156be4c0d29c192bf8ede1c
BLAKE2b-256 2031825c948c6974c99f443976f62750a564a7db1ab145dd470de940827af5a2

See more details on using hashes here.

File details

Details for the file pyxel-1.9.16-cp37-abi3-win32.whl.

File metadata

  • Download URL: pyxel-1.9.16-cp37-abi3-win32.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for pyxel-1.9.16-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 4277aa935d234c9188405a2fb6840d733e650960179994c72accd74f6d69ddd6
MD5 edb7163cd78b66c099f56d8fe940a784
BLAKE2b-256 ef9670b64bf096840d2b69fb19c5f7096c60caf35182cfe6ffee482b47b5de30

See more details on using hashes here.

File details

Details for the file pyxel-1.9.16-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyxel-1.9.16-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f54e5dec688359e95eb979ac041c0c0685bb450840dbfbd5eac3a4c40b4a073c
MD5 7cc0f1b38e6c0a12b90eedaa51d5b353
BLAKE2b-256 90089446b3b2d02d3646c19c14458c5d91e4d4fa6879dec3d53298cacacf1982

See more details on using hashes here.

File details

Details for the file pyxel-1.9.16-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for pyxel-1.9.16-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 0b11d20ca91b5c8e9145f1e74c8d3d2471594428dea89e6502f637e73bb64915
MD5 9e7de1115b929d5d9b4192b39e8befcb
BLAKE2b-256 c6d3847a447600add07c794b8b0ecefee70aff7b4dcfda8845109faf34b76df4

See more details on using hashes here.

File details

Details for the file pyxel-1.9.16-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyxel-1.9.16-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ccefcab1915f294094e754e1af8eafde228a640370b6d0989940080d17d9f7ab
MD5 4d88198a193b9601e426cb11616c6da9
BLAKE2b-256 7f40ee329de5918260bb3b5d51ba085dc8797d07433b81dbf511beb22029a0be

See more details on using hashes here.

File details

Details for the file pyxel-1.9.16-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyxel-1.9.16-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a2a958c520cdfd7b78ba3c98dcab18ba3e2d97e2a307e13f32ec8fe12e0cef08
MD5 b6a8b612aa05eea8abe638fd97d6bc73
BLAKE2b-256 6a74de1cbf317a5561d231802a026e32c9432ee4aab4848b82ab8102fce7d6b9

See more details on using hashes here.

File details

Details for the file pyxel-1.9.16-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyxel-1.9.16-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e97b226f22b47ee7ede806a9bddb43fdb6cccc0334d10088163201fd948edf0d
MD5 9cc9954fc97841101ff77cd6d08763af
BLAKE2b-256 0ebfc85cb9023f900fa720771c1518bce36a0d7e692b47d250842acb3ba7bd47

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page