PewPew¶
Es existiert eine ganze Familie von PewPew-Geräten. Allen gemeinsam ist das 8×8-Display mit 4 Farben und die 6 Tasten, und sie werden über dasselbe API angesteuert, so dass Programme unverändert auf allen laufen, soweit sie keine weiteren Hardware-spezifischen Funktionen benützen. Sie unterscheiden sich in der Form, den Displayfarben und der Wahl des Mikrocontrollers und können beim Entwickler Radomir Dopieralski auf https://www.tindie.com/stores/deshipu/ oder beim Hersteller Makerfabs erworben werden.
Installation¶
Die nötigen Libraries für den Betrieb unseres PewPew Lite 4.2 mit MicroPython finden sich auf https://github.com/pewpew-game/pew-pewpewlite-4.x-micropython. Ausserdem benötigen wir https://github.com/cwalther/game-menu/tree/menumodule. Verwende bei beiden Clone or download ▸ Download ZIP (oder klone sie mit Git). Wer will, kann von den game- Repositories unter https://github.com/pewpew-game noch ein paar Beispiel-Spiele herunterladen.
Beim ersten Start nach der Installation wurde von MicroPython in dem Teil des Flash-Speichers, der nicht von der Firmware belegt ist, ein Filesystem eingerichtet. Dort können Libraries und Scripts abgelegt werden, ohne dass die Firmware neu installiert werden muss. Um Dateien vom Computer zu übertragen, kann WebREPL verwendet werden, entweder mit dem Kasten oben rechts im Web-Interface oder auf der Kommandozeile mit dem im WebREPL-Download enthaltenen Tool webrepl_cli.py. Es gibt keinen offiziellen Standard, um Dateien über die serielle (USB-) Verbindung zu übertragen, jedoch tun das einige von der Community entwickelte Tools, indem sie sie quasi automatisiert über die REPL eintippen. Beispiele sind pyboard.py, rshell und ampy.
Die Dateien pew.py, random.py und time.py aus pew-pewpewlite-4.x-micropython müssen in den Ordner /lib/ auf dem Board. Dieser existiert standardmässig noch nicht und muss erst erzeugt werden. Bei webrepl_cli.py kann ein absoluter Zielpfad angegeben werden, das Web-Interface platziert Dateien immer im current working directory. Dieses kann per REPL geändert werden. Führe also folgendes aus:
>>> import os
>>> os.mkdir('/lib')
>>> os.chdir('/lib')
Ausserdem brauchen wir das Modul main.py aus game-menu. Wenn es ins Wurzelverzeichnis / installiert wird, präsentiert es beim Einschalten ein Menu, aus dem alle installierten Spiele gestartet werden können. Jegliche Datei namens /main.py wird beim Starten automatisch ausgeführt, nach /boot.py, welches automatisch kreiert wurde und einige Grundeinstellungen macht, unter anderem den WebREPL-Server startet. Um mit dem Web-Interface dorthin zu installieren, einfach vorher wieder os.chdir('/') ausführen.
Der Inhalt des Filesystems kann mit os.listdir() und anderen Funktionen aus dem Modul os erkundet werden.
API-Dokumentation¶
Die Dokumentation für PewPew ist zu finden unter https://pewpew.readthedocs.io/, und das Kapitel zum API ist hier zur Gänze reproduziert:
PewPew Library Reference¶
-
pew.init()¶ Initialize the module.
This function switches the display on and performs some basic setup.
-
pew.brightness(level)¶ Set the brightness of the display, from 0 (minimum) to 15 (maximum). On devices that don’t support varying the brightness this does nothing.
-
pew.show(pix)¶ Show the provided image on the display, starting at the top left corner. You will want to call this once for every frame.
-
pew.keys()¶ Return a number telling which keys (or buttons) have been pressed since the last check. The number can then be filtered with the
&operator and theK_X,K_DOWN,K_LEFT,K_RIGHT,K_UP, andK_Oconstants to see whether any of the keys was pressed.
-
pew.tick(delay)¶ Wait until
delayseconds have passed since the last call to this function. You can call it every frame to ensure a constant frame rate.
-
class
pew.Pix(width=8, height=8, buffer=None)¶ Pix represents a drawing surface,
widthpixels wide andheightpixels high.If no
bufferis specified for storing the data, a suitable one will be automatically created.-
classmethod
from_iter(cls, lines)¶ Creates a new Pix and initialzes its contents by iterating over
linesand then over individual pixels in each line. All the lines have to be at least as long as the first one.
-
classmethod
from_text(cls, text, color=None, background=0, colors=None)¶ Creates a new Pix and renders the specified text on it. It is exactly the size needed to fit the specified text. Newlines and other control characters are rendered as spaces.
If
coloris not specified, it will use yellow and red for the letters by default. Otherwise it will use the specified color, withbackgroundcolor as the background.Alternatively,
colorsmay be specified as a 4-tuple of colors, and then thecolorandbackgroundarguments are ignored, and the four specified colors are used for rendering the text.
-
pixel(self, x, y, color=None)¶ If
coloris specified, sets the pixel at locationx,yto that color. If not, returns the color of the pixel at that location.If the location is out of bounds of the drawing surface, returns 0.
-
box(self, color, x=0, y=0, width=self.width, height=self.height)¶ Draws a filled box with the specified
colorwith its top left corner at the specified location and of the specified size. If no location and size are specified, fills the whole drawing surface.
-
blit(self, source, dx=0, dy=0, x=0, y=0, width=None, height=None, key=None)¶ Copied the
sourcedrawing surface onto this surface at location specified withdxanddy.If
x,y,widthandheightare specified, only copies that fragment of thesourceimage, otherwise copies it whole.If
keycolor is specified, that color is considered transparent on the source image, and is not copied onto this drawing surface.
-
classmethod
Ausprobieren¶
Spiele auf der REPL mit dem PewPew-API: Zeichne etwas aufs Display! Prüfe, welche Tasten gedrückt sind! Welche der Zahlen 0 – 3 stellt welche Farbe dar?
Vorschläge zum Einstieg:
>>> import pew
>>> pew.init()
>>> p = pew.Pix()
>>> p.pixel(1, 2, 3)
>>> pew.show(p)
>>> pew.keys()
Schritt 1: Programm-Skelett¶
Mitschreiben im Programm: Grundlegende Spiel-Schleife.
import pew
# -- initialization ----
pew.init()
screen = pew.Pix()
# -- game loop ----
while True:
# -- input handling ----
k = pew.keys()
# modify state
# -- drawing ----
# draw on screen
pew.show(screen)
pew.tick(0.15)
Neues: Kommentare
Speichere das Programm als four.py und übertrage es aufs Board. Um es von der REPL aus auszuführen, wird das import-Statement verwendet:
>>> import four
Mit ctrl-C kommst du aus der unendlichen Schleife auf den REPL-Prompt zurück. Wird jetzt nochmals import four ausgeführt, geschieht nichts. Warum? Wie in Hello World: LED blinken erwähnt, wird der Code nur beim ersten Import ausgeführt und das resultierende Modul-Objekt gecacht. Diesen Cache müssen wir erst löschen, um das Programm nochmals auszuführen. Er ist von Python aus zugänglich als Dictionary sys.modules. Also:
>>> import sys
>>> del sys.modules['four']
Nebst ctrl-C gibt es noch einen zweiten Weg, ein PewPew-Programm zu beenden: Drücke auf dem Gerät alle vier Richtungstasten gleichzeitig. Dadurch wird ein Fehler namens GameOver ausgelöst, der das Programm abbricht. Dem Traceback auf der REPL ist zu entnehmen, dass der Fehler aus der Funktion keys in pew.py kommt. Diese Methode funktionert also nur bei Programmen, die regelmässig pew.keys() aufrufen.