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:oxoaccel [2020/12/13 17:07] – [Weitere Demos] Ivo Blöchliger | lehrkraefte:blc:informatik:glf20:oxoaccel [2020/12/16 09:37] (current) – [Weitere Demos] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Spiele auf der OxoCard mit dem Beschleunigungssensor ====== | ||
| + | |||
| + | Der folgende Code sollte auf direkt auf der OxoCard getestet werden (macht sonst kaum Spass). | ||
| + | <code python accel.py> | ||
| + | from oxocard import * | ||
| + | from ivobuttons import * | ||
| + | from oxoaccelerometer import * | ||
| + | |||
| + | # Zustand vom Spiel | ||
| + | pos = [0,0] # x- und y-Koordinaten in einer Liste, pos[0], und pos[1] | ||
| + | farbe = RED | ||
| + | naechste = [0,0] # Zeit, ab wann die nächste Bewegung in x- und y-Richtung stattfinden darf. | ||
| + | |||
| + | # Beschleunigungssensor | ||
| + | acc = Accelerometer.create() | ||
| + | |||
| + | |||
| + | # Funktionen zum Zeichnen und löschen des Spielzustands | ||
| + | |||
| + | def zeichnen(): | ||
| + | global pos, farbe # Veriablen von ausserhalb dieser Funktion verwenden | ||
| + | fastDot(pos[0], | ||
| + | fastRepaint() | ||
| + | | ||
| + | def loeschen(): | ||
| + | global pos | ||
| + | fastDot(pos[0], | ||
| + | | ||
| + | # Begrenzt einen Wert zwischen 0 und 7 (damit man sicher auf dem Display bleibt). | ||
| + | def clip(v): | ||
| + | if v<0: | ||
| + | v=0 | ||
| + | if v>7: | ||
| + | v=7 | ||
| + | return v | ||
| + | | ||
| + | zeichnen() | ||
| + | |||
| + | pneu = [0,0] # Variable für neue Position | ||
| + | while True: | ||
| + | ac = acc.getValues() | ||
| + | pneu[0], pneu[1] = pos[0], pos[1] | ||
| + | zeit = getms() | ||
| + | for i in range(2): | ||
| + | if abs(ac[i])> | ||
| + | dir = 1 # Richtung positiv | ||
| + | if ac[i]< | ||
| + | dir = -1 | ||
| + | if i==1: # Vorzeichenwechsel für y | ||
| + | dir = -dir | ||
| + | pneu[i] = clip(pos[i]+dir) | ||
| + | # Kleine Beschleunigung, | ||
| + | naechste[i] = zeit + 200 - abs(ac[i]*20) | ||
| + | | ||
| + | # Nur wenn sich etwas geändert hat auch neu zeichnen. | ||
| + | # Arrays sind dann gleich (==) wenn alle Elemente gleich sind. | ||
| + | if pneu!=pos: | ||
| + | loeschen() | ||
| + | pos[0], pos[1] = pneu[0], pneu[1] | ||
| + | zeichnen() | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Weitere Demos ===== | ||
| + | |||
| + | <hidden Punkt mit Trail> | ||
| + | <code python trail.py> | ||
| + | from oxocard import * | ||
| + | from ivobuttons import * | ||
| + | from oxoaccelerometer import * | ||
| + | |||
| + | # Zustand vom Spiel | ||
| + | pos = [0,0] # x- und y-Koordinaten in einer Liste, pos[0], und pos[1] | ||
| + | farbe = RED # Farbe vom Punkt | ||
| + | naechste = [0,0] # Zeit, ab wann die nächste Bewegung in x- und y-Richtung stattfinden darf. | ||
| + | |||
| + | # Position und Farben vom Trail | ||
| + | ntrail = 12 | ||
| + | trail = [[0,0] for i in range(ntrail)] | ||
| + | farben = [(255-i*20, | ||
| + | farben[ntrail-1]=BLACK | ||
| + | trailstart = 0 | ||
| + | wegmit = getms() | ||
| + | |||
| + | # Beschleunigungssensor | ||
| + | acc = Accelerometer.create() | ||
| + | |||
| + | |||
| + | # Funktionen zum Zeichnen und löschen des Spielzustands | ||
| + | |||
| + | def zeichnen(): | ||
| + | global pos, farbe, trail, farben, trailstart, ntrail | ||
| + | for i in range(ntrail): | ||
| + | t = (i+trailstart)%ntrail | ||
| + | fastDot(trail[t][0], | ||
| + | fastDot(pos[0], | ||
| + | fastRepaint() | ||
| + | | ||
| + | def loeschen(): | ||
| + | global pos, trail, trailstart | ||
| + | fastDot(pos[0], | ||
| + | t = trailstart-1 | ||
| + | fastDot(trail[t][0], | ||
| + | | ||
| + | def trailsave(): | ||
| + | global pos, trail, trailstart | ||
| + | trailstart = (trailstart+ntrail-1)%ntrail | ||
| + | trail[trailstart][0] = pos[0] | ||
| + | trail[trailstart][1] = pos[1] | ||
| + | | ||
| + | | ||
| + | # Begrenzt einen Wert zwischen 0 und 7 (damit man sicher auf dem Display bleibt). | ||
| + | def clip(v): | ||
| + | if v<0: | ||
| + | v=0 | ||
| + | if v>7: | ||
| + | v=7 | ||
| + | return v | ||
| + | | ||
| + | zeichnen() | ||
| + | |||
| + | pneu = [0,0] # Variable für neue Position | ||
| + | while True: | ||
| + | ac = acc.getValues() | ||
| + | pneu[0], pneu[1] = pos[0], pos[1] | ||
| + | zeit = getms() | ||
| + | for i in range(2): | ||
| + | if abs(ac[i])> | ||
| + | dir = 1 # Richtung positiv | ||
| + | if ac[i]< | ||
| + | dir = -1 | ||
| + | if i==1: # Vorzeichenwechsel für y | ||
| + | dir = -dir | ||
| + | pneu[i] = clip(pos[i]+dir) | ||
| + | # Kleine Beschleunigung, | ||
| + | naechste[i] = zeit + 100 - abs(ac[i]*10) | ||
| + | | ||
| + | # Nur wenn sich etwas geändert hat auch neu zeichnen. | ||
| + | # Arrays sind dann gleich (==) wenn alle Elemente gleich sind. | ||
| + | if pneu!=pos: | ||
| + | trailsave() | ||
| + | wegmit = zeit | ||
| + | loeschen() | ||
| + | pos[0], pos[1] = pneu[0], pneu[1] | ||
| + | zeichnen() | ||
| + | |||
| + | if zeit-wegmit> | ||
| + | wegmit=zeit | ||
| + | loeschen() | ||
| + | trailsave() | ||
| + | zeichnen() | ||
| + | | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <hidden Sand> | ||
| + | Hinweis: Auf Ihrer OxoCard könnte sich noch eine fehlerhafte ivobuttons-Bibliothek befinden. Laden Sie dazu die Datei install.py auf die OxoCard, siehe | ||
| + | <code python shaker.py> | ||
| + | from oxocard import * | ||
| + | from ivobuttons import * | ||
| + | from oxoaccelerometer import * | ||
| + | |||
| + | # Zustand vom Spiel | ||
| + | |||
| + | # Positionen und Farben vom Trail | ||
| + | n = 24 | ||
| + | pos = [[i%8,i//8] for i in range(n)] | ||
| + | farben = [(255-i*10, | ||
| + | |||
| + | # Beschleunigungssensor | ||
| + | acc = Accelerometer.create() | ||
| + | |||
| + | for i in range(8): | ||
| + | if i<4 or i>4: | ||
| + | fastDot(i, | ||
| + | |||
| + | # Funktionen zum Zeichnen und löschen des Spielzustands | ||
| + | |||
| + | def zeichnen(): | ||
| + | global pos, farben, | ||
| + | for i,p in enumerate(pos): | ||
| + | | ||
| + | fastRepaint() | ||
| + | | ||
| + | def loeschen(): | ||
| + | global pos, farben, | ||
| + | for i,p in enumerate(pos): | ||
| + | | ||
| + | | ||
| + | | ||
| + | # Begrenzt einen Wert zwischen 0 und 7 (damit man sicher auf dem Display bleibt). | ||
| + | def clip(v): | ||
| + | if v<0: | ||
| + | v=0 | ||
| + | if v>7: | ||
| + | v=7 | ||
| + | return v | ||
| + | | ||
| + | zeichnen() | ||
| + | |||
| + | pneu = [0,0] # Variable für neue Position | ||
| + | while True: | ||
| + | ac = acc.getValues() | ||
| + | dir = [0,0] | ||
| + | for c in range(2): | ||
| + | dir[c] = 1 # Richtung positiv | ||
| + | if ac[c]< | ||
| + | dir[c] = -1 | ||
| + | dir[1]=-dir[1] | ||
| + | |||
| + | for i in range(n): | ||
| + | x,y = pos[i][0], pos[i][1] | ||
| + | a,b = clip(x+dir[0]), | ||
| + | if a!=x or b!=y: | ||
| + | change = False | ||
| + | if fastGetDot(a, | ||
| + | change = True | ||
| + | elif (abs(a-x)+abs(b-y))==2: | ||
| + | if fastGetDot(a, | ||
| + | b=y | ||
| + | change = True | ||
| + | elif fastGetDot(x, | ||
| + | a=x | ||
| + | change = True | ||
| + | if change: | ||
| + | fastDot(x, | ||
| + | pos[i][0], pos[i][1] = a,b | ||
| + | fastDot(pos[i][0], | ||
| + | fastRepaint() | ||
| + | | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <hidden Shaker Tetris> | ||
| + | Versuchen Sie in der oberen Hälfte, vertikal die drei gleichen Farben hinzukriegen. Dieser verschwinden dann. Ziel ist es, alle Pixel zum Verschwinden zu bringen. | ||
| + | <code python shakertetris.py> | ||
| + | from oxocard import * | ||
| + | from ivobuttons import * | ||
| + | from oxoaccelerometer import * | ||
| + | |||
| + | # Zustand vom Spiel | ||
| + | |||
| + | # Positionen und Farben vom Trail | ||
| + | n = 24 | ||
| + | pos = [[i%8,i//8] for i in range(n)] | ||
| + | choice = [RED, YELLOW, MAGENTA, BLUE] | ||
| + | farben = [choice[(i// | ||
| + | |||
| + | # Beschleunigungssensor | ||
| + | acc = Accelerometer.create() | ||
| + | |||
| + | for i in range(8): | ||
| + | if i<4 or i>4: | ||
| + | fastDot(i, | ||
| + | |||
| + | # Funktionen zum Zeichnen und löschen des Spielzustands | ||
| + | |||
| + | def zeichnen(): | ||
| + | global pos, farben, | ||
| + | for i,p in enumerate(pos): | ||
| + | | ||
| + | fastRepaint() | ||
| + | | ||
| + | def loeschen(): | ||
| + | global pos, farben, | ||
| + | for i,p in enumerate(pos): | ||
| + | | ||
| + | | ||
| + | | ||
| + | # Begrenzt einen Wert zwischen 0 und 7 (damit man sicher auf dem Display bleibt). | ||
| + | def clip(v): | ||
| + | if v<0: | ||
| + | v=0 | ||
| + | if v>7: | ||
| + | v=7 | ||
| + | return v | ||
| + | | ||
| + | zeichnen() | ||
| + | |||
| + | pneu = [0,0] # Variable für neue Position | ||
| + | while True: | ||
| + | ac = acc.getValues() | ||
| + | dir = [0,0] | ||
| + | for c in range(2): | ||
| + | dir[c] = 1 # Richtung positiv | ||
| + | if ac[c]< | ||
| + | dir[c] = -1 | ||
| + | dir[1]=-dir[1] | ||
| + | |||
| + | p=0 | ||
| + | while p<n: | ||
| + | x,y = pos[p][0], pos[p][1] | ||
| + | a,b = clip(x+dir[0]), | ||
| + | if a!=x or b!=y: | ||
| + | change = False | ||
| + | if fastGetDot(a, | ||
| + | change = True | ||
| + | elif (abs(a-x)+abs(b-y))==2: | ||
| + | if fastGetDot(a, | ||
| + | b=y | ||
| + | change = True | ||
| + | elif fastGetDot(x, | ||
| + | a=x | ||
| + | change = True | ||
| + | if change: | ||
| + | fastDot(x, | ||
| + | pos[p][0], pos[p][1] = a,b | ||
| + | fastDot(pos[p][0], | ||
| + | fastRepaint() | ||
| + | for x in range(8): | ||
| + | f = fastGetDot(x, | ||
| + | if f!=BLACK and f==fastGetDot(x, | ||
| + | print(" | ||
| + | for flash in range(10): | ||
| + | for y in range(3): | ||
| + | fastDot(x, | ||
| + | fastRepaint() | ||
| + | sleep(0.1) | ||
| + | for y in range(3): | ||
| + | fastDot(x, | ||
| + | fastRepaint() | ||
| + | sleep(0.1) | ||
| + | | ||
| + | i=0 | ||
| + | while i<n: | ||
| + | if pos[i][0]==x and pos[i][1]< | ||
| + | fastDot(pos[i][0], | ||
| + | del pos[i] | ||
| + | del farben[i] | ||
| + | if i<=p: | ||
| + | p-=1 | ||
| + | n-=1 | ||
| + | else: | ||
| + | i+=1 | ||
| + | fastRepaint() | ||
| + | | ||
| + | p+=1 | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | </ | ||
| + | <hidden Labyrinth> | ||
| + | Durch Neigen der OxoCard gelangen Sie von der oberen linken Ecke in die untere rechte Ecke. | ||
| + | <code python laby.py> | ||
| + | from oxocard import * | ||
| + | from ivobuttons import * | ||
| + | from oxoaccelerometer import * | ||
| + | from random import randrange | ||
| + | |||
| + | |||
| + | # Gamestate als Dictionary, d.h. wie eine Liste aber mit Namen als Indizes | ||
| + | laby = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | def myArrayShuffle(a): | ||
| + | l = len(a) | ||
| + | for i in range(l): | ||
| + | j = randrange(i, | ||
| + | a[i],a[j] = a[j],a[i] | ||
| + | |||
| + | |||
| + | def generate(): | ||
| + | global laby | ||
| + | laby[' | ||
| + | todo = [[1,1]] | ||
| + | laby[' | ||
| + | dirs = ((1, | ||
| + | ind = [0,1,2,3] | ||
| + | while len(todo)> | ||
| + | index = max(len(todo)-1-int(randrange(101)**2/ | ||
| + | pos = todo[index] | ||
| + | del todo[index] | ||
| + | myArrayShuffle(ind) | ||
| + | for d in ind: | ||
| + | a,b = pos[0]+dirs[d][0], | ||
| + | if laby[' | ||
| + | aa,bb = a+dirs[d][0], | ||
| + | if laby[' | ||
| + | laby[' | ||
| + | laby[' | ||
| + | todo.append(pos) | ||
| + | todo.append((aa, | ||
| + | break | ||
| + | laby[' | ||
| + | laby[' | ||
| + | |||
| + | def dimmen(m, farben): | ||
| + | for i in range(len(farben)): | ||
| + | farben[i]=tuple(map(lambda x: | ||
| + | |||
| + | def zeichnen(m=255): | ||
| + | global laby | ||
| + | farben = [BLACK, RED, BLUE, YELLOW] | ||
| + | dimmen(m, | ||
| + | for x in range(8): | ||
| + | xx = laby[' | ||
| + | for y in range(8): | ||
| + | yy = laby[' | ||
| + | f = BLACK | ||
| + | if xx>=0 and xx< | ||
| + | f = farben[laby[' | ||
| + | fastDot(x, | ||
| + | fastDot(3, | ||
| + | fastRepaint() | ||
| + | | ||
| + | | ||
| + | enableRepaint(False) | ||
| + | |||
| + | acc = Accelerometer.create() | ||
| + | generate() | ||
| + | zeichnen() | ||
| + | |||
| + | dimmwerte = [0, | ||
| + | |||
| + | while True: | ||
| + | ac = acc.getValues() | ||
| + | zeit = getms() | ||
| + | pos = [laby[' | ||
| + | for i in range(2): | ||
| + | if abs(ac[i])> | ||
| + | dir = 1 # Richtung positiv | ||
| + | if ac[i]< | ||
| + | dir = -1 | ||
| + | if i==1: # Vorzeichenwechsel für y | ||
| + | dir = -dir | ||
| + | pos[i] += dir | ||
| + | # Kleine Beschleunigung, | ||
| + | laby[' | ||
| + | | ||
| + | if (pos[0]!=laby[' | ||
| + | laby[' | ||
| + | laby[' | ||
| + | zeichnen() | ||
| + | if (laby[' | ||
| + | for i in range(len(dimmwerte)-1, | ||
| + | zeichnen(dimmwerte[i]) | ||
| + | laby[' | ||
| + | generate() | ||
| + | for i in dimmwerte: | ||
| + | zeichnen(i) | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | </ | ||