aboutsummaryrefslogtreecommitdiff
path: root/turtetris_master/state_machine.py
diff options
context:
space:
mode:
authorKarel Kočí <karel.koci@nic.cz>2017-07-19 14:58:24 +0200
committerKarel Kočí <karel.koci@nic.cz>2017-07-19 15:05:43 +0200
commit89e09de5e12744f9435b6f31fa2e9f7116fc6347 (patch)
tree46c91fa193478a3b935110caeabd50ea42a9dd26 /turtetris_master/state_machine.py
parentb934596cfd5415ce83b5a7b42120e0f0e229937a (diff)
downloadturris-tetris-89e09de5e12744f9435b6f31fa2e9f7116fc6347.tar.gz
turris-tetris-89e09de5e12744f9435b6f31fa2e9f7116fc6347.tar.bz2
turris-tetris-89e09de5e12744f9435b6f31fa2e9f7116fc6347.zip
Add statemachine and empty game class
Diffstat (limited to 'turtetris_master/state_machine.py')
-rw-r--r--turtetris_master/state_machine.py52
1 files changed, 50 insertions, 2 deletions
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)