Changeset 92:bea1b9364583


Ignore:
Timestamp:
Sep 2, 2009, 10:47:46 AM (11 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@93
Message:

Refactor Fox so we can have different types. Add a greedy fox

Location:
gamelib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r84 r92  
    195195        if closest.pos == self.pos:
    196196            # Caught a chicken
    197             gameboard.remove_chicken(closest)
    198             self.hunting = False
     197            self._catch_chicken(closest, gameboard)
    199198            return self.pos
    200199        return self._find_best_path_step(closest.pos, gameboard)
     200
     201    def _catch_chicken(self, chicken, gameboard):
     202        """Catch a chicken"""
     203        gameboard.remove_chicken(chicken)
     204        self.hunting = False
    201205
    202206    def _update_pos(self, gameboard, new_pos):
     
    229233            self.landmarks.append(final_pos)
    230234        elif tiles.TILE_MAP[this_tile] == 'fence' and not self.dig_pos:
    231             self.tick = 5
    232             self.dig_pos = final_pos
     235            self._dig(final_pos)
    233236            return self.pos
    234237        return final_pos
     238
     239    def _dig(self, dig_pos):
     240        """Setup dig parameters, to be overridden if needed"""
     241        self.tick = 5
     242        self.dig_pos = dig_pos
     243
     244    def _make_hole(self, gameboard):
     245        """Make a hole in the fence"""
     246        gameboard.tv.set(self.dig_pos.to_tuple(),
     247                tiles.REVERSE_TILE_MAP['broken fence'])
     248        self.dig_pos = None
    235249
    236250    def move(self, gameboard):
     
    239253        if self.dig_pos:
    240254            if self.tick:
    241                 # We're digging through the fence
    242255                self.tick -= 1
     256                # We're still digging through the fence
    243257                # Check the another fox hasn't dug a hole for us
    244                 # We're top busy digging to notice if a hole appears nearby,
     258                # We're too busy digging to notice if a hole appears nearby,
    245259                # but we'll notice if the fence we're digging vanishes
    246260                this_tile = gameboard.tv.get(self.dig_pos.to_tuple())
    247261                if tiles.TILE_MAP[this_tile] == 'broken fence':
    248262                    self.tick = 0
     263                return
    249264            else:
    250265                # We've dug through the fence, so make a hole
    251                 gameboard.tv.set(self.dig_pos.to_tuple(),
    252                         tiles.REVERSE_TILE_MAP['broken fence'])
    253                 self.dig_pos = None
     266                self._make_hole(gameboard)
    254267            return
    255268        if self.hunting:
     
    260273        self._fix_face(final_pos)
    261274        self.pos = final_pos
    262        
    263            
     275
     276class NinjaFox(Fox):
     277    """Ninja foxes are hard to see"""
     278
     279class DemoFox(Fox):
     280    """Demolition Foxes destroy fences easily"""
     281
     282class GreedyFox(Fox):
     283    """Greedy foxes eat more chickens"""
     284
     285    def __init__(self, pos):
     286        Fox.__init__(self, pos)
     287        self.chickens_eaten = 0
     288
     289    def _catch_chicken(self, chicken, gameboard):
     290        gameboard.remove_chicken(chicken)
     291        self.chickens_eaten += 1
     292        if self.chickens_eaten > 2:
     293            self.hunting = False
  • gamelib/gameboard.py

    r84 r92  
    375375                    break
    376376            if not skip:
    377                 fox = animal.Fox((x, y))
     377                roll = random.randint(0, 10)
     378                if roll < 9:
     379                    fox = animal.Fox((x, y))
     380                else:
     381                    fox = animal.GreedyFox((x, y))
    378382                self.add_fox(fox)
    379383
Note: See TracChangeset for help on using the changeset viewer.