diff options
author | Karel Kočí <karel.koci@nic.cz> | 2018-01-30 16:36:54 +0100 |
---|---|---|
committer | Karel Kočí <karel.koci@nic.cz> | 2018-01-30 16:36:54 +0100 |
commit | e027b3020140fa4d9a599521d2ab49d0578b85bd (patch) | |
tree | 3e4b080abf5e95baa3e2617213ddd563d2ec470a /turtetris_master/state_machine.py | |
parent | 1dae42389b97b27e6c11d5bb093270e07e6fe05e (diff) | |
download | turris-tetris-e027b3020140fa4d9a599521d2ab49d0578b85bd.tar.gz turris-tetris-e027b3020140fa4d9a599521d2ab49d0578b85bd.tar.bz2 turris-tetris-e027b3020140fa4d9a599521d2ab49d0578b85bd.zip |
Add replay mode
Diffstat (limited to 'turtetris_master/state_machine.py')
-rw-r--r-- | turtetris_master/state_machine.py | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/turtetris_master/state_machine.py b/turtetris_master/state_machine.py index 7cbfcaa..e0cc6ee 100644 --- a/turtetris_master/state_machine.py +++ b/turtetris_master/state_machine.py @@ -1,5 +1,10 @@ from .screen_checker import ScreenChecker from .game import Game +from .recorder import Recorder +from .recorder import Replayer + +# We run 1/30 loops per second so this gives us one minute delay +SWITCH_TIME_DELAY = 1800 class StateMachine: @@ -10,21 +15,43 @@ class StateMachine: self.state = "initializing" self.matrix = matrix self.input = input - self.__update_state__('screen_checker') + self.game = None + self.recorder = None + self.replayer = None + self.timeout = SWITCH_TIME_DELAY + self.__update_state__('screen-checker') + + def __new_game__(self): + "Initialize new game" + self.game = Game(self.matrix) + self.recorder = Recorder(self.matrix) + + def __reset_delay__(self): + self.timeout = SWITCH_TIME_DELAY + + def __delay__(self): + if self.timeout > 0: + self.timeout -= 1 + return False + else: + self.__reset_delay__() + return True def __update_state__(self, state): "Applies given state" def __exception__(): - raise Exception('Can\'t transfer from ' + self.state + ' to ' - + state) - if state == "screen_checker": + 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) + if self.state == "screen-checker" or \ + self.state == "game-over" or \ + self.state == "replay": + self.__new_game__() elif self.state == "game-pause": pass else: @@ -35,6 +62,8 @@ class StateMachine: elif state == "game-over": if self.state != "game": __exception__() + self.__reset_delay__() + self.recorder.store(self.game.score) self.matrix.fill('red') i = self.matrix.height - 2 # Show score @@ -45,6 +74,10 @@ class StateMachine: for s in range(self.game.score % 5): self.matrix.pixel(s + 3, i, 'green') self.matrix.display() + elif state == "replay": + if self.state != "game-over" and self.state != "screen-checker": + __exception__() + self.replayer = Replayer(self.matrix) else: __exception__() self.state = state @@ -52,12 +85,17 @@ class StateMachine: def tick(self): "Do tick" inpt = self.input.check() - if self.state == "screen_checker": + if self.state == "screen-checker": if inpt['start']: self.__update_state__('game') + if inpt['select']: + self.__update_state__('replay') else: self.screen_checker.tick() elif self.state == "game": + # Note: This records previous tick output but that doens't matter + # as loosing latest frame is harmless + self.recorder.tick() if inpt['start']: self.__update_state__('game-pause') elif not self.game.tick(inpt): @@ -66,10 +104,17 @@ class StateMachine: if inpt['start']: self.__update_state__('game') elif inpt['select']: - self.game = Game(self.matrix) + self.__new_game__() self.__update_state__('game') elif self.state == "game-over": if inpt['start']: self.__update_state__('game') + elif self.__delay__(): + self.__update_state__('replay') + elif self.state == "replay": + if inpt['start']: + self.__update_state__('game') + elif not self.replayer.tick(): + self.replayer = Replayer(self.matrix) else: raise Exception('Invalid state ' + self.state) |