Math

class pygamelib.base.Math

Bases: object

The math class regroup math functions required for game development.

New in version 1.2.0.

For the moment there is only static methods in that class but it will evolve in the future.

__init__()

Methods

__init__()

distance(row1, column1, row2, column2)

Return the euclidean distance between to points.

intersect(row1, column1, width1, height1, ...)

This function check if 2 rectangles intersect.

lerp(a, b, t)

Return the linear interpolation between 2 values relative to a third value.

static distance(row1: int, column1: int, row2: int, column2: int) float

Return the euclidean distance between to points.

Points are identified by their row and column. If you want the distance in number of cells, you need to round the result (see example).

Parameters:
  • row1 (int) – the row number (coordinate) of the first point.

  • column1 (int) – the column number (coordinate) of the first point.

  • row2 (int) – the row number (coordinate) of the second point.

  • column2 (int) – the column number (coordinate) of the second point.

Returns:

The distance between the 2 points.

Return type:

float

Example:

distance = round(base.Math.distance(player.row,
                                player.column,
                                npc.row,
                                npc.column)
                )
static intersect(row1: int, column1: int, width1: int, height1: int, row2: int, column2: int, width2: int, height2: int) bool

This function check if 2 rectangles intersect.

The 2 rectangles are defined by their positions (row, column) and dimension (width and height).

Parameters:
  • row1 (int) – The row of the first rectangle

  • column1 (int) – The column of the first rectangle

  • width1 (int) – The width of the first rectangle

  • height1 (int) – The height of the first rectangle

  • row2 (int) – The row of the second rectangle

  • column2 – The column of the second rectangle

  • width2 (int) – The width of the second rectangle

  • height2 (int) – The height of the second rectangle

Returns:

A boolean, True if the rectangles intersect False, otherwise.

Example:

if intersect(projectile.row, projectile.column, projectile.width,
             projectile.height, bady.row, bady.column, bady.width,
             bady.height):
    projectile.hit([bady])
static lerp(a: float, b: float, t: float) float

Return the linear interpolation between 2 values relative to a third value.

New in version 1.3.0.

Parameters:
  • a (float) – Start value of the interpolation. Returned if t is 0.

  • b (float) – End value of the interpolation. Returned if t is 1.

  • t (float) – A value between 0 and 1 used to interpolate between a and b.

Example:

value = lerp(0, 100, 0.5) # 50