annotate gamelib/animal.py @ 53:f20dd3dcb118

foxes don't run backwards
author Adrianna Pińska <adrianna.pinska@gmail.com>
date Mon, 31 Aug 2009 17:47:10 +0000
parents be2496df2368
children d92a2f973cc4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Class for the various animals in the game"""
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 from pgu.vid import Sprite
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4
44
7e884084e7b1 Move animal sprites to imagecache.
Simon Cross <hodgestar@gmail.com>
parents: 38
diff changeset
5 import imagecache
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7 class Animal(Sprite):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
8 """Base class for animals"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
10 def __init__(self, image_left, image_right, pos):
38
03121c89d5fd Make the secret foxes really secret
Neil Muller <drnlmuller@gmail.com>
parents: 32
diff changeset
11 # Create the animal somewhere far off screen
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
12 Sprite.__init__(self, image_left, (-1000, -1000))
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
13 self.image_left = image_left
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
14 self.image_right = image_right
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
15 self.pos = pos
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
17 def loop(self, tv, _sprite):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
18 ppos = tv.tile_to_view(self.pos)
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
19 self.rect.x = ppos[0]
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
20 self.rect.y = ppos[1]
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
22 def move(self, state):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
23 """Given the game state, return a new position for the object"""
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
24 # Default is not to move
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
25 return self.pos
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
27 class Chicken(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
28 """A chicken"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
30 def __init__(self, pos):
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
31 image_left = imagecache.load_image('sprites/chkn.png')
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
32 image_right = imagecache.load_image('sprites/chkn.png', ("right_facing",))
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
33 Animal.__init__(self, image_left, image_right, pos)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
35 def move(self, gameboard):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
36 """A free chicken will move away from other free chickens"""
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
37 return self.pos
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 class Egg(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
40 """An egg"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41
47
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
42 def __init__(self, pos):
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
43 image = imagecache.load_image('sprites/egg.png')
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
44 Animal.__init__(self, image, image, pos)
47
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
45
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
46 # Eggs don't move
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
47
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48 class Fox(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
49 """A fox"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
51 def __init__(self, pos):
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
52 image_left = imagecache.load_image('sprites/fox.png')
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
53 image_right = imagecache.load_image('sprites/fox.png', ("right_facing",))
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
54 self.full = False
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
55 Animal.__init__(self, image_left, image_right, pos)
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
56
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
57 def move(self, gameboard):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
58 """Foxes will aim to move towards the closest henhouse or free
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
59 chicken"""
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
60 if self.full:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
61 return
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
62 # Find the closest chicken
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
63 min_dist = 999
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
64 min_vec = None
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
65 closest = None
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
66 for chicken in gameboard.chickens:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
67 vec = (chicken.pos[0] - self.pos[0], chicken.pos[1] - self.pos[1])
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
68 dist = abs(vec[0]) + abs(vec[1])
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
69 if dist < min_dist:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
70 min_dist = dist
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
71 min_vec = vec
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
72 closest = chicken
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
73 xpos, ypos = self.pos
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
74 if min_vec[0] < 0:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
75 xpos -= 1
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
76 self.setimage(self.image_left)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
77 elif min_vec[0] > 0:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
78 xpos += 1
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
79 self.setimage(self.image_right)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
80 if min_vec[1] < 0:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
81 ypos -= 1
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
82 elif min_vec[1] > 0:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
83 ypos += 1
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
84 if closest.pos == self.pos:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
85 gameboard.remove_chicken(closest)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
86 self.full = True
32
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
87 for fox in gameboard.foxes:
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
88 if fox is not self:
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
89 if fox.pos[0] == xpos and fox.pos[1] == ypos:
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
90 if xpos != self.pos[0]:
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
91 xpos = self.pos[0]
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
92 elif ypos != self.pos[1]:
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
93 ypos = self.pos[1]
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
94 else: # We move a step away
f5f74f1f3a0b Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents: 29
diff changeset
95 xpos += 1
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
96 self.pos = (xpos, ypos)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
97
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 28
diff changeset
98