class FruitPiece:
    character = "@"
    color = "green_on_indigo"
    def __init__(self, position):
        self.position = position

class Fruit:
    width = 2
    height = 2
    display = False
    pieces = []
    character = "@"
    color = "green_on_indigo"

    def __init__(self, position):
        self.position = position

    def play_turn(self, game):
        if not self.pieces:
            self.create_pieces(game)
        if game.turn_number % 2 == 0: 
            x, y = self.position
            if y == 24: 
                game.remove_agent(self)
            else:
                catcher = game.get_agent_by_name("Player")
                new_position = (x, y + 1)
                if new_position == catcher.position:
                    catcher.explode()
                    game.end()
                else:
                    self.position = new_position

    def create_pieces(self, game):
        x, y = self.position
        self.pieces = []
        for i in range(self.width):
            piece = FruitPiece((x + i, y))
            self.pieces.append(piece)
            game.add_agent(piece)
        for i in range(self.height):
            piece = FruitPiece((x, y + i))
            self.pieces.append(piece)
            game.add_agent(piece)

    def update_piece_positions(self):
        if game.turn_number % 2 == 0: 
            self.set_color()
            x, y = self.position
            if y == HEIGHT - 1: 
                game.remove_agent(self)
            else:
                ship = game.get_agent_by_name("Player")
                new_position = (x, y - 1)
                if new_position == ship.position:
                    ship.explode()
                    game.end()
                else:
                    self.position = new_position


class FruitSpawner:
    display = False

    def play_turn(self, game):    
        if self.should_spawn_fruit(game.turn_number):
            asteroid = Fruit((randint(0, WIDTH - 1), 0))
            game.add_agent(fruit)

    def should_spawn_fruit(self, turn_number):
        return randint(0, 1000) < turn_number