PglBaseObject

class pygamelib.base.PglBaseObject

Bases: object

The base object of most of the pygamelib’s classes.

New in version 1.3.0.

The PglBaseObject has 2 goals:

  • Store the object’s screen position.

  • Implements a modified observer design pattern.

It is “modified” as it acts both as the observer and the client. The idea behind it is that any object can observe and be observed by any other objects.

The base logic of the pattern is already implemented and probably does not require re-implementation on the child object. However, the handle_notification() method needs to be implemented in each client. The actual processing of the notification is indeed specific to each object.

Storing the screen position is particularly useful for BoardItem subclasses as they only know their position relative to the Board but might need to know their absolute screen coordinates.

This is a lightweight solution to that issue. It is not foolproof however! The screen_row and screen_column attributes are not wrapped properties and can be modified to mess up things. It shouldn’t be done lightly. You have been warned!

__init__() None

Like the object class, this class constructor takes no parameter.

Methods

__init__()

Like the object class, this class constructor takes no parameter.

attach(observer)

Attach an observer to this instance.

detach(observer)

Detach an observer from this instance.

handle_notification(subject[, attribute, value])

A virtual method that needs to be implemented by the observer.

notify([modifier, attribute, value])

Notify all the observers that a change occurred.

store_screen_position(row, column)

Store the screen position of the object.

Attributes

screen_column

A property to get/set the screen column.

screen_row

A property to get/set the screen row.

attach(observer)

Attach an observer to this instance. It means that until it is detached, it will be notified every time that a notification is issued (usually on changes).

An object cannot add itself to the list of observers (to avoid infinite recursions).

Parameters:

observer (PglBaseObject) – An observer to attach to this object.

Returns:

True or False depending on the success of the operation.

Return type:

bool

Example:

myboard = Board()
screen = Game.instance().screen
# screen will be notified of all changes in myboard
myboard.attach(screen)
detach(observer)

Detach an observer from this instance. If observer is not in the list this returns False.

Parameters:

observer (PglBaseObject) – An observer to detach from this object.

Returns:

True or False depending on the success of the operation.

Return type:

bool

Example:

# screen will no longer be notified of the changes in myboard.
myboard.detach(screen)
handle_notification(subject, attribute=None, value=None)

A virtual method that needs to be implemented by the observer. By default it does nothing but each observer needs to implement it if something needs to be done when notified.

This method always receive the notifying object as first parameter. The 2 other parameters are optional and can be None.

You can use the attribute and value as you see fit. You are free to consider attribute as an event and value as the event’s value.

Parameters:
  • subject (PglBaseObject) – The object that has changed.

  • attribute (str) – The attribute that has changed, it is usually a “FQDN style” string. This can be None.

  • value (Any) – The new value of the attribute. This can be None.

notify(modifier=None, attribute: str = None, value: Any = None) None

Notify all the observers that a change occurred.

Parameters:
  • modifier (PglBaseObject) – An optional parameter that identify the modifier object to exclude it from the notified objects.

  • attribute (str) – An optional parameter that identify the attribute that has changed.

  • value (Any) – An optional parameter that identify the new value of the attribute.

Example:

# This example is silly, you would usually notify other objects from inside
# an object that changes a value that's important for the observers.
color = Color(255,200,125)
color.attach(some_text_object)
color.notify()
property screen_column: int

A property to get/set the screen column.

Parameters:

value (int) – the screen column

Return type:

int

property screen_row: int

A property to get/set the screen row.

Parameters:

value (int) – the screen row

Return type:

int

store_screen_position(row: int, column: int) bool

Store the screen position of the object.

This method is automatically called by Screen.place().

Parameters:
  • row (int) – The row (or y) coordinate.

  • column (int) – The column (or x) coordinate.

Example:

an_object.store_screen_coordinate(3,8)