Mercurial > rinkhals
comparison gamelib/animal.py @ 84:5494af02a0e8
Chickens with rifles!
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Tue, 01 Sep 2009 21:31:08 +0000 |
parents | bf28f499c6b4 |
children | bea1b9364583 |
comparison
equal
deleted
inserted
replaced
83:9bd2c22e1746 | 84:5494af02a0e8 |
---|---|
1 """Class for the various animals in the game""" | 1 """Class for the various animals in the game""" |
2 | |
3 import random | |
2 | 4 |
3 from pgu.vid import Sprite | 5 from pgu.vid import Sprite |
4 from pgu.algo import getline | 6 from pgu.algo import getline |
5 | 7 |
6 import imagecache | 8 import imagecache |
14 # Create the animal somewhere far off screen | 16 # Create the animal somewhere far off screen |
15 Sprite.__init__(self, image_left, (-1000, -1000)) | 17 Sprite.__init__(self, image_left, (-1000, -1000)) |
16 self.image_left = image_left | 18 self.image_left = image_left |
17 self.image_right = image_right | 19 self.image_right = image_right |
18 self.pos = Position(tile_pos[0], tile_pos[1]) | 20 self.pos = Position(tile_pos[0], tile_pos[1]) |
21 self.equipment = [] | |
19 | 22 |
20 def loop(self, tv, _sprite): | 23 def loop(self, tv, _sprite): |
21 ppos = tv.tile_to_view(self.pos.to_tuple()) | 24 ppos = tv.tile_to_view(self.pos.to_tuple()) |
22 self.rect.x = ppos[0] | 25 self.rect.x = ppos[0] |
23 self.rect.y = ppos[1] | 26 self.rect.y = ppos[1] |
32 if final_pos.left_of(self.pos): | 35 if final_pos.left_of(self.pos): |
33 self.setimage(self.image_left) | 36 self.setimage(self.image_left) |
34 elif final_pos.right_of(self.pos): | 37 elif final_pos.right_of(self.pos): |
35 self.setimage(self.image_right) | 38 self.setimage(self.image_right) |
36 | 39 |
40 def equip(self, item): | |
41 self.equipment.append(item) | |
42 | |
43 def weapons(self): | |
44 return [e for e in self.equipment if e.is_weapon] | |
45 | |
37 class Chicken(Animal): | 46 class Chicken(Animal): |
38 """A chicken""" | 47 """A chicken""" |
39 | 48 |
40 def __init__(self, pos): | 49 def __init__(self, pos): |
41 image_left = imagecache.load_image('sprites/chkn.png') | 50 image_left = imagecache.load_image('sprites/chkn.png') |
44 Animal.__init__(self, image_left, image_right, pos) | 53 Animal.__init__(self, image_left, image_right, pos) |
45 | 54 |
46 def move(self, gameboard): | 55 def move(self, gameboard): |
47 """A free chicken will move away from other free chickens""" | 56 """A free chicken will move away from other free chickens""" |
48 pass | 57 pass |
58 | |
59 def _find_killable_fox(self, weapon, gameboard): | |
60 """Choose a random fox within range of this weapon.""" | |
61 killable_foxes = [] | |
62 for fox in gameboard.foxes: | |
63 if weapon.in_range(gameboard, self, fox): | |
64 killable_foxes.append(fox) | |
65 if not killable_foxes: | |
66 return None | |
67 return random.choice(killable_foxes) | |
68 | |
69 def attack(self, gameboard): | |
70 """An armed chicken will attack a fox within range.""" | |
71 if not self.weapons(): | |
72 # Not going to take on a fox bare-winged. | |
73 return | |
74 # Choose the first weapon equipped. | |
75 weapon = self.weapons()[0] | |
76 fox = self._find_killable_fox(weapon, gameboard) | |
77 if not fox: | |
78 return | |
79 if weapon.hit(gameboard, self, fox): | |
80 gameboard.kill_fox(fox) | |
49 | 81 |
50 class Egg(Animal): | 82 class Egg(Animal): |
51 """An egg""" | 83 """An egg""" |
52 | 84 |
53 def __init__(self, pos): | 85 def __init__(self, pos): |