Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

PyStage

Documentation Status

Scratch-like Python programming.

PyStage bridges the gap between Scratch and Python. This module implements all code blocks as available in scratch. With the PyStage command line converter Scratch projects can be easily transferred to Python executable Code. To run your game in Python, PyStage uses pygame-ce, the community edition of PyGame.

test

Demo

Consider the following code. It defines a stage and a sprite on the stage. The functions are code blocks that are associated with events (i.e. connected to a hat block in Scratch) via the corresponding methods.

from pystage.en import Stage

stage = Stage()
stage.add_backdrop("grid")
zombie = stage.add_a_sprite()

def doit(zombie):
    zombie.say("Hello pyStage!")
    for i in range(4):
        zombie.move(10)
        zombie.wait(1)
        zombie.think("This is awesome!")
        zombie.turn_left(90)
        zombie.wait(1)
        zombie.think("")
    zombie.say("Move me around with WASD.")

zombie.when_program_starts(doit)

def right(zombie):
    zombie.change_x_by(10)

def left(zombie):
    zombie.change_x_by(-10)

def up(zombie):
    zombie.change_y_by(10)

def down(zombie):
    zombie.change_y_by(-10)

zombie.when_key_pressed("d", right)
zombie.when_key_pressed("a", left)
zombie.when_key_pressed("w", up)
zombie.when_key_pressed("s", down)

stage.play()

And here is the result:

Demo screen capture

Goals

  • Export your Scratch project to real executable Python code!
  • Helping coding beginners to learn Python "by Scratch".
  • Implementation of scratch blocks in several languages (ideal for kids). As of now en, de and lt.

A quick into video

See our quick intro to PyStage for ScratchCon 2021

Installation

pip install pystage

PyStage needs Python 3.10 or newer. pygame-ce is installed automatically. It cannot be installed alongside classic pygame, so if you have that installed, uninstall it first: pip uninstall pygame.

Getting started

If you are interested in PyStage and want to get in touch, feel free to join us on our Discord Server.

Languages

PyStage is available in these languages:

Get in touch if you want to help maintain a translation or add another language. How to review and correct a translation is described in TRANSLATING.md.

For the English API, you can start with the following example code:

from pystage.en import Sprite, Stage

stage = Stage()
zombie = stage.add_a_sprite()

def doit(zombie: Sprite):
    for i in range(4):
        zombie.move(10)
        zombie.turn_left(90)
        zombie.wait(1)

zombie.when_program_starts(doit)

stage.play()

Use the autocompletion of your editor to see what methods are available. There are methods for all Scratch blocks bound to your stage and sprite instances, including the Pen extension, variables, lists, clones and all events. The only blocks that do nothing are the ones that need a microphone (loudness).

Some things work differently than in normal Python programs, because your code behaves like a Scratch script:

  • The screen is refreshed after each iteration of a loop and after blocks that take time, like wait. You do not have to care about a game loop.
  • Like in Scratch, a sprite can never leave the stage completely (at least 15 pixels of it stay visible); set stage._core.fencing = False if your game needs sprites fully off the stage.
  • Functions in the same file that you call from your code behave like custom blocks in Scratch, i.e. they can contain loops and wait as well:
from pystage.en import Sprite, Stage, no_refresh

stage = Stage()
zombie = stage.add_a_sprite()

def jump(zombie: Sprite, height):
    for i in range(10):
        zombie.change_y_by(height / 10)
    for i in range(10):
        zombie.change_y_by(-height / 10)

@no_refresh
def draw_square(zombie: Sprite):
    # Like "run without screen refresh" in Scratch:
    # The square is there at once.
    zombie.pen_down()
    for i in range(4):
        zombie.move(50)
        zombie.turn_left(90)
    zombie.pen_up()

def doit(zombie: Sprite):
    draw_square(zombie)
    while True:
        jump(zombie, 80)
        zombie.wait(1)

zombie.when_program_starts(doit)

stage.play()
  • If there is an error in your code, the message is shown in the window and in the terminal.

The documentation is here: https://pystage.readthedocs.io/en/latest/pystage.en.html


NOTE

Please note this is an alpha version. Not all things may work as expected. If so, please contact us or open an issue. Documentation is automatically generated but especially the user-facing block API is still mostly empty.


Import your Scratch projects

Use our Web converter

  1. Publish your Scratch project or download your Scratch project as .sb3 file.
  2. Go to https://convert.pystage.org and convert your project.
  3. Download the resulting zip file, it contains your new Python project ready to run.

Convert on your computer

Converting a Scratch project to Python code:

  1. Export your Scratch project on the Scratch page. You will get an .sb3 file.
  2. Use our command line converter. Yes, it's unhandy, we will work on it to make things easier.

The base usage is python -m pystage.convert.sb3 <SB3 File> -l en -d <DIRECTORY> The parameters are:

  • -l language of generated python "scratch blocks". In the moment of writing, en and de are available. Without it, the language neutral core API is used.
  • -d the output directory, defaults to current directory
  • -i print intermediate code representation
  • -s print sb3 project.json
  • -p print python code
  • -v verbose
  • -vv debug mode

Scripts, custom blocks, variables, lists, monitors and comments of your project are converted. Blocks from extensions other than Pen (e.g. Music or Text to Speech) are not supported; they show up as a text starting with NOT SUPPORTED in the Python code.

Fonts and licences

PyStage is MIT licensed. It bundles the original Scratch 3 text fonts (Noto Sans, Source Serif Pro, Handlee, Knewave and Griffy under the SIL Open Font License 1.1, and Grand9K Pixel under CC BY-SA 3.0; the font files are unmodified and the licence of a font does not affect your own code) so that text in SVG costumes looks like in Scratch, and Roboto (Apache License 2.0) as fallback; see THIRD-PARTY-NOTICES.md.

Maintainer

  • Kai Eckert, Mannheim Technical University

Contributors

  • Branden Husted, Calvin University
  • Neil Mitchell
  • Zhonglin Niu, Calvin University
  • Victor Norman, Calvin University
  • Florian Rupp
  • daolong (ahlongxp)

Metadata

Release files for pystage 0.3.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystage 0.3.0a1
File Size Uploaded Provenance Actions
pystage-0.3.0a1.tar.gz 1.4 MB Without provenance Details for pystage-0.3.0a1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pystage 0.3.0a1
File Interpreter ABI Platform Actions
pystage-0.3.0a1-py3-none-any.whl Python 3 none any Details for pystage-0.3.0a1-py3-none-any.whl

Total release size: 2.6 MB

Release files / pystage-0.3.0a1.tar.gz

Download URL pystage-0.3.0a1.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
8f99c3a18fe1324928d93c823d560b8b2fc36e48fe7703c3d1aa90a7ec644b13
BLAKE2b-256 checksum
How to use checksums
856f1942750c88ba4ea6f1668d362cab9cd30c740450405cab2122c86e0eda87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release files / pystage-0.3.0a1-py3-none-any.whl

Download URL pystage-0.3.0a1-py3-none-any.whl
Size 1.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
0b26f099be7bc3f79846e6aa95d2b45db7efcb968ff6ac86e6c97b5fe2650da2
BLAKE2b-256 checksum
How to use checksums
59281ac3d8f909fbadc4d5dd32d90cd6df9f173e23caa826a17b81fb509b50c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Security for pystage 0.3.0a1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page