Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| lehrkraefte:blc:informatik:glf20:oxotetris [2020/12/15 10:17] – Ivo Blöchliger | lehrkraefte:blc:informatik:glf20:oxotetris [2020/12/17 13:02] (current) – [Highscores auf dem Web] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Tetris auf der OxoCard ====== | ||
| + | Erläuterungen zum Code gibt es als Videos: | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | |||
| + | <code python oxotetris.py> | ||
| + | from oxocard import * | ||
| + | from ivobuttons import * | ||
| + | from random import randrange | ||
| + | |||
| + | ivobuttons.delay=300 | ||
| + | ivobuttons.repeat_delay=100 | ||
| + | |||
| + | # Gamestate als Dictionary, d.h. wie eine Liste aber mit Namen als Indizes | ||
| + | tetris = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | |||
| + | # Ergibt True, wenn der Stein platziert werden kann, | ||
| + | # False sonst. | ||
| + | def platzierbar(stein, | ||
| + | global tetris | ||
| + | return all((pt[0]+x> | ||
| + | | ||
| + | |||
| + | |||
| + | # Generiert einen neuen Stein und gibt | ||
| + | # True zurück, wenn das möglich ist, und sonst | ||
| + | # False, wenn der Stein nicht mehr platziert werden kann. | ||
| + | def neuerStein(): | ||
| + | global tetris | ||
| + | # Zufälligen Stein kopieren | ||
| + | stein = randrange(len(tetris[" | ||
| + | tetris[" | ||
| + | tetris[" | ||
| + | | ||
| + | tetris[" | ||
| + | tetris[" | ||
| + | |||
| + | tetris[" | ||
| + | tetris[' | ||
| + | print(" | ||
| + | return platzierbar(tetris[" | ||
| + | |||
| + | # Generiert ein neues Array mit Koordinaten des gedrehten Steins | ||
| + | # Die richtung ist +1 oder -1 | ||
| + | def drehen(richtung): | ||
| + | global tetris | ||
| + | # Gedrehte Koordinaten | ||
| + | res = [[pt[1]*richtung, | ||
| + | # Untere linke Ecke | ||
| + | mins = [min([pt[0] for pt in res]), max([pt[1] for pt in res])] | ||
| + | for pt in res: | ||
| + | for i in range(2): | ||
| + | pt[i]-=mins[i] | ||
| + | return res | ||
| + | |||
| + | # Aktuellen Stein zeichnen (evtl. mit Farbe) | ||
| + | def steinZeichnen(farbe=None): | ||
| + | global tetris | ||
| + | if farbe==None: | ||
| + | farbe = tetris[' | ||
| + | for pt in tetris[' | ||
| + | if (pt[1]+tetris[' | ||
| + | if farbe==BLACK: | ||
| + | black(pt[0]+tetris[' | ||
| + | else: | ||
| + | fastDot(pt[0]+tetris[' | ||
| + | fastRepaint() | ||
| + | |||
| + | # y ist ein Array mit True/False, welche Linien zu blinken sind | ||
| + | def blinkLines(y): | ||
| + | for i in range(5): | ||
| + | for farbe in (WHITE, BLACK): | ||
| + | for yy in range(8): | ||
| + | if y[yy]: | ||
| + | for xx in range(8): | ||
| + | fastDot(xx, | ||
| + | fastRepaint() | ||
| + | sleep(0.1) | ||
| + | |||
| + | # Lässt Linien verschwinden, | ||
| + | def removeLines(y): | ||
| + | global tetris | ||
| + | count = 0 | ||
| + | for yy in range(8): | ||
| + | if y[yy]: | ||
| + | count+=1 | ||
| + | for yyy in range(yy, | ||
| + | for x in range(8): | ||
| + | f = BLACK | ||
| + | if yyy>0: | ||
| + | tetris[' | ||
| + | else: | ||
| + | tetris[' | ||
| + | if tetris[' | ||
| + | fastDot(x, | ||
| + | else: | ||
| + | black(x, | ||
| + | fastRepaint() | ||
| + | sleep(0.1) | ||
| + | tetris[' | ||
| + | print(" | ||
| + | | ||
| + | # Überprüft, | ||
| + | def checkLines(): | ||
| + | global tetris | ||
| + | # Array mit True, False, für volle Zeilen True | ||
| + | y = [all(tetris[' | ||
| + | # Volle Zeilen? | ||
| + | if any(y): | ||
| + | blinkLines(y) | ||
| + | removeLines(y) | ||
| + | | ||
| + | |||
| + | # Stein runter, evtl. Linien löschen, neuer Stein generieren | ||
| + | # Liefert False, wenn das Spiel weiter geht | ||
| + | # True, wenn GameOver ist. | ||
| + | def steinDown(): | ||
| + | global tetris | ||
| + | tetris[' | ||
| + | if platzierbar(tetris[' | ||
| + | steinZeichnen(BLACK) | ||
| + | tetris[' | ||
| + | steinZeichnen() | ||
| + | return False | ||
| + | else: | ||
| + | # Stein definitiv platzieren: | ||
| + | for pt in tetris[' | ||
| + | if (pt[1]+tetris[' | ||
| + | tetris[' | ||
| + | checkLines() | ||
| + | return not neuerStein() | ||
| + | |||
| + | |||
| + | # Richtung ist +1 oder -1 | ||
| + | def steinMove(richtung): | ||
| + | global tetris | ||
| + | if platzierbar(tetris[' | ||
| + | steinZeichnen(BLACK) | ||
| + | tetris[' | ||
| + | steinZeichnen() | ||
| + | | ||
| + | # Richtung ist +1 oder -1 | ||
| + | def steinRotate(richtung): | ||
| + | global tetris | ||
| + | gedreht = drehen(richtung) | ||
| + | if platzierbar(gedreht, | ||
| + | steinZeichnen(BLACK) | ||
| + | tetris[' | ||
| + | steinZeichnen() | ||
| + | |||
| + | def black(x,y): | ||
| + | fastDot(x, | ||
| + | |||
| + | def init(): | ||
| + | for x in range(8): | ||
| + | for y in range(8): | ||
| + | black(x,y) | ||
| + | neuerStein(); | ||
| + | steinZeichnen(); | ||
| + | |||
| + | bigTextScroll(" | ||
| + | init() | ||
| + | |||
| + | ############# | ||
| + | # Game Loop # | ||
| + | ############# | ||
| + | gameOver = False | ||
| + | |||
| + | while not gameOver: | ||
| + | zeit = getms() | ||
| + | s = ivobuttons.states() | ||
| + | if zeit-tetris[' | ||
| + | gameOver = steinDown(); | ||
| + | if s & IVO_L2: | ||
| + | steinMove(-1) | ||
| + | if s & IVO_R2: | ||
| + | steinMove(1) | ||
| + | if s & IVO_R1: | ||
| + | steinRotate(1) | ||
| + | if s & IVO_L1: | ||
| + | steinRotate(-1) | ||
| + | | ||
| + | |||
| + | # Game Over | ||
| + | |||
| + | print(" | ||
| + | while True: | ||
| + | bigTextScroll((" | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Highscores auf dem Web ===== | ||
| + | Was folgt sind Notizen, um von der OxoCard die Highscores auf dem Web zu publizieren. | ||
| + | <code python> | ||
| + | from tcpcom import * | ||
| + | # See dcoumentation directly in the code of the OxoCard modules files | ||
| + | Wlan.connect(" | ||
| + | client = HTTPClient() | ||
| + | client._isSSL=True | ||
| + | if client.connect(" | ||
| + | response = client.sendPostRequest("/ | ||
| + | print(response) | ||
| + | client.closeConnection() | ||
| + | |||
| + | </ | ||