class Apple: character = 'A' def __init__(self, position): self.position = position self.attached = False self.offset = (0,0) def play_turn(self, game): width, height = game.board_size player = game.get_agent_by_name('player') if self.attached: px, py = player.position ox, oy = self. offset self.position = (px + ox, py + oy) return x, y = self.position if game.turn_number % 2 == 0: new_position= (x +1, y) if new_position == player.position: self.attach_to_player(player) elif game.on_board(new_position): self.position = new_position else: game.remove_agent(self) def attach_to_player(self, player): self.attached = True index = len(player.attached_apples) self.offset = (-1 - index, 0) player.attached_apples.append(self) # if y == height - 1: # game.remove_agent(self) # else: # player = game.get_agent_by_name('player') # new_position = (x+1, y) # if new_position == player.position: # game.end() # else: # self.position = new_position