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__()

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__() Initialize self.
distance(row1, column1, row2, column2) Return the euclidian distance between to points.
intersect(row1, column1, width1, height1, …) This function check if 2 rectangles intersect.
static distance(row1, column1, row2, column2)

Return the euclidian 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, column1, width1, height1, row2, column2, width2, height2)

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])