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.
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.
frompiston2d.windowimportWindowfrompiston2d.windowimportWindowSettingsfrompiston2d.window.eventsimportEventsfrompiston2d.window.eventsimportEventSettingswindow=Window(WindowSettings("my window name", (180, 180)))
events=Events(EventSettings())frompiston2d.openglimportGlGraphics# ...graphics=GlGraphics("3.2")# 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:
passwhileevent:=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()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()Use setuptools to build
pipinstallsetuptoolswheelsetuptools_rustpythonsetup.pyinstall