Movable

This module contains the Movable class. It can potentially hold more movement related classes.

Movable(**kwargs) A class representing BoardItem capable of movements.
Projectile([name, direction, step, range, …]) A class representing a projectile type board item.
class gamelib.Movable.Movable(**kwargs)

Bases: gamelib.BoardItem.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.

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

This class contains a private member called _overlapping. This private member is used to store the reference to an overlappable object while a movable occupy its position. The Board then restore the overlapped object. You should let the Board class take care of that.

can_move()

Movable implements can_move().

Returns:True
Return type:Boolean
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.

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.

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.

size()

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. It is used for example to evaluate the space taken in the inventory.

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)
class gamelib.Movable.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, *args)

Bases: gamelib.Movable.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.
  • args – extra parameters to pass to hit_callback.
  • 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, updward_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)
can_move()

Movable implements can_move().

Returns:True
Return type:Boolean
debug_info()

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

Return type:str
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)
display()

Print the model WITHOUT carriage return.

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
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.

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

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. It is used for example to evaluate the space taken in the inventory.

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)