Mercurial > rinkhals
annotate gamelib/animal.py @ 31:3c4db7bba432
Add 's' as a key for starting the game from the menu.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 30 Aug 2009 18:56:09 +0000 |
parents | 2e88c680672c |
children | f5f74f1f3a0b |
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): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
13 Sprite.__init__(self, image, pos) |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
14 self.pos = pos |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
15 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
16 def loop(self, tv, _sprite): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
17 ppos = tv.tile_to_view(self.pos) |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
18 self.rect.x = ppos[0] |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
19 self.rect.y = ppos[1] |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
20 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
21 def move(self, state): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
22 """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
|
23 # Default is not to move |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
24 return self.pos |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
25 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
26 class Chicken(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
27 """A chicken""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
28 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
29 def __init__(self, pos): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
30 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
|
31 Animal.__init__(self, image, pos) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
32 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
33 def move(self, gameboard): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
34 """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
|
35 return self.pos |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
36 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
37 class Egg(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
38 """An egg""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
39 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
40 # Eggs don't move |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
41 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
42 class Fox(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
43 """A fox""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
44 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
45 def __init__(self, pos): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
46 image = pygame.image.load(data.filepath('sprites/fox.png')) |
29 | 47 self.full = False |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
48 Animal.__init__(self, image, pos) |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
49 |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
50 def move(self, gameboard): |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
51 """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
|
52 chicken""" |
29 | 53 if self.full: |
54 return | |
55 # Find the closest chicken | |
56 min_dist = 999 | |
57 min_vec = None | |
58 closest = None | |
59 for chicken in gameboard.chickens: | |
60 vec = (chicken.pos[0] - self.pos[0], chicken.pos[1] - self.pos[1]) | |
61 dist = abs(vec[0]) + abs(vec[1]) | |
62 if dist < min_dist: | |
63 min_dist = dist | |
64 min_vec = vec | |
65 closest = chicken | |
66 xpos, ypos = self.pos | |
67 if min_vec[0] < 0: | |
68 xpos -= 1 | |
69 elif min_vec[0] > 0: | |
70 xpos += 1 | |
71 if min_vec[1] < 0: | |
72 ypos -= 1 | |
73 elif min_vec[1] > 0: | |
74 ypos += 1 | |
75 if closest.pos == self.pos: | |
76 gameboard.remove_chicken(closest) | |
77 self.full = True | |
78 self.pos = (xpos, ypos) | |
79 | |
80 |