def add_tower(self, pos, cls): tower = cls(pos, self) if pygame.sprite.spritecollideany(tower, self.creeps): raise BuildError, "Can't build on creeps" buildcells = list(util.iterpoints(pos, (cls.size, cls.size))) self.field.buildon(*buildcells) # check if we block creeps block_creeps = False for creep in self.creeps: if tuple(creep.current_cell()) not in self.field.dir_field: block_creeps = True break for e in self.field._enters: if e not in self.field.dir_field: block_creeps = True break if block_creeps: self.field.clearon(*buildcells) raise BuildError, "Blocking" tower.add([self.towers]) for creep in self.creeps: creep.forget_way() self.money -= tower.cost
def sell_tower(self, tower): sizes = (tower.size, tower.size) topleft = util.placeintrect(tower.g_pos, sizes) cells = util.iterpoints(topleft, sizes) self.field.clearon(*cells) money = int(self.sell_factor * tower.cost) message = '+%d' % money self.money += money self.messages.add(Message(message, 1, tower.g_pos, GOLD_COLOR)) tower.kill()
def build_tower(self, tower_cls, pos): if tower_cls.cost > self.get_money(): raise BuildError, "Not enough money" sizes = (tower_cls.size, tower_cls.size) topleft = util.placeintrect(pos, sizes) canbuild = all( self.field.canbuildon(p) for p in util.iterpoints(topleft, sizes)) if canbuild: self.add_tower(topleft, tower_cls) else: raise BuildError, "Can't build here"