Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

Repository files navigation

piston2d

made-with-pythonmade-with-rustPRs Welcome

piston2d, or piston2d-python, is a Python binding for the amazing Rust library piston2d.

This is very WIP, so I will not accept any issues (yet). Assume that breaking changes happen with every commit.

Getting started

This assumes you have installed the library to your computer. This is not meant to be extensive, but will develop into a proper tutorial as the project goes on.

Setting up the window and event handlers

frompiston2d.windowimportWindowfrompiston2d.windowimportWindowSettingsfrompiston2d.window.eventsimportEventsfrompiston2d.window.eventsimportEventSettingswindow=Window(WindowSettings("my window name", (180, 180)))
events=Events(EventSettings())

Setting up graphics

frompiston2d.openglimportGlGraphics# ...graphics=GlGraphics("3.2")

Start the loop

# List to keep track of keys...keys= []
whileevent:=events.next(window):
ifbutton:=event.press_args():
keys.append(button.value())
print("Keys pressed: {}".format(keys))
ifbutton:=event.release_args():
try:
keys.remove(button.value())
except:
pass

Draw loop

whileevent:=events.next(window):
# ...ifargs:=event.render_args():
# Begin the draw loopcontext=graphics.draw_begin(args.viewport)
# Draw code here# End the draw loopgraphics.draw_end()

Drawing shapes

As of writing, only the rectangle and circle arc are implemented:

frompiston2d.graphicsimportcircle_arc, rectangleWHITE= [1.0, 1.0, 1.0, 1.0]
# ...whileevent:=events.next(window):
# ...ifargs:=event.render_args():
# Begin the draw loopcontext=graphics.draw_begin(args.viewport)
# Draw circle arc in center (ish)circle_arc(WHITE, 15.0, 0.0,
math.tau, [50, 50, 80, 80], context.transform(), graphics)
# Draw rectangle in top left cornerrectangle(WHITE,
[0, 0, 30, 30], context.transform(), graphics)
# End the draw loopgraphics.draw_end()

You may also want to clear the screen:

# ...BLACK= [0.0, 0.0, 0.0, 1.0]
whileevent:=events.next(window):
# ...ifargs:=event.render_args():
# Begin the draw loopcontext=graphics.draw_begin(args.viewport)
# Clear the screengraphics.clear_color(BLACK)
# Draw code here...# End the draw loopgraphics.draw_end()

Examples

  • tests/simple_window.py • A simple window to test drawing using GlGraphics.

    Example: Example

Building

Use setuptools to build

pipinstallsetuptoolswheelsetuptools_rustpythonsetup.pyinstall

Releases

Packages

Contributors

Languages