Path

Iterator that describes a Path

This module implements an iterator Path to be used the descriptions of the points that form a particular path. There are also the following classes with specialised paths:

Aswell as the following pre-implemented useful paths:

Path

class steering.path.Path(path_func, domain_end, domain_start=0, increment=1)[source]

Iterator that describes a Path

Provides a flexible interface to describe dynamic paths as an iterator, it uses a Path Function in the form of f(x) = y to describe the path.

Parameters:
  • path_func (function number -> number) – The function that describes the path
  • domain_start (int) – Start point for the function domain, defaults to 0
  • domain_end (int) – End point for the function domain
  • increment (int) – Step to generate every point on the path

Example

The way this class is meant to be used is by sub-classing it and creating your own class with your own properties, this is a slightly useluess implementation of CircumferencePath that only traverses the path once.

class OnceCircumferencePath(Path):

    def __init__(self, center, radius):
        self.center = center
        self.radius = radius

        def circumference_path(self, x):
            angle = math.radians(x)
            center = self.center
            x = center[0] + math.cos(angle)*self.radius
            y = center[1] + math.sin(angle)*self.radius
            return x, y

        super(OnceCircumferencePath, self).__init__(parabola_path, domain_start = 0, domain_end = 360, increment = 15)

>>> mypath = OnceCircumferencePath(center = (50, 50), radius = 50)
>>> next(mypath)
(100.0, 50.0)
>>> next(mypath)
(98.29629131445341, 62.940952255126035)
>>> next(mypath)
(93.30127018922194, 75.0)

A good tip is to use lambda functions in order to have dynamically updated paths, this allows to have attributes like ‘center’ update with the position of something in the game, which will alter the points the path will produce

class OnceCircumferencePath(Path):

    def __init__(self, center, radius):
        self.center = center
        self.radius = radius

        def circumference_path(self, x):
            angle = math.radians(x)
            # Notice that we are now calling the attribute 'center' as a function
            center = self.center()
            x = center[0] + math.cos(angle)*self.radius
            y = center[1] + math.sin(angle)*self.radius
            return x, y

        super(OnceCircumferencePath, self).__init__(parabola_path, domain_start = 0, domain_end = 360, increment = 15)

>>> character = SomeGameObjectWithARect()
# The 'center' parameter is now defined as a lambda functions that gets the position of a character
>>> mypath = OnceCircumferencePath(center = (lambda: character.rect.center), radius = 50)
as_list()[source]

Returns the path as a list of points

This ignores the infinity of CyclicPath and MirroredPath and returns a finite list. Nevertheless, you should keep in mind that if for your own sub-classes this methods does not return the expected results, it’s probabbly the method’s fault (my faul) and you should implement your own since this is used for drawing indicators.

Returns:
Return type:list(tuple(float, float))
reset()[source]

Returns the iterator to it’s initial point

Special Paths

class steering.path.CyclicPath(path_func, domain_end, domain_start=0, increment=1)[source]

Iterator that implements Cyclic Paths

This is a sub-class of Path that returns to the path’s starting point once it reaches the end, this produces an infinite iterator.

Uses the same parameters as Path.

class steering.path.MirroredPath(path_func, domain_end, domain_start=0, increment=1)[source]

Iterator that implements Mirrored Paths

This is a sub-class of Path that Mirrors the path produced by the given function, this produces an infinite iterator that backtracks on the traversed path once it reaches it’s domain_end, and does the same after it reaches domain_start.

Uses the same parameters as Path.

as_list()[source]

Returns the path as a list of points

This ignores the infinity of CyclicPath and MirroredPath and returns a finite list. Nevertheless, you should keep in mind that if for your own sub-classes this methods does not return the expected results, it’s probabbly the method’s fault (my faul) and you should implement your own since this is used for drawing indicators.

Returns:
Return type:list(tuple(float, float))

Pre-implemented Paths

class steering.path.PathCircumference(center, radius, start=0)[source]

Circumference-like CyclicPath

Parameters:
class steering.path.PathParabola(origin, width=400, height=100)[source]

Parabola-like MirroredPath

Parameters:
  • origin (tuple(int, int) or function -> tuple(int, int)) – Lowest point of the parabola
  • width (int) –
  • height (int) –