Ejemplo n.º 1
0
    def min_time_strategy(self, game):
        best_angle = -1
        best_time = float("inf")
        best_vel = Vector2D(0, 0)
        best_p = None
        best_d = 0

        for angle in np.linspace(0, math.pi * 2, NUM_RAYS):

            vel = Vector2D.from_angle(angle)
            line = Line(self.pos.x, self.pos.y, self.pos.x + vel.x * 1000,
                        self.pos.y + vel.y * 1000)

            if game.draw:
                line.draw(game.screen)

            time = 0

            for other_line in game.puck.path:
                p = line.intersection(other_line)
                if p:
                    if game.draw:
                        Circle(p.x, p.y, 5,
                               pygame.Color('yellow')).draw(game.screen)

                    t = other_line.find_t(
                        p) * other_line.length() / game.puck.vel.mag()

                    if game.draw:
                        textsurface = myfont.render(
                            str(round(time + t, 3)) + " frames", False,
                            (0, 0, 0))
                        game.screen.blit(textsurface, (p.x, p.y))

                    if self.pos.distsq(p) < 5**2:
                        self.vel = Vector2D(0, 0)

                    d = math.sqrt(self.pos.distsq(p))

                    if time + t < best_time and p.y <= game.center_y and d <= self.max_vel * (
                            time + t):
                        best_time = time + t
                        best_p = p
                        best_time = time + t
                        best_angle = angle
                        best_vel = copy(vel)
                        best_d = math.sqrt(self.pos.distsq(p))
                time += other_line.length() / game.puck.vel.mag()

        if best_p:
            if game.draw:
                Circle(best_p.x, best_p.y, 10,
                       pygame.Color('purple')).draw(game.screen)
            self.vel = Vector2D.from_angle(best_angle).setmag(
                min(self.max_vel, best_d / best_time))