aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--turtetris_master/__init__.py12
-rw-r--r--turtetris_master/game.py10
-rw-r--r--turtetris_master/screen_checker.py7
-rw-r--r--turtetris_master/state_machine.py52
-rw-r--r--turtetris_slave/__init__.py1
5 files changed, 66 insertions, 16 deletions
diff --git a/turtetris_master/__init__.py b/turtetris_master/__init__.py
index e7f9fab..b389525 100644
--- a/turtetris_master/__init__.py
+++ b/turtetris_master/__init__.py
@@ -1,23 +1,17 @@
import time
from .led_output import Matrix
from .usb_input import Gamepad
-from .screen_checker import ScreenChecker
+from .state_machine import StateMachine
def main():
"Main function"
inpt = Gamepad()
mtrx = Matrix()
- sc = ScreenChecker(mtrx)
- mtrx.display() # Display first state
+ sm = StateMachine(mtrx, inpt)
while True:
tstart = time.time()
- ##
- #print(inpt.check())
- if sc.tick():
- mtrx.display()
- #print(mtrx.__mat__[2])
- ##
+ sm.tick()
trest = (1/60) - (time.time() - tstart)
if trest > 0:
time.sleep(trest)
diff --git a/turtetris_master/game.py b/turtetris_master/game.py
new file mode 100644
index 0000000..9b0c7cf
--- /dev/null
+++ b/turtetris_master/game.py
@@ -0,0 +1,10 @@
+class Game:
+ "game it self"
+
+ def __init__(self, matrix):
+ self.matrix = matrix
+ pass
+
+ def tick(self, input):
+ "Do game tick"
+ return True
diff --git a/turtetris_master/screen_checker.py b/turtetris_master/screen_checker.py
index d8ed327..508db1b 100644
--- a/turtetris_master/screen_checker.py
+++ b/turtetris_master/screen_checker.py
@@ -12,6 +12,7 @@ class ScreenChecker:
self.__pos__[i][1] = i
for y in range(0, i):
matrix.pixel(i, y, 'green')
+ matrix.display()
def __pos_inc__(self, p):
p += 1
@@ -21,13 +22,11 @@ class ScreenChecker:
def tick(self):
self.__tick__ += 1
- if self.__tick__ > 60:
+ if self.__tick__ > 15:
self.__tick__ = 0
for i in range(0, self.matrix.height):
self.matrix.pixel(self.__pos__[i][0], i, '000000')
self.__pos__[i][0] = self.__pos_inc__(self.__pos__[i][0])
self.__pos__[i][1] = self.__pos_inc__(self.__pos__[i][1])
self.matrix.pixel(self.__pos__[i][1], i, color='green')
- return True
- else:
- return False
+ self.matrix.display()
diff --git a/turtetris_master/state_machine.py b/turtetris_master/state_machine.py
index 880a582..cef03e4 100644
--- a/turtetris_master/state_machine.py
+++ b/turtetris_master/state_machine.py
@@ -1,4 +1,52 @@
-#class StateMachine:
- #"Game state machine"
+from .screen_checker import ScreenChecker
+from .game import Game
+class StateMachine:
+ "Game state machine"
+
+ def __init__(self, matrix, input):
+ "Initializes game machine"
+ self.state = "initializing"
+ self.matrix = matrix
+ self.input = input
+ self.__update_state__('screen_checker')
+
+ def __update_state__(self, state):
+ "Applies given state"
+ def __exception__():
+ raise Exception('Can\'t transfer from ' + self.state + ' to '
+ + state)
+ if state == "screen_checker":
+ if self.state == "initializing":
+ self.screen_checker = ScreenChecker(self.matrix)
+ else:
+ __exception__()
+ elif state == "game":
+ if self.state == "screen_checker" or self.state == "game-over":
+ self.game = Game(self.matrix)
+ else:
+ __exception__()
+ elif state == "game-over":
+ if self.state != "game":
+ __exception__()
+ else:
+ __exception__()
+ self.state = state
+
+ def tick(self):
+ "Do tick"
+ inpt = self.input.check()
+ if self.state == "screen_checker":
+ if inpt['start']:
+ self.__update_state__('game')
+ else:
+ self.screen_checker.tick()
+ elif self.state == "game":
+ if not self.game.tick(inpt):
+ self.__update_state__('game-over')
+ elif self.state == "game-over":
+ if inpt['start']:
+ self.__update_state__('game')
+ else:
+ raise Exception('Invalid state ' + self.state)
diff --git a/turtetris_slave/__init__.py b/turtetris_slave/__init__.py
index d5dd129..ea75ad0 100644
--- a/turtetris_slave/__init__.py
+++ b/turtetris_slave/__init__.py
@@ -22,7 +22,6 @@ def main():
leds.prepare()
while True:
msg = sck.recv_string()
- print(msg)
json0 = msg.find(' ') # First empty char is end of envelope
leds.output(json.loads(msg[json0:]))