class AiPlayer(Player): def __init__(self, position, team, number): super(AiPlayer, self).__init__(position, team, number) del self.keys # part of Player, but AiPlayer doesn't need this self.statemachine = Statemachine(self, {'move': MoveState(), 'jump': JumpState(), 'prone': ProneState()}) def on_key_press(self, symbol, modifier): pass # don't respond to key presses def on_key_release(self, symbol, modifier): pass # don't respond to key presses def tick(self): super(AiPlayer, self).tick() self.statemachine.tick() def collided_with(self, other, description=None, details=None): super(AiPlayer, self).collided_with(other, description, details) self.statemachine.state.on_collided_with(other, description, details) def is_enemy_bullet_approaching(self): """still a rather simplistic approach ... """ if not hasattr(self, 'gamestate'): # can't take existence of gamestate for granted! return False bullets = self.gamestate.get_all(Bullet) x_pos = self.boundingbox.center_x for b in bullets: if b.owner.team == self.team: continue # not an enemy bullet distance = abs(x_pos - b.boundingbox.center_x) if distance > 24: continue # not dangerous yet fly_left = b.velocity < 0 to_right = b.boundingbox.center_x > x_pos if (fly_left and to_right) or ((not fly_left) and (not to_right)): return b # OMG a bullet is closing in! return False # when we end up here, no dangerous bullets were found def is_goodie_present(self): if not hasattr(self, 'gamestate'): return False goodies = self.gamestate.get_all(Itemcrate) return len(goodies) > 0 def is_nearest_goodie_to_the_left(self): if not hasattr(self, 'gamestate'): return None goodies = self.gamestate.get_all(Itemcrate) min_distance = -1 ret_val = None x_pos = self.boundingbox.center_x for g in goodies: distance = abs(x_pos - g.boundingbox.center_x) if (min_distance < 0) or (distance < min_distance): min_distance = distance ret_val = g.boundingbox.center_x < x_pos return ret_val
def __init__(self, position, team, number): super(AiPlayer, self).__init__(position, team, number) del self.keys # part of Player, but AiPlayer doesn't need this self.statemachine = Statemachine(self, {'move': MoveState(), 'jump': JumpState(), 'prone': ProneState()})