kitao / pyxel

A retro game engine for Python
MIT License
15.35k stars 847 forks source link
game game-development game-engine gamedev graphics html opengl pico-8 pixel-art python pyxel rust tic-80 webgl

Downloads GitHub Repo stars GitHub forks GitHub Sponsors

ko-fi

[ English | 中文 | Deutsch | Español | Français | Italiano | 日本語 | 한국어 | Português | Русский | Türkçe | Українська ]

Pyxel is a retro game engine for Python.

With simple specifications inspired by retro gaming consoles, such as displaying only 16 colors and supporting 4 sound channels, you can easily enjoy making pixel-art-style games.

The development of Pyxel is driven by user feedback. Please give Pyxel a star on GitHub!

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

Pyxel is open source under the MIT License and free to use. Let's start making retro games with Pyxel!

Specifications

Color Palette

How to Install

Windows

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

pip install -U pyxel

When installing Python using the official installer, make sure to check the Add Python 3.x to PATH option to enable the pyxel command.

Mac

After installing Homebrew, run the following commands:

brew install pipx
pipx ensurepath
pipx install pyxel

To upgrade Pyxel after installation, run pipx upgrade pyxel.

Linux

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

sudo pip3 install -U pyxel

If the previous command fails, consider building Pyxel from source by following the instructions in the Makefile.

Web

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

For detailed instructions, please refer to this page.

Try Examples

After installing Pyxel, you can copy the examples to the current directory with the following command:

pyxel copy_examples

The following examples will be copied to your current directory:

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 transitions 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
14_synthesizer.py Synthesizer using audio expansion features Demo Code
15_tiled_map_file.py Loading and drawing a Tile Map File (.tmx) Demo Code
16_transform.py Image rotation and scaling Demo Code
99_flip_animation.py Animation with flip function (non-web platforms only) Demo Code
30sec_of_daylight.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

The examples can be executed with the following commands:

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

How to Use

Create Application

In your Python script, import the Pyxel module, specify the window size with the init function, and then start the Pyxel application with the 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 the run function are the update function, which processes frame updates, and the draw function, which handles screen drawing.

In an actual application, it is recommended to wrap Pyxel code in a class, as shown 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()

For creating simple graphics without animation, you can use the show function to simplify your code.

import pyxel

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

Run Application

A created script can be executed using the python command:

python PYTHON_SCRIPT_FILE

It can also be run with the pyxel run command:

pyxel run PYTHON_SCRIPT_FILE

Additionally, the pyxel watch command monitors changes in a specified directory and automatically re-runs the program when changes are detected:

pyxel watch WATCH_DIR PYTHON_SCRIPT_FILE

Directory monitoring can be stopped by pressing Ctrl(Command)+C.

Special Key Controls

The following special key actions are available while a Pyxel application is running:

How to Create Resources

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

You can start Pyxel Editor with the following command:

pyxel edit PYXEL_RESOURCE_FILE

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

After starting Pyxel Editor, you can switch to another resource file by dragging and dropping it onto Pyxel Editor.

The created resource file can be loaded using the load function.

Pyxel Editor has the following editing modes.

Image Editor

The mode for editing the image in each image bank.

You can drag and drop an image file (PNG/GIF/JPEG) into the image editor to load the image into the currently selected image bank.

Tilemap Editor

The mode for editing tilemaps that arrange images from the image banks in a tile pattern.

Drag and drop a TMX file (Tiled Map File) onto the tilemap editor to load its layer 0 into the currently selected tilemap.

Sound Editor

The mode for editing sounds used for melodies and sound effects.

Music Editor

The mode for editing musics in which the sounds are arranged in order of playback.

Other Resource Creation Methods

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

Pyxel sounds can also be created using the following method:

Refer to the API reference for the usage of these functions.

How to Distribute Applications

Pyxel supports a dedicated application distribution file format (Pyxel application file) that is cross-platform.

A Pyxel application file (.pyxapp) is created using the pyxel package command:

pyxel package APP_DIR STARTUP_SCRIPT_FILE

If you need to include resources or additional modules, place them in the application directory.

Metadata can be displayed at runtime by specifying it in the following format within the startup script. Fields other than title and author are optional.

# title: Pyxel Platformer
# author: Takashi Kitao
# desc: A Pyxel platformer example
# site: https://github.com/kitao/pyxel
# license: MIT
# version: 1.0

The created application file can be run using the pyxel play command:

pyxel play PYXEL_APP_FILE

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

API Reference

System

Resource

Input

Graphics

Audio

Math

Image Class

Tilemap Class

Sound Class

Music Class

Advanced API

Pyxel includes an "Advanced API" that is not mentioned in this reference, as it may confuse users or require specialized knowledge to use.

If you are confident in your skills, try creating amazing works using this as a guide!

How to Contribute

Submitting Issues

Use the Issue Tracker to submit bug reports and feature or enhancement requests. Before submitting a new issue, make sure there are no similar open issues.

Functional Testing

Anyone who manually tests the code and reports bugs or suggestions for enhancements in the Issue Tracker is very welcome!

Submitting Pull Requests

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

Submitting a pull request implies that you agree to license your contribution under the MIT License.

Other Information

License

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

Recruiting Sponsors

Pyxel is looking for sponsors on GitHub Sponsors. Please consider sponsoring Pyxel to support its continued maintenance and feature development. As a benefit, sponsors can consult directly with the Pyxel developer. For more details, please visit this page.