From 89e09de5e12744f9435b6f31fa2e9f7116fc6347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Wed, 19 Jul 2017 14:58:24 +0200 Subject: Add statemachine and empty game class --- turtetris_master/__init__.py | 12 +++------ turtetris_master/game.py | 10 ++++++++ turtetris_master/screen_checker.py | 7 +++-- turtetris_master/state_machine.py | 52 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 turtetris_master/game.py (limited to 'turtetris_master') 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) -- cgit v1.2.3