Mercurial > rinkhals
comparison gamelib/animal.py @ 29:2e88c680672c
Minimal fox raid logic
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 30 Aug 2009 18:46:46 +0000 |
parents | ac3a74352b74 |
children | f5f74f1f3a0b |
comparison
equal
deleted
inserted
replaced
28:ac3a74352b74 | 29:2e88c680672c |
---|---|
1 """Class for the various animals in the game""" | 1 """Class for the various animals in the game""" |
2 | 2 |
3 import pygame | 3 import pygame |
4 import random | |
4 from pgu.vid import Sprite | 5 from pgu.vid import Sprite |
5 | 6 |
6 import data | 7 import data |
7 | 8 |
8 class Animal(Sprite): | 9 class Animal(Sprite): |
41 class Fox(Animal): | 42 class Fox(Animal): |
42 """A fox""" | 43 """A fox""" |
43 | 44 |
44 def __init__(self, pos): | 45 def __init__(self, pos): |
45 image = pygame.image.load(data.filepath('sprites/fox.png')) | 46 image = pygame.image.load(data.filepath('sprites/fox.png')) |
47 self.full = False | |
46 Animal.__init__(self, image, pos) | 48 Animal.__init__(self, image, pos) |
47 | 49 |
48 def move(self, gameboard): | 50 def move(self, gameboard): |
49 """Foxes will aim to move towards the closest henhouse or free | 51 """Foxes will aim to move towards the closest henhouse or free |
50 chicken""" | 52 chicken""" |
51 return self.pos | 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 |