def __init__(self, pos): self.id_type = "snake" super(Snake, self).__init__(source=self.animal_sprites["snake"], pos=pos) self.num_moves = 1 self.target = None self.body = SnakeBod(self.coords) self.skip_from_prune = False self.lastcoords = None self.digesting = None
class Snake(AIAnimal): def __init__(self, pos): self.id_type = "snake" super(Snake, self).__init__(source=self.animal_sprites["snake"], pos=pos) self.num_moves = 1 self.target = None self.body = SnakeBod(self.coords) self.skip_from_prune = False self.lastcoords = None self.digesting = None def update(self): global TILEMAP if self.skip_from_prune: # super disorienting not to skip next turn after prune self.skip_from_prune = False return elif self.digesting: self.digest() else: self.move() def digest(self): movelist = [] should_stop = False for _ in range(2): current_segment = self.body[self.digesting.coords] if current_segment == self.body.tail: should_stop = True break self.digesting.coords = current_segment.prev.coords new_pos = coord_to_pixel(self.digesting.coords) movelist.append(new_pos) if len(movelist) < 2: movelist.append(coord_to_pixel(self.digesting.coords)) movelist.append(coord_to_pixel(self.digesting.coords)) anim = Animation(x=movelist[0][0], y=movelist[0][1], duration=0.01) + \ Animation(x=movelist[1][0], y=movelist[1][1], duration=0.01) anim.start(self.digesting) if should_stop: self.finish_digesting() def finish_digesting(self): global ENTITYMAP GAMEINFO["gameinstance"].remove_widget(self.digesting) del ENTITYMAP[self.digesting.entity_id] self.digesting = False def move(self): self.lastcoords = self.coords self.coords = self.select_move() if self.coords == self.lastcoords: return collided = check_for_collision(self) if collided: collided.die(self) self.digesting = collided self.target = None new_pixels = coord_to_pixel(self.coords) anim = Animation(x=new_pixels[0], y=new_pixels[1], duration=0.001) anim.start(self) GAMEINFO["gameinstance"].remove_widget(self) self.body.append(self.coords) GAMEINFO["gameinstance"].add_widget(self) TILEMAP[self.coords].move_into(self) def select_move(self): if not self.target or not self.target.isalive: self.target = self.find_nearest("pig") new_coords = self.get_astar(self.target, movement_mask="predator") return new_coords def die(self, killer): new_coords, finish_digesting = self.body.prune(killer.coords, self.digesting) if finish_digesting: self.finish_digesting() self.coords = new_coords new_pos = coord_to_pixel(self.coords) anim = Animation(x=new_pos[0], y=new_pos[1], duration=0.1) anim.start(self) self.skip_from_prune = True