Пример #1
0
 def __init__(self, warrior1, warrior2):
     self.warrior1 = warrior1
     self.warrior2 = warrior2
     self.prev_action1 = IdleAction()
     self.prev_action2 = IdleAction()
Пример #2
0
class GameController:

    def __init__(self, warrior1, warrior2):
        self.warrior1 = warrior1
        self.warrior2 = warrior2
        self.prev_action1 = IdleAction()
        self.prev_action2 = IdleAction()

    def turn_around(self, state1, state2):
        if state2.position.y != 195 or state1.position.y != 195:
            return

        if state1.facing_right and state2.position.x < state1.position.x:
            state1.facing_right = False
            state2.facing_right = True
        elif not state1.facing_right and state2.position.x > state1.position.x:
            state1.facing_right = True
            state2.facing_right = False

    def check_boundaries(self, state1, state2):
        for state in [state1, state2]:
            if state.position.x < 10: 
                state.position.x = 10
            elif state.position.x > 310:
                state.position.x = 310

    def attack(self, action1, state1, action2, state2):
        a2 = action2
        if (action1.attack()):
            hit_rect = action1.hit_rect(state1)
            warrior_rect = state2.get_rect()

            if hit_rect.collide(warrior_rect):
                action1.hit = True
                if state2.state != STATES.STATE_BLOCK:
                    state2.health -= action1.damage
                    if action1.damage <= 10:
                        a2 = a2.combine(InjureAction())
                    else:
                        a2 = a2.combine(FallAction())
        return a2

    def apply_action(self, action1, state1, action2, state2):
        action1 = self.prev_action1.combine(action1)
        action2 = self.prev_action2.combine(action2)

        action1.apply(state1)
        action2.apply(state2)

        a2 = self.attack(action1, state1, action2, state2)
        a1 = self.attack(action2, state2, action1, state1)

        self.prev_action1 = a1
        self.prev_action2 = a2

        self.check_boundaries(state1, state2)
        self.turn_around(state1, state2)


    def mainloop(self):
        done = False
        while not done:
            action1 = self.warrior1.next_action(self.warrior2.state)
            action2 = self.warrior2.next_action(self.warrior1.state)
            self.apply_action(action1, warrior1.state, action2, warrior2.state)
            if warrior1.state.health <= 0 or warrior2.state.health <= 0:
                done = True