board_items

This module contains the basic board items classes.

BoardItem(**kwargs) Base class for any item that will be placed on a Board.
BoardItemVoid(**kwargs) A class that represent a void cell.
BoardComplexItem(**kwargs)

New in version 1.2.0.

BoardItemComplexComponent(**kwargs) The default component of a complex item.
Movable(**kwargs) A class representing BoardItem capable of movements.
Projectile([name, direction, step, range, …]) A class representing a projectile type board item.
Immovable(**kwargs) This class derive BoardItem and describe an object that cannot move or be moved (like a wall).
Actionable(**kwargs) This class derives Immovable.
Character(**kwargs) A base class for a character (playable or not)
Player(**kwargs) A class that represent a player controlled by a human.
ComplexPlayer(**kwargs)

New in version 1.2.0.

NPC(**kwargs) A class that represent a non playable character controlled by the computer.
ComplexNPC(**kwargs)

New in version 1.2.0.

TextItem([text])

New in version 1.2.0.

Wall(**kwargs) A Wall is a specialized Immovable object that as unmodifiable characteristics:
ComplexWall(**kwargs)

New in version 1.2.0.

Treasure(**kwargs) A Treasure is an Immovable that is pickable and with a non zero value.
ComplexTreasure(**kwargs)

New in version 1.2.0.

Door(**kwargs) A Door is a GenericStructure that is not pickable, overlappable and restorable.
ComplexDoor(**kwargs)

New in version 1.2.0.

GenericStructure(**kwargs) A GenericStructure is as the name suggest, a generic object to create all kind of structures.
GenericActionableStructure(**kwargs) A GenericActionableStructure is the combination of a GenericStructure and an Actionable.
Tile(**kwargs)

New in version 1.2.0.

class pygamelib.board_items.Actionable(**kwargs)

Bases: pygamelib.board_items.Immovable

This class derives Immovable. It adds the ability to an Immovable BoardItem to be triggered and execute some code.

Parameters:
  • action (function) – the reference to a function (Attention: no parentheses at the end of the function name).
  • action_parameters (list) – the parameters to the action function.
  • perm (constants) – The permission that defines what types of items can actually activate the actionable. The permission has to be one of the permissions defined in constants

On top of these parameters Actionable accepts all parameters from Immovable and therefor from BoardItem.

Note

The common way to use this class is to use GenericActionableStructure. Please refer to GenericActionableStructure for more details.

activate()

This function is calling the action function with the action_parameters.

Usually it’s automatically called by move() when a Player or NPC (see board_items)

class pygamelib.board_items.BoardComplexItem(**kwargs)

Bases: pygamelib.board_items.BoardItem

New in version 1.2.0.

A BoardComplexItem is the base item for multi cells elements. It inherits from BoardItem and accepts all its parameters.

The main difference is that a complex item can use Sprite as representation.

You can see a complex item as a collection of other items that are ruled by the same laws. They behave as one but a complex item is actually made of complex components. At first it is not important but you may want to exploit that as a feature for your game.

On top of BoardItem the constructor accepts the following parameters:

Parameters:
  • sprite (Sprite) – A sprite representing the item.
  • size (array[int]) – The size of the item. It impact movement and collision detection amongst other things. If it is left empty the Sprite size is used. If no sprite is given to the constructor the default size is 2x2.
  • base_item_type (BoardItemComplexComponent) – the building block of the complex item. The complex item is built from a 2D array of base items.
Null_sprixel:

The null_sprixel is a bit of a special parameter: during construction a null sprixel is replaced by a BoardItemVoid. This is a trick to show the background (i.e transparency). A sprixel can take the color of the background but a complex item with a null_sprixel that correspond to transparent zone of a sprite will really be transparent and show the background.

Null_sprixel:

Sprixel

item(row, column)

Return the item at the row, column position if it is within the item’s boundaries.

Return type:pygamelib.board_items.BoardItem
Raises:PglOutOfBoardBoundException – if row or column are out of bound.
update_sprite()

Update the complex item with the current sprite. This method needs to be called everytime the sprite is changed.

Example:

item = BoardComplexItem(sprite=position_idle)
for s in [walk_1, walk_2, walk_3, walk_4]:
    item.sprite = s
    item.update_sprite()
    board.move(item, constants.RIGHT, 1)
    time.sleep(0.2)
class pygamelib.board_items.BoardItem(**kwargs)

Bases: object

Base class for any item that will be placed on a Board.

Parameters:
  • type (str) – A type you want to give your item. It can be any string. You can then use the type for sorting or grouping for example.
  • name (str) – A name for this item. For identification purpose.
  • pos (array) – the position of this item. When the item is managed by the Board and Game engine this member hold the last updated position of the item. It is not updated if you manually move the item. It must be an array of 2 integers [row,column]
  • model (str) – The model to use to display this item on the Board. Be mindful of the space it will require. Default value is ‘*’.
  • parent – The parent object of the board item. Usually a Board or Game object.

Important

Starting with version 1.2.0 and introduction of complex items, BoardItems have a size. That size CANNOT be set. It is always 1x1. This is because a BoardItem always takes 1 cell, regardless of its actual number of characters. Python does not really provide a way to prevent changing that member but if you do, you’ll break rendering. You have been warned.

can_move()

This is a virtual method that must be implemented in deriving classes. This method has to return True or False. This represent the capacity for a BoardItem to be moved by the Board.

collides_with(other)

Tells if this item collides with another item.

Parameters:other (BoardItem) – The item you want to check for collision.
Return type:bool

Example:

if projectile.collides_with(game.player):
    game.player.hp -= 5
column

Convenience method to get the current stored column of the item.

This is absolutely equivalent to access to item.pos[1].

Returns:The column coordinate
Return type:int

Example:

if item.column != item.pos[1]:
    print('Something extremely unlikely just happened...')
debug_info()

Return a string with the list of the attributes and their current value.

Return type:str
display()

Print the model WITHOUT carriage return.

distance_to(other)

Calculates the distance with an item.

Parameters:other (BoardItem) – The item you want to calculate the distance to.
Returns:The distance between this item and the other.
Return type:float

Example:

if npc.distance_to(game.player) <= 2.0:
    npc.seek_and_destroy = True
height

Convenience method to get the height of the item.

This is absolutely equivalent to access to item.size[1].

Returns:The height
Return type:int

Example:

if item.height > board.height:
    print('The item is too big for the board.')
inventory_space()

This is a virtual method that must be implemented in deriving class. This method has to return an integer. This represent the size of the BoardItem for the Inventory. It is used for example to evaluate the space taken in the inventory.

Important

That abstract function was called size() before version 1.2.0. As it was exclusively used for inventory space management, it as been renamed. Particularly because now items do have a need for a size.

overlappable()

This is a virtual method that must be implemented in deriving class. This method has to return True or False. This represent the capacity for a BoardItem to be overlapped by another BoardItem.

pickable()

This is a virtual method that must be implemented in deriving class. This method has to return True or False. This represent the capacity for a BoardItem to be pick-up by player or NPC.

position_as_vector()

Returns the current item position as a Vector2D

Returns:The position as a 2D vector
Return type:Vector2D

Example:

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

Convenience method to get the current stored row of the item.

This is absolutely equivalent to access to item.pos[0].

Returns:The row coordinate
Return type:int

Example:

if item.row != item.pos[0]:
    print('Something extremely unlikely just happened...')
store_position(row, column)

Store the BoardItem position for self access.

The stored position is used for consistency and quick access to the self postion. It is a redundant information and might not be synchronized.

Parameters:
  • row (int) – the row of the item in the Board.
  • column (int) – the column of the item in the Board.

Example:

item.store_position(3,4)
width

Convenience method to get the width of the item.

This is absolutely equivalent to access to item.size[0].

Returns:The width
Return type:int

Example:

if item.width > board.width:
    print('The item is too big for the board.')
class pygamelib.board_items.BoardItemComplexComponent(**kwargs)

Bases: pygamelib.board_items.BoardItem

The default component of a complex item.

It is literrally just a BoardItem but is subclassed for easier identification.

It is however scanning its parent for the item’s basic properties (overlappable, restorable, etc.)

A component can never be pickable by itself.

can_move()

Returns True if the item can move, False otherwise.

Example:

if item.item(4,5).can_move():
    print('The item can move')
overlappable()

Returns True if the item is overlappable, False otherwise.

Example:

if item.item(4,5).overlappable():
    print('The item is overlappable')
pickable()

Returns True if the item is pickable, False otherwise.

Example:

if item.item(4,5).pickable():
    print('The item is pickable')
restorable()

Returns True if the item is restorable, False otherwise.

Example:

if item.item(4,5).restorable():
    print('The item is restorable')
class pygamelib.board_items.BoardItemVoid(**kwargs)

Bases: pygamelib.board_items.BoardItem

A class that represent a void cell.

overlappable()

A BoardItemVoid is obviously overlappable (so player and NPC can walk over).

Returns:True
pickable()

A BoardItemVoid is not pickable, therefor this method return false.

Returns:False
class pygamelib.board_items.Character(**kwargs)

Bases: object

A base class for a character (playable or not)

Parameters:
  • agility (int) – Represent the agility of the character
  • attack_power (int) – Represent the attack power of the character.
  • defense_power (int) – Represent the defense_power of the character
  • hp (int) – Represent the hp (Health Point) of the character
  • intelligence (int) – Represent the intelligence of the character
  • max_hp (int) – Represent the max_hp of the character
  • max_mp (int) – Represent the max_mp of the character
  • mp (int) – Represent the mp (Mana/Magic Point) of the character
  • remaining_lives (int) – Represent the remaining_lives of the character. For a NPC it is generally a good idea to set that to 1. Unless the NPC is a multi phased boss.
  • strength (int) – Represent the strength of the character

These characteristics are here to be used by the game logic but very few of them are actually used by the Game (pygamelib.engine) engine.

class pygamelib.board_items.ComplexDoor(**kwargs)

Bases: pygamelib.board_items.Door, pygamelib.board_items.BoardComplexItem

New in version 1.2.0.

A complex door is nothing more than a Door mashed with a BoardComplexItem.

It supports all parameters of both with inheritance going first to Door and second to BoardComplexItem.

The main interest is of course the multiple cell representation and the Sprites support.

Example:

castle_door = ComplexDoor(
        sprite=sprite_castle_door
    )
class pygamelib.board_items.ComplexNPC(**kwargs)

Bases: pygamelib.board_items.NPC, pygamelib.board_items.BoardComplexItem

New in version 1.2.0.

A complex NPC is nothing more than a NPC mashed with a BoardComplexItem.

It supports all parameters of both with inheritance going first to NPC and second to BoardComplexItem.

The main interest is of course the multiple cell representation and the Sprites support.

Example:

player = ComplexNPC(
        name='Idiot McComplexStupid',
        sprite=npc_sprite_collection['troll_licking_stones']
    )
class pygamelib.board_items.ComplexPlayer(**kwargs)

Bases: pygamelib.board_items.Player, pygamelib.board_items.BoardComplexItem

New in version 1.2.0.

A complex player is nothing more than a Player mashed with a BoardComplexItem.

It supports all parameters of both with inheritance going first to Player and second to BoardComplexItem.

The main interest is of course the multiple cell representation and the Sprites support.

Example:

player = ComplexPlayer(
        name='Mighty Wizard',
        sprite=sprite_collection['wizard_idle']
    )
class pygamelib.board_items.ComplexTreasure(**kwargs)

Bases: pygamelib.board_items.Treasure, pygamelib.board_items.BoardComplexItem

New in version 1.2.0.

A complex treasure is nothing more than a Treasure mashed with a BoardComplexItem.

It supports all parameters of both with inheritance going first to Treasure and second to BoardComplexItem.

The main interest is of course the multiple cell representation and the Sprites support.

Example:

chest = ComplexTreasure(
        sprite=sprite_chest
    )
class pygamelib.board_items.ComplexWall(**kwargs)

Bases: pygamelib.board_items.Wall, pygamelib.board_items.BoardComplexItem

New in version 1.2.0.

A complex wall is nothing more than a Wall mashed with a BoardComplexItem.

It supports all parameters of both with inheritance going first to Wall and second to BoardComplexItem.

The main interest is of course the multiple cell representation and the Sprites support.

Example:

wall = ComplexWall(
        sprite=sprite_brick_wall
    )
class pygamelib.board_items.Door(**kwargs)

Bases: pygamelib.board_items.GenericStructure

A Door is a GenericStructure that is not pickable, overlappable and restorable. It has a value of 0 and a size of 1 by default. It is an helper class that allows to focus on game design and mechanics instead of small building blocks.

Parameters:
  • model (str) – The model that will represent the door on the map
  • value (int) – The value of the door, it is useless in that case. The default value is 0.
  • inventory_space (int) – The size of the door in the inventory. Unless you make the door pickable (I have no idea why you would do that…), this parameter is not used.
  • type (str) – The type of the door. It is often used as a type identifier for your game main loop. For example: unlocked_door or locked_door.
  • pickable (Boolean) – Is this door pickable by the player? Default value is False.
  • overlappable (Boolean) – Is this door overlappable by the player? Default value is True.
  • restorable (Boolean) – Is this door restorable after being overlapped? Default value is True.

Note

All the options from GenericStructure are also available to this constructor.

Example:

door1 = Door(model=Sprites.DOOR,type='locked_door')
class pygamelib.board_items.GenericActionableStructure(**kwargs)

Bases: pygamelib.board_items.GenericStructure, pygamelib.board_items.Actionable

A GenericActionableStructure is the combination of a GenericStructure and an Actionable. It is only a helper combination.

Please see the documentation for GenericStructure and Actionable for more information.

class pygamelib.board_items.GenericStructure(**kwargs)

Bases: pygamelib.board_items.Immovable

A GenericStructure is as the name suggest, a generic object to create all kind of structures.

It can be tweaked with all the properties of BoardItem, Immovable and it can be made pickable, overlappable or restorable or any combination of these.

If you need an action to be done when a Player and/or a NPC touch the structure please have a look at pygamelib.board_items.GenericActionableStructure.

Parameters:
  • pickable (bool) – Define if the structure can be picked-up by a Player or NPC.
  • overlappable (bool) – Define if the structure can be overlapped by a Player or NPC.
  • restorable (bool) – Define if the structure can be restored by the Board after a Player or NPC passed through. For example, you want a door or an activator structure (see GenericActionableStructure for that) to remain on the board after it’s been overlapped by a player. But you could also want to develop some kind of Space Invaders game were the protection block are overlappable but not restorable.

On top of these, this object takes all parameters of BoardItem and Immovable

Important

If you need a structure with a permission system please have a look at GenericActionableStructure. This class has a permission system for activation.

overlappable()

This represent the capacity for a BoardItem to be overlapped by player or NPC.

To set this value please use set_overlappable()

Returns:False
Return type:bool
pickable()

This represent the capacity for a BoardItem to be picked-up by player or NPC.

To set this value please use set_pickable()

Returns:True or False
Return type:bool

See also

set_pickable()

restorable()

This represent the capacity for an Immovable BoardItem (in this case a GenericStructure item) to be restored by the board if the item is overlappable and has been overlapped by another Movable item.

The value of this property is set with set_restorable()

Returns:False
Return type:bool

See also

set_restorable()

set_overlappable(val)

Make the structure overlappable or not.

Parameters:val (bool) – True or False depending on the fact that the structure can be overlapped (i.e that a Player or NPC can step on it) or not.

Example:

myneatstructure.set_overlappable(True)
set_pickable(val)

Make the structure pickable or not.

Parameters:val (bool) – True or False depending on the pickability of the structure.

Example:

myneatstructure.set_pickable(True)
set_restorable(val)

Make the structure restorable or not.

Parameters:val (bool) – True or False depending on the restorability of the structure.

Example:

myneatstructure.set_restorable(True)
class pygamelib.board_items.GenericStructureComplexComponent(**kwargs)

Bases: pygamelib.board_items.GenericStructure, pygamelib.board_items.BoardItemComplexComponent

A ComplexComponent specifically for generic structures.

class pygamelib.board_items.Immovable(**kwargs)

Bases: pygamelib.board_items.BoardItem

This class derive BoardItem and describe an object that cannot move or be moved (like a wall). Thus this class implements BoardItem.can_move(). However it does not implement BoardItem.pickable() or BoardItem.overlappable()

can_move()

Return the capability of moving of an item.

Obviously an Immovable item is not capable of moving. So that method always returns False.

Returns:False
Return type:bool
inventory_space()

Return the size of the Immovable Item for the Inventory.

Returns:The size of the item.
Return type:int
restorable()

This is a virtual method that must be implemented in deriving class. This method has to return True or False. This represent the capacity for an Immovable BoardItem to be restored by the board if the item is overlappable and has been overlapped by another Movable (Movable) item.

class pygamelib.board_items.Movable(**kwargs)

Bases: pygamelib.board_items.BoardItem

A class representing BoardItem capable of movements.

Movable subclasses BoardItem.

Parameters:
  • step (int) – the amount of cell a movable can cross in one turn. Default value: 1.
  • step_vertical (int) – the amount of cell a movable can vertically cross in one turn. Default value: step value.
  • step_horizontal (int) – the amount of cell a movable can horizontally cross in one turn. Default value: step value.
  • movement_speed (int|float) – The time (in seconds) between 2 movements of a Movable. It is used by all the Game’s actuation methods to enforce move speed of NPC and projectiles.

The movement_speed parameter is only used when the Game is configured with MODE_RT. Additionally the dtmove property is used to accumulate time between frames. It is entirely managed by the Game object and most of the time you shouldn’t mess up with it. Unless you want to manage movements by yourself. If so, have fun! That’s the point of the pygamelib to let you do whatever you like.

This class derive BoardItem and describe an object that can move or be moved (like a player or NPC). Thus this class implements BoardItem.can_move(). However it does not implement BoardItem.pickable() or BoardItem.overlappable()

can_move()

Movable implements can_move().

Returns:True
Return type:Boolean
dtmove
has_inventory()

This is a virtual method that must be implemented in deriving class. This method has to return True or False. This represent the capacity for a Movable to have an inventory.

class pygamelib.board_items.NPC(**kwargs)

Bases: pygamelib.board_items.Movable, pygamelib.board_items.Character

A class that represent a non playable character controlled by the computer. For the NPC to be successfully managed by the Game, you need to set an actuator.

None of the parameters are mandatory, however it is advised to make good use of some of them (like type or name) for game design purpose.

In addition to its own member variables, this class inherits all members from:

This class sets a couple of variables to default values:

  • max_hp: 10
  • hp: 10
  • remaining_lives: 1
  • attack_power: 5
  • movement_speed: 0.25 (one movement every 0.25 second). Only useful if the game
    mode is set to MODE_RT.
Parameters:actuator (pygamelib.actuators.Actuator) – An actuator, it can be any class but it need to implement pygamelib.actuators.Actuator.

Example:

mynpc = NPC(name='Idiot McStupid', type='dumb_enemy')
mynpc.step = 1
mynpc.actuator = RandomActuator()
has_inventory()

Define if the NPC has an inventory.

This method returns false because the game engine doesn’t manage NPC inventory yet but it could be in the future. It’s a good habit to check the value returned by this function.

Returns:False
Return type:Boolean

Example:

if mynpc.has_inventory():
    print("Cool: we can pickpocket that NPC!")
else:
    print("No pickpocketing XP for us today :(")
overlappable()

Define if the NPC is overlappable.

Obviously this method also always return False.

Returns:False
Return type:Boolean

Example:

if mynpc.overlappable():
    Utils.warn("Something is fishy, that NPC is overlappable but"
        "is not a Ghost...")
pickable()

Define if the NPC is pickable.

Obviously this method always return False.

Returns:False
Return type:Boolean

Example:

if mynpc.pickable():
    Utils.warn("Something is fishy, that NPC is pickable"
        "but is not a Pokemon...")
class pygamelib.board_items.Player(**kwargs)

Bases: pygamelib.board_items.Movable, pygamelib.board_items.Character

A class that represent a player controlled by a human. It accepts all the parameters from Character and is a Movable.

This class sets a couple of variables to default values:

  • max_hp: 100
  • hp: 100
  • remaining_lives: 3
  • attack_power: 10
  • movement_speed: 0.1 (one movement every 0.1 second). Only useful if the game mode
    is set to MODE_RT.

Note

If no inventory is passed as parameter a default one is created.

has_inventory()

This method returns True (a player has an inventory).

overlappable()

This method returns false (a player cannot be overlapped).

Note

If you wish your player to be overlappable, you need to inherit from that class and re-implement overlappable().

pickable()

This method returns False (a player is obviously not pickable).

class pygamelib.board_items.Projectile(name='projectile', direction=10000100, step=1, range=5, model='⌁', movement_animation=None, hit_animation=None, hit_model=None, hit_callback=None, is_aoe=False, aoe_radius=0, parent=None, callback_parameters=[], movement_speed=0.15)

Bases: pygamelib.board_items.Movable

A class representing a projectile type board item. That class can be sub-classed to represent all your needs (fireballs, blasters shots, etc.).

That class support the 2 types of representations: model and animations. The animation cases are slightly more evolved than the regular item.animation. It does use the item.animation but with more finesse as a projectile can travel in many directions. So it also keeps track of models and animation per travel direction.

You probably want to subclass Projectile. It is totally ok to use it as it, but it is easier to create a subclass that contains all your Projectile information and let the game engine deal with orientation, range keeping, etc. Please see examples/07_projectiles.py for a good old fireball example.

By default, Projectile travels in straight line in one direction. This behavior can be overwritten by setting a specific actuator (a projectile is a Movable so you can use my_projectile.actuator).

The general way to use it is as follow:

  • Create a factory object with your static content (usually the static models, default direction and hit callback)
  • Add the direction related models and/or animation (keep in mind that animation takes precedence over static models)
  • deep copy that object when needed and add it to the projectiles stack of the game object.
  • use Game.actuate_projectiles(level) to let the Game engine do the heavy lifting.

The Projectile constructor takes the following parameters:

Parameters:
  • direction (int) – A direction from the constants module
  • range (int) – The maximum range of the projectile in number of cells that can be crossed. When range is attained the hit_callback is called with a BoardItemVoid as a collision object.
  • step (int) – the amount of cells a projectile can cross in one turn
  • model (str) – the default model of the projectile.
  • movement_animation (Animation) – the default animation of a projectile. If a projectile is sent in a direction that has no explicit and specific animation, then movement_animation is used if defined.
  • hit_animation (Animation) – the animation used when the projectile collide with something.
  • hit_model (str) – the model used when the projectile collide with something.
  • hit_callback (function) – A reference to a function that will be called upon collision. The hit_callback is receiving the object it collides with as first parameter.
  • is_aoe (bool) – Is this an ‘area of effect’ type of projectile? Meaning, is it doing something to everything around (mass heal, exploding rocket, fireball, etc.)? If yes, you must set that parameter to True and set the aoe_radius. If not, the Game object will only send the colliding object in front of the projectile.
  • aoe_radius (int) – the radius of the projectile area of effect. This will force the Game object to send a list of all objects in that radius.
  • callback_parameters (list) – A list of parameters to pass to hit_callback.
  • movement_speed (int|float) – The movement speed of the projectile
  • parent – The parent object (usually a Board object or some sort of BoardItem).

Important

The effects of a Projectile are determined by the callback. No callback == no effect!

Example:

fireball = Projectile(
                        name="fireball",
                        model=Utils.red_bright(black_circle),
                        hit_model=Sprites.EXPLOSION,
                    )
fireball.set_direction(constants.RIGHT)
my_game.add_projectile(1, fireball,
                       my_game.player.pos[0], my_game.player.pos[1] + 1)
add_directional_animation(direction, animation)

Add an animation for a specific direction.

Parameters:
  • direction (int) – A direction from the constants module.
  • animation (Animation) – The animation for the direction

Example:

fireball.add_directional_animation(constants.UP, constants.UP, animation)
add_directional_model(direction, model)

Add an model for a specific direction.

Parameters:
  • direction (int) – A direction from the constants module.
  • model (str) – The model for the direction

Example:

fireball.add_directional_animation(constants.UP, updward_animation)
directional_animation(direction)

Return the animation for a specific direction.

Parameters:direction (int) – A direction from the constants module.
Return type:Animation

Example:

# No more animation for the UP direction
fireball.directional_animation(constants.UP)
directional_model(direction)

Return the model for a specific direction.

Parameters:direction (int) – A direction from the constants module.
Return type:str

Example:

fireball.directional_model(constants.UP)
has_inventory()

Projectile cannot have inventory by default.

Returns:False
Return type:Boolean
hit(objects)

A method that is called when the projectile hit something.

That method is automatically called by the Game object when the Projectile collide with another object or is at the end of its range.

Here are the call cases covered by the Game object:

  • range is reached without collision and projectile IS NOT an AoE type: hit() is called with a single BoardItemVoid in the objects list.
  • range is reached without collision and projectile IS an AoE type: hit() is called with the list of all objects within aoe_radius (including structures).
  • projectile collide with something and IS NOT an AoE type: hit() is called with the single colliding object in the objects list.
  • projectile collide with something and IS an AoE type: hit() is called with the list of all objects within aoe_radius (including structures).

In turn, that method calls the hit_callback with the following parameters (in that order):

  1. the projectile object
  2. the list of colliding objects (that may contain only one object)
  3. the callback parameters (from the constructor callback_parameters)
Parameters:objects – A list of objects hit by or around the projectile.

Example:

my_projectile.hit([npc1])
overlappable()

Projectile are overlappable by default.

Returns:True
Return type:Boolean
remove_directional_animation(direction)

Remove an animation for a specific direction.

Parameters:direction (int) – A direction from the constants module.

Example:

# No more animation for the UP direction
fireball.remove_directional_animation(constants.UP)
remove_directional_model(direction)

Remove the model for a specific direction.

Parameters:direction (int) – A direction from the constants module.

Example:

fireball.directional_model(constants.UP)
restorable()

We assume that by default, Projectiles are restorable.

Returns:True
Return type:bool
set_direction(direction)

Set the direction of a projectile

This method will set a UnidirectionalActuator with the direction. It will also take care of updating the model and animation for the given direction if they are specified.

Parameters:direction (int) – A direction from the constants module.

Example:

fireball.set_direction(constants.UP)
class pygamelib.board_items.TextItem(text=None, **kwargs)

Bases: pygamelib.board_items.BoardComplexItem

New in version 1.2.0.

The text item is a board item that can contains text. The text can then be manipulated and placed on a Board.

It is overall a BoardComplexItem (so it takes all the parameters of that class). The big difference is that the first parameter is the text you want to display.

The text parameter can be either a regular string or a Text object (in case you want formatting and colors).

Parameters:text (str | Text) – The text you want to display.

Example:

city_name = TextItem('Super City')
fancy_city_name = TextItem(text=base.Text('Super City', base.Fore.GREEN,
    base.Back.BLACK,
    base.Style.BRIGHT
))
my_board.place_item(city_name, 0, 0)
my_board.place_item(fancy_city_name, 1, 0)
text

The text within the item.

TextItem.text can be set to either a string or a Text object.

It will always return a Text object.

Internally it translate the text to a Sprite to display it correctly on a Board. If print()-ed it will do so like the Text object.

class pygamelib.board_items.Tile(**kwargs)

Bases: pygamelib.board_items.GenericStructure, pygamelib.board_items.BoardComplexItem

New in version 1.2.0.

A Tile is a standard BoardComplexItem configured by default to:

  • be overlappable
  • be not pickable
  • be immovable.

Aside from the movable attributes (it inherit from GenericStructure so it’s an Immovable object), everything else is configurable.

It is particularly useful to display a Sprite on the background or to create terrain.

Parameters:
  • overlappable (bool) – Defines if the Tile can be overlapped.
  • restorable (bool) – Defines is the Tile should be restored after being overlapped.
  • pickable (bool) – Defines if the Tile can be picked up by the Player or NPC.

Please see BoardComplexItem for additional parameters.

Example:

grass_sprite = Sprite.load_from_ansi_file('textures/grass.ans')
for pos in grass_positions:
    outdoor_level.place_item( Tile(sprite=grass_sprite), pos[0], pos[1] )
can_move()

A Tile cannot move.

Returns:False
Return type:bool
class pygamelib.board_items.Treasure(**kwargs)

Bases: pygamelib.board_items.Immovable

A Treasure is an Immovable that is pickable and with a non zero value. It is an helper class that allows to focus on game design and mechanics instead of small building blocks.

Parameters:
  • model (str) – The model that will represent the treasure on the map
  • value (int) – The value of the treasure, it is usually used to calculate the score.
  • inventory_space (int) – The space occupied by the treasure. It is used by Inventory as a measure of space. If the treasure’s size exceed the Inventory size (or the cumulated size of all items + the treasure exceed the inventory max_size()) the Inventory will refuse to add the treasure.

Note

All the options from Immovable are also available to this constructor.

Example:

money_bag = Treasure(model=Sprites.MONEY_BAG,value=100,inventory_space=2)
print(f"This is a money bag {money_bag}")
player.inventory.add_item(money_bag)
print(f"The inventory value is {player.inventory.value()} and is at
    {player.inventory.size()}/{player.inventory.max_size}")
overlappable()

This represent the capacity for a Treasure to be overlapped by player or NPC.

A treasure is not overlappable.

Returns:False
Return type:bool
pickable()

This represent the capacity for a Treasure to be picked-up by player or NPC.

A treasure is obviously pickable by the player and potentially NPCs. Board puts the Treasure in the Inventory if the picker implements has_inventory()

Returns:True
Return type:bool
restorable()

This represent the capacity for a Treasure to be restored after being overlapped.

A treasure is not overlappable, therefor is not restorable.

Returns:False
Return type:bool
class pygamelib.board_items.Wall(**kwargs)

Bases: pygamelib.board_items.Immovable

A Wall is a specialized Immovable object that as unmodifiable characteristics:

  • It is not pickable (and cannot be).
  • It is not overlappable (and cannot be).
  • It is not restorable (and cannot be).

As such it’s an object that cannot be moved, cannot be picked up or modified by Player or NPC and block their ways. It is therefor advised to create one per board and reuse it in many places.

Parameters:
  • model (str) – The representation of the Wall on the Board.
  • name (str) – The name of the Wall.
  • size (int) – The size of the Wall. This parameter will probably be deprecated as size is only used for pickable objects.
overlappable()

This represent the capacity for a BoardItem to be overlapped by player or NPC.

Returns:False
Return type:bool
pickable()

This represent the capacity for a BoardItem to be pick-up by player or NPC.

Returns:False
Return type:bool

Example:

if mywall.pickable():
    print('Whoaa this wall is really light... and small...')
else:
    print('Really? Trying to pick-up a wall?')
restorable()

This represent the capacity for an Immovable Movable item. A wall is not overlappable.

Returns:False
Return type:bool