def update(self): if self.waiting: self.wait_counter -= 1 if not self.wait_counter: self.get_busy() super(Peasant, self).update() if not self.path and tuple(self.rect.midbottom) in WHARFS: wharf = WHARFS.index(tuple(self.rect.midbottom)) box = self.game.wharfs[wharf] if box.rect.midbottom != self.rect.midbottom: return self.game.wharfs[wharf] = None self.attach_box(box) self.home = choice(HOMES) self.walk(find_closest_path(self.home, self.levels)) elif not self.path and self.box and self.home: if tuple(self.rect.midbottom) == self.home: self.box.kill() self.box = None self.path = getline(self.rect.midbottom, find_closest_path(self.home, self.levels)) self.path.reverse() else: self.path = getline(self.rect.midbottom, self.home) self.path.reverse() elif not self.path and not self.waiting: self.wait()
def walk(self, target): if not self.levels[target[0], target[1]]: target = find_closest_path(target, self.levels) self.path = astar(self.rect.midbottom, target, self.levels) if not self.path: self.path = getline(self.rect.midbottom, target) self.path.reverse()
def intermediate_positions(self, b): """Only operates in two dimensions.""" if max(abs(self.x - b.x), abs(self.y - b.y)) <= 1: # pgu gets this case wrong on occasion. return [b] start = self.to_tile_tuple() end = b.to_tile_tuple() points = getline(start, end) points.remove(start) # exclude start_pos if end not in points: # Rounding errors in getline cause this points.append(end) return [Position(p[0], p[1]) for p in points]
def drop_box(self): if None in self.wharfs: wharfs = filter(lambda x: not self.wharfs[x], range(3)) wharf = choice(wharfs) box = Box(self.sprites) box.image = self.box box.rect = box.image.get_rect() box.rect.midbottom = [WHARFS[wharf][0], 0] box.path = getline(box.rect.midbottom, WHARFS[wharf]) box.path.reverse() self.wharfs[wharf] = box peasant = choice(filter(lambda x: not x.box, self.peasants)) peasant.walk(WHARFS[wharf]) pygame.time.set_timer(25, 4000) else: pygame.time.set_timer(25, 2000)