Skip to main content

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 and de.

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.

Currently, only the English API is fully translated, the German API is automatically generated. All other languages will be added when PyStage gets more stable. Get in touch if you want to help maintain a translation.

Deutsch

Die deutsche API heißt pystage.de. Auch die Werte aus den Auswahlmenüs von Scratch kannst du auf Deutsch angeben (Groß- und Kleinschreibung ist egal, die englischen Werte funktionieren weiterhin), und Fehlermeldungen und Hinweise erscheinen auf Deutsch:

from pystage.de import Bühne, Figur

bühne = Bühne(titel="Mein erstes Spiel")
zombie = bühne.füge_eine_figur_hinzu()

def nach_oben(zombie: Figur):
    zombie.ändere_y_um(10)

zombie.wenn_taste_gedrückt_wird("Pfeil nach oben", nach_oben)
zombie.wenn_taste_gedrückt_wird("Leertaste", nach_oben)
bühne.abspielen()

Weitere Werte: "beliebiges" (Taste), "letztes", "zufälliges" und "alles" bei Listen, "Betrag", "abrunden", "aufrunden", "Wurzel" bei der mathematischen Funktion, "nächstes Bühnenbild", "Mauszeiger", "Rand", "mir selbst".

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.

Contributors

  • Kai Eckert, Stuttgart Media University
  • Florian Rupp, Stuttgart Media University

Metadata

Release files for pystage 0.2.0

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.2.0
File Size Uploaded Provenance Actions
pystage-0.2.0.tar.gz 1.4 MB Without provenance Details for pystage-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 2.5 MB

Release files / pystage-0.2.0.tar.gz

Download URL pystage-0.2.0.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
07331af42dc8123d84ad54e3c93cc6dadd89c0b67df9d77945e16983d141235e
BLAKE2b-256 checksum
How to use checksums
dfd4fe0f32c7a76dd05517b0f84c802db0e1277f4911d44ef8f1473829c6727a
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.2.0-py3-none-any.whl

Download URL pystage-0.2.0-py3-none-any.whl
Size 1.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
ce7942509d9e86e123eeb71e36d086aac863b1d5c40b3e85961f0c67f8973aff
BLAKE2b-256 checksum
How to use checksums
56f7e02c56ea239d37f93baf5208f3e51023340349b34f526a935b7caef1ac6c
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.2.0

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