Remove debugging print. Standardise validation behaviour
[erdslangetjie.git] / erdslangetjie / player.py
1 # Player and Player-like objects
2
3 from erdslangetjie.data import load_image
4
5
6 class GameSprite(object):
7
8     def __init__(self):
9         self.pos = (0, 0)
10         self.sprite = None
11
12     def get_image(self):
13         return self.sprite
14
15     def get_texture(self):
16         return self.sprite.texture
17
18
19 class FigureSprite(GameSprite):
20
21     def can_move(self, direction, level):
22         '''Check if we can move in the given direction'''
23         cand_pos = (self.pos[0] + direction[0], self.pos[1] + direction[1])
24         return not level.blocked(cand_pos)
25
26
27 class ThePlayer(FigureSprite):
28
29     def __init__(self):
30         super(ThePlayer, self).__init__()
31         self.sprite = load_image('sprites/player.png')
32
33     def move(self, direction, level):
34         if self.can_move(direction, level):
35             self.pos = (self.pos[0] + direction[0], self.pos[1] + direction[1])
36             return True
37         return False
38
39
40 class Nemesis(FigureSprite):
41
42     def __init__(self):
43         super(Nemesis, self).__init__()
44         self.sprite = load_image('sprites/nemesis.png')
45         self.reset_pos()
46         self.been = []
47
48     def move(self, level, caught):
49         if not self.on_board():
50             # Advance towards the map a step at a time
51             self.pos = (self.pos[0] + 1, self.pos[1])
52             if self.on_board():
53                 self.pos = level.enter_pos
54             return False
55         # AI goes here
56         steps = 0
57         self.been.append(self.pos)
58         for direction in [(1, 0), (-1, 0), (0, -1), (0, 1)] * 2:
59             new_pos = (self.pos[0] + direction[0], self.pos[1] + direction[1])
60             if self.can_move(direction, level) and new_pos not in self.been:
61                 self.pos = new_pos
62                 self.been.append(new_pos)
63                 steps += 1
64             if steps >= 2:
65                 break
66             if caught():
67                 break
68         if steps == 0:
69             self.been = []
70         return True
71
72     def reset_pos(self):
73         self.pos = (-10, 0)
74
75     def on_board(self):
76         return self.pos[0] >= 0