Mercurial > rinkhals
annotate gamelib/animal.py @ 38:03121c89d5fd
Make the secret foxes really secret
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 13:28:59 +0000 |
parents | f5f74f1f3a0b |
children | 7e884084e7b1 |
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 import pygame |
29 | 4 import random |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
5 from pgu.vid import Sprite |
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 import data |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
8 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
9 class Animal(Sprite): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
10 """Base class for animals""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
11 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
12 def __init__(self, image, pos): |
38
03121c89d5fd
Make the secret foxes really secret
Neil Muller <drnlmuller@gmail.com>
parents:
32
diff
changeset
|
13 # Create the animal somewhere far off screen |
03121c89d5fd
Make the secret foxes really secret
Neil Muller <drnlmuller@gmail.com>
parents:
32
diff
changeset
|
14 Sprite.__init__(self, image, (-1000, -1000)) |
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): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
31 image = pygame.image.load(data.filepath('sprites/chkn.png')) |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
32 Animal.__init__(self, image, pos) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
33 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
34 def move(self, gameboard): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
35 """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
|
36 return self.pos |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
37 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
38 class Egg(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
39 """An egg""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
40 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
41 # Eggs don't move |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
42 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
43 class Fox(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
44 """A fox""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
45 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
46 def __init__(self, pos): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
47 image = pygame.image.load(data.filepath('sprites/fox.png')) |
29 | 48 self.full = False |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
49 Animal.__init__(self, image, pos) |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
50 |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
51 def move(self, gameboard): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
52 """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
|
53 chicken""" |
29 | 54 if self.full: |
55 return | |
56 # Find the closest chicken | |
57 min_dist = 999 | |
58 min_vec = None | |
59 closest = None | |
60 for chicken in gameboard.chickens: | |
61 vec = (chicken.pos[0] - self.pos[0], chicken.pos[1] - self.pos[1]) | |
62 dist = abs(vec[0]) + abs(vec[1]) | |
63 if dist < min_dist: | |
64 min_dist = dist | |
65 min_vec = vec | |
66 closest = chicken | |
67 xpos, ypos = self.pos | |
68 if min_vec[0] < 0: | |
69 xpos -= 1 | |
70 elif min_vec[0] > 0: | |
71 xpos += 1 | |
72 if min_vec[1] < 0: | |
73 ypos -= 1 | |
74 elif min_vec[1] > 0: | |
75 ypos += 1 | |
76 if closest.pos == self.pos: | |
77 gameboard.remove_chicken(closest) | |
78 self.full = True | |
32
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
79 for fox in gameboard.foxes: |
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
80 if fox is not self: |
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
81 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
|
82 if xpos != self.pos[0]: |
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
83 xpos = self.pos[0] |
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
84 elif ypos != self.pos[1]: |
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
85 ypos = self.pos[1] |
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
86 else: # We move a step away |
f5f74f1f3a0b
Discourage foxes from stepping on each other
Neil Muller <drnlmuller@gmail.com>
parents:
29
diff
changeset
|
87 xpos += 1 |
29 | 88 self.pos = (xpos, ypos) |
89 | |
90 |