PathActuator

class pygamelib.actuators.PathActuator(path: List[int] | None = None, parent: BoardItem | None = None)

Bases: Actuator

The path actuator is a subclass of Actuator. The move inside the function next_move depends on path and index. If the state is not running it returns None otherwise it increments the index & then, further compares the index with length of the path. If they both are same then, index is set to value zero and the move is returned back.

Parameters:
__init__(path: List[int] | None = None, parent: BoardItem | None = None)

The constructor take only one (positional) parameter: the parent object.

Important

The default state of ALL actuators is RUNNING. If you want your actuator to be in a different state (PAUSED for example), you have to do it yourself.

Methods

__init__([path, parent])

The constructor take only one (positional) parameter: the parent object.

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.

load(data)

Load data and create a new PathActuator out of it.

next_move()

Return the movement based on current index

notify([modifier, attribute, value])

Notify all the observers that a change occurred.

pause()

Set the actuator state to PAUSED.

serialize()

Return a dictionary with all the attributes of this object.

set_path(path)

Defines a new path

start()

Set the actuator state to RUNNING.

stop()

Set the actuator state to STOPPED.

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.

classmethod load(data: dict) PathActuator

Load data and create a new PathActuator out of it.

Parameters:

data (dict) – Data to create a new actuator (usually generated by serialize())

Returns:

A new actuator.

Return type:

PathActuator

Example:

path_actuator = PathActuator.load(actuator_data)
next_move() int

Return the movement based on current index

The movement is selected from path if state is RUNNING, otherwise it returns NO_DIR from the constants module. When state is RUNNING, the movement is selected before incrementing the index by 1. When the index equal the length of path, the index should return back to 0.

Returns:

The next movement

Return type:

int | pygamelib.constants.Direction.NO_DIR

Example:

path_actuator.next_move()
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()
pause()

Set the actuator state to PAUSED.

Example:

mygame.pause()
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

serialize() dict

Return a dictionary with all the attributes of this object.

Returns:

A dictionary with all the attributes of this object.

Return type:

dict

set_path(path: List[int])

Defines a new path

This will also reset the index back to 0.

Parameters:

path (list) – A list of movements.

Example:

path_actuator.set_path([Direction.UP,Direction.DOWN,Direction.LEFT,Direction.RIGHT])
start()

Set the actuator state to RUNNING.

If the actuator state is not RUNNING, actuators’ next_move() function (and all derivatives) should not return anything.

Example:

mygame.start()
stop()

Set the actuator state to STOPPED.

Example:

mygame.stop()
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)