Changeset 116:d539ef5a3333


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

Add basic chicken->egg cycle

Location:
gamelib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r115 r116  
    6666                ("right_facing",))
    6767        Animal.__init__(self, image_left, image_right, pos)
     68        self.egg = False
     69        self.egg_counter = 0
    6870
    6971    def move(self, gameboard):
    7072        """A free chicken will move away from other free chickens"""
    7173        pass
     74
     75    def lay(self):
     76        """See if the chicken lays an egg"""
     77        if not self.egg:
     78            self.egg = True
     79            self.egg_counter = 2
     80
     81    def hatch(self):
     82        """See if we have an egg to hatch"""
     83        if self.egg:
     84            self.egg_counter -= 1
     85            if self.egg_counter == 0:
     86                # Egg hatches
     87                self.egg = False
     88                return Chicken(self.pos.to_tuple())
     89        return None
    7290
    7391    def _find_killable_fox(self, weapon, gameboard):
  • gamelib/engine.py

    r99 r116  
    5555        self.game.gameboard.update_chickens()
    5656        sound.background_music("daytime.ogg")
     57        self.game.gameboard.hatch_eggs()
    5758
    5859    def event(self, e):
     
    9192        self.game.gameboard.spawn_foxes()
    9293        sound.background_music("nighttime.ogg")
     94
     95        self.game.gameboard.lay_eggs()
    9396
    9497    def event(self, e):
  • gamelib/gameboard.py

    r115 r116  
    3939        self.cash_counter = mklabel()
    4040        self.chicken_counter = mklabel()
     41        self.egg_counter = mklabel()
    4142        self.killed_foxes = mklabel()
    4243        self.rifle_counter = mklabel()
    4344
    4445        self.add_counter(mklabel("Groats:"), self.cash_counter)
     46        self.add_counter(mklabel("Eggs:"), self.egg_counter)
    4547        self.add_counter(icons.CHKN_ICON, self.chicken_counter)
    4648        self.add_counter(icons.KILLED_FOX, self.killed_foxes)
     
    6567    update_fox_counter = mkcountupdate('killed_foxes')
    6668    update_chicken_counter = mkcountupdate('chicken_counter')
     69    update_egg_counter = mkcountupdate('egg_counter')
    6770
    6871    def add_spacer(self, height=30):
     
    133136        self.selected_tool = None
    134137        self.animal_to_place = None
    135         self.chickens = []
    136         self.foxes = []
     138        self.chickens = set()
     139        self.foxes = set()
    137140        self.buildings = []
    138141        self.cash = 0
     
    231234        building = self.get_building(tile_pos)
    232235        if building:
     236            # XXX: quick hack so egg loop triggers
     237            building.add_occupant(self.animal_to_place)
     238            if self.animal_to_place in self.tv.sprites:
     239                self.tv.sprites.remove(self.animal_to_place)
     240            self.animal_to_place = None
    233241            self.open_building_dialog(building)
    234242            return
     
    332340
    333341    def clear_foxes(self):
    334         for fox in self.foxes[:]:
     342        for fox in self.foxes.copy():
    335343            # Any foxes that didn't make it to the woods are automatically
    336344            # killed
     
    340348            else:
    341349                self.tv.sprites.remove(fox)
    342         self.foxes = [] # Remove all the foxes
     350        self.foxes = set() # Remove all the foxes
    343351
    344352    def move_foxes(self):
     
    349357
    350358    def add_chicken(self, chicken):
    351         self.chickens.append(chicken)
     359        self.chickens.add(chicken)
    352360        if chicken.outside():
    353361            self.tv.sprites.append(chicken)
     
    355363
    356364    def add_fox(self, fox):
    357         self.foxes.append(fox)
     365        self.foxes.add(fox)
    358366        self.tv.sprites.append(fox)
    359367
     
    361369        self.buildings.append(building)
    362370        self.tv.sprites.append(building)
     371
     372    def lay_eggs(self):
     373        eggs = 0
     374        for building in self.buildings:
     375            if building.NAME in [buildings.HenHouse.NAME]:
     376                for chicken in building.occupants():
     377                    print 'Laying check', chicken, chicken.egg, chicken.egg_counter
     378                    chicken.lay()
     379                    if chicken.egg:
     380                        eggs += 1
     381        self.toolbar.update_egg_counter(eggs)
     382
     383    def hatch_eggs(self):
     384        eggs = 0
     385        for building in self.buildings:
     386            if building.NAME in [buildings.HenHouse.NAME]:
     387                for chicken in building.occupants():
     388                    print 'Checking', chicken, chicken.egg, chicken.egg_counter
     389                    new_chick = chicken.hatch()
     390                    if chicken.egg:
     391                        eggs += 1
     392                    if new_chick:
     393                        print 'hatching chicken %s in %s ' % (chicken, building)
     394                        building.add_occupant(new_chick)
     395                        self.add_chicken(new_chick)
     396        self.toolbar.update_egg_counter(eggs)
    363397
    364398    def kill_fox(self, fox):
     
    370404
    371405    def remove_fox(self, fox):
    372         if fox in self.foxes:
    373             self.foxes.remove(fox)
     406        self.foxes.discard(fox)
     407        if fox in self.tv.sprites:
    374408            self.tv.sprites.remove(fox)
    375409
    376410    def remove_chicken(self, chick):
    377         if chick in self.chickens:
    378             self.chickens.remove(chick)
     411        self.chickens.discard(chick)
     412        self.toolbar.update_chicken_counter(len(self.chickens))
     413        if chick in self.tv.sprites:
    379414            if chick.outside():
    380415                self.tv.sprites.remove(chick)
    381             self.toolbar.update_chicken_counter(len(self.chickens))
    382416
    383417    def remove_building(self, building):
Note: See TracChangeset for help on using the changeset viewer.