diff gamelib/animal.py @ 422:ab4fc3fe0f96

chickens scatter; chop wood
author Adrianna Pińska <adrianna.pinska@gmail.com>
date Sat, 21 Nov 2009 15:35:23 +0000
parents f20ccd43a282
children 97dd557504a2
line wrap: on
line diff
--- a/gamelib/animal.py	Sat Nov 21 15:24:56 2009 +0000
+++ b/gamelib/animal.py	Sat Nov 21 15:35:23 2009 +0000
@@ -178,8 +178,22 @@
         gameboard.remove_chicken(self)
 
     def move(self, gameboard):
-        """A free chicken will move away from other free chickens"""
-        pass
+        """A free chicken will wander around aimlessly"""
+        pos_x, pos_y = self.pos.to_tile_tuple()
+        surrounds = [Position(pos_x + dx, pos_y + dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]]
+        pos_options = [pos for pos in surrounds if gameboard.in_bounds(pos) and gameboard.tv.get(pos.to_tile_tuple()) == gameboard.GRASSLAND and not gameboard.get_outside_chicken(pos.to_tile_tuple())] + [self.pos]
+        self.pos = pos_options[random.randint(0, len(pos_options)-1)]
+
+    def chop(self, gameboard):
+        pos_x, pos_y = self.pos.to_tile_tuple()
+        surrounds = [Position(pos_x + dx, pos_y + dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]]
+        tree_options = [pos for pos in surrounds if gameboard.in_bounds(pos) and gameboard.tv.get(pos.to_tile_tuple()) == gameboard.WOODLAND]
+        if tree_options:
+            num_trees_to_cut = random.randint(0, len(tree_options)-1)
+            trees_to_cut = random.sample(tree_options, num_trees_to_cut)
+            for tree_pos in trees_to_cut:
+                gameboard.add_wood(5)
+                gameboard.tv.set(tree_pos.to_tile_tuple(), gameboard.GRASSLAND)
 
     def lay(self, gameboard):
         """See if the chicken lays an egg"""