base

The Game.py module has only one class: Game. It is what could be called the game engine. It holds a lot of methods that helps taking care of some complex mechanics behind the curtain.

This module contains the Inventory class.

This module regroup all the specific exceptions of the library. The idea behind most exceptions is to provide more context and info that the standard exceptions.

This module contains the Board class. It is the base class for all levels.

Math() The math class regroup math functions required for game development.
PglException(error, message) Exception raised for non specific errors in the pygamelib.
PglInvalidLevelException(message) Exception raised if a level is not associated to a board in Game().
PglInvalidTypeException(message) Exception raised for invalid types.
PglObjectIsNotMovableException(message) Exception raised if the object that is being moved is not a subclass of Movable.
PglOutOfBoardBoundException(message) Exception for out of the board’s boundaries operations.
Vector2D([row, column]) A 2D vector class.
Text([text, fg_color, bg_color, style]) An object to manipulate and display text in multiple contexts.
exception pygamelib.base.HacException(error, message)

Bases: pygamelib.base.PglException

A simple forward to PglException

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.HacInvalidLevelException(message)

Bases: pygamelib.base.PglInvalidLevelException

Forward to PglInvalidLevelException

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.HacInvalidTypeException(message)

Bases: pygamelib.base.PglInvalidTypeException

A simple forward to PglInvalidTypeException

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.HacInventoryException(error, message)

Bases: pygamelib.base.PglInventoryException

Forward to PglInventoryException.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.HacObjectIsNotMovableException(message)

Bases: pygamelib.base.PglObjectIsNotMovableException

Simple forward to PglObjectIsNotMovableException

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.HacOutOfBoardBoundException(message)

Bases: pygamelib.base.PglOutOfBoardBoundException

Simple forward to PglOutOfBoardBoundException

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

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.

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])
exception pygamelib.base.PglException(error, message)

Bases: Exception

Exception raised for non specific errors in the pygamelib.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.PglInvalidLevelException(message)

Bases: Exception

Exception raised if a level is not associated to a board in Game().

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.PglInvalidTypeException(message)

Bases: Exception

Exception raised for invalid types.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.PglInventoryException(error, message)

Bases: Exception

Exception raised for issue related to the inventory. The error is an explicit string, and the message explains the error.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.PglObjectIsNotMovableException(message)

Bases: Exception

Exception raised if the object that is being moved is not a subclass of Movable.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.PglOutOfBoardBoundException(message)

Bases: Exception

Exception for out of the board’s boundaries operations.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pygamelib.base.PglOutOfItemBoundException(message)

Bases: Exception

Exception for out of the item’s boundaries operations.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class pygamelib.base.Text(text='', fg_color='', bg_color='', style='')

Bases: object

An object to manipulate and display text in multiple contexts.

New in version 1.2.0.

The Text class is a collection of text formating and display static methods.

You can either instantiate an object or use the static methods.

The Text object allow for easy text manipulation through its collection of independent attributes. They help to set the text, its style and the foreground and background colors.

The Text object can generate a Sprite to represent itself. This is particularly useful to the place text on the game Board.

Parameters:
  • text (str) – The text to manipulate
  • fg_color (str) – The foreground color for the text.
  • bg_color (str) – The background color for the text.
  • style (str) – The style for the text.
bg_color = None

The bg_color attribute sets the background color. It needs to be a str.

static black(message)

This method works exactly the way green_bright() work with different color.

static black_bright(message)

This method works exactly the way green_bright() work with different color.

static black_dim(message)

This method works exactly the way green_bright() work with different color.

static blue(message)

This method works exactly the way green_bright() work with different color.

static blue_bright(message)

This method works exactly the way green_bright() work with different color.

static blue_dim(message)

This method works exactly the way green_bright() work with different color.

static cyan(message)

This method works exactly the way green_bright() work with different color.

static cyan_bright(message)

This method works exactly the way green_bright() work with different color.

static cyan_dim(message)

This method works exactly the way green_bright() work with different color.

static debug(message)

Print a debug message.

The debug message is a regular message prefixed by INFO in blue on a green background.

Parameters:message (str) – The message to print.

Example:

base.Text.debug("This is probably going to success, eventually...")
static fatal(message)

Print a fatal message.

The fatal message is a regular message prefixed by FATAL in white on a red background.

Parameters:message (str) – The message to print.

Example:

base.Text.fatal("|x_x|")
fg_color = None

The fg_color attribute sets the foreground color. It needs to be a str.

static green(message)

This method works exactly the way green_bright() work with different color.

static green_bright(message)

Return a string formatted to be bright green

Parameters:message (str) – The message to format.
Returns:The formatted string
Return type:str

Example:

print( Text.green_bright("This is a formatted message") )
static green_dim(message)

This method works exactly the way green_bright() work with different color.

static info(message)

Print an informative message.

The info is a regular message prefixed by INFO in white on a blue background.

Parameters:message (str) – The message to print.

Example:

base.Text.info("This is a very informative message.")
static magenta(message)

This method works exactly the way green_bright() work with different color.

static magenta_bright(message)

This method works exactly the way green_bright() work with different color.

static magenta_dim(message)

This method works exactly the way green_bright() work with different color.

parent = None

This object’s parent. It needs to be a BoardItem.

static print_white_on_red(message)

Print a white message over a red background.

Parameters:message (str) – The message to print.

Example:

base.Text.print_white_on_red("This is bright!")
static red(message)

This method works exactly the way green_bright() work with different color.

static red_bright(message)

This method works exactly the way green_bright() work with different color.

static red_dim(message)

This method works exactly the way green_bright() work with different color.

style = None

The style attribute sets the style of the text. It needs to be a str.

text = None

The text attribute. It needs to be a str.

static warn(message)

Print a warning message.

The warning is a regular message prefixed by WARNING in black on a yellow background.

Parameters:message (str) – The message to print.

Example:

base.Text.warn("This is a warning.")
static white(message)

This method works exactly the way green_bright() work with different color.

static white_bright(message)

This method works exactly the way green_bright() work with different color.

static white_dim(message)

This method works exactly the way green_bright() work with different color.

static yellow(message)

This method works exactly the way green_bright() work with different color.

static yellow_bright(message)

This method works exactly the way green_bright() work with different color.

static yellow_dim(message)

This method works exactly the way green_bright() work with different color.

class pygamelib.base.Vector2D(row=0.0, column=0.0)

Bases: object

A 2D vector class.

New in version 1.2.0.

Contrary to the rest of the library Vector2D uses floating point numbers for its coordinates/direction/orientation. However since the rest of the library uses integers, the numbers are rounded to 2 decimals. You can alter that behavior by increasing or decreasing (if you want integer for example).

Vector2D use the row/column internal naming convention as it is easier to visualize For learning developers. If it is a concept that you already understand and are more familiar with the x/y coordinate system you can also use x and y.

  • x is equivalent to column
  • y is equivalent to row

Everything else is the same.

Vectors can be printed and supports basic operations:

  • addition
  • substraction
  • multiplication

Let’s elaborate a bit more on the multiplication. The product behaves in 2 different ways:

If you multiply a vector with a scalar (int or float), the return value is a Vector2D with each vector component multiplied by said scalar.

If you multiply a Vector2D with another Vector2D you ask for the the cross product of vectors. This is an undefined mathematical operation in 2D as the cross product is supposed to be perpendicular to the 2 other vectors (along the z axis in our case). Since we don’t have depth (z) in 2D, this will return the magnitude of the signed cross product of the 2 vectors.

Example of products:

v1 = base.Vector2D(1,2)
v2 = base.Vector2D(3,4)
# This returns -2
mag = v1 * v2
# This returns a Vector2D with values (-1, -2)
inv = v1 * -1
# This return a Vector2D with values (2.85, 3.8) or 95% of v2
dim = v2 * 0.95
Parameters:
  • row (int) – The row/y parameter.
  • column (int) – The column/x parameter.

Example:

gravity = Vector2D(9.81, 0)
# Remember that minus on row is up.
speed = Vector2D(-0.123, 0.456)
# In that case you might want to increase the rounding precision
speed.rounding_precision = 3
column

The column component of the vector.

classmethod from_direction(direction, step)

Build and return a Vector2D from a direction.

Directions are from the constants module.

Parameters:
  • direction (int) – A direction from the constants module.
  • step (int) – The number of cell to cross in one movement.

Example:

v2d_up = Vector2D.from_direction(constants.UP, 1)
length()

Returns the length of a vector.

Return type:float

Example:

if speed.length() == 0.0:
    print('We are not moving... at all...')
rounding_precision = None

The rounding_precision attributes is used when vectors values are calculated and the result rounded for convenience. It can be changed anytime to increase or decrease the precision anytime.

row

The row component of the vector.

unit()

Returns a normalized unit vector.

Returns:A unit vector
Return type:Vector2D

Example:

gravity = Vector2D(9.81, 0)
next_position = item.position_as_vector() + gravity.unit()
x

x is an alias for column.

y

y is an alias for row.

base.Text = <class 'pygamelib.base.Text'>