aboutsummaryrefslogtreecommitdiff
path: root/turtetris_master/state_machine.py
blob: cef03e460e4c3935f5dc0fcc162050cbe3ee81fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)