Board¶
This module contains the Board class. It is the base class for all levels.
-
class
gamelib.Board.
Board
(**kwargs)¶ Bases:
object
A class that represent a game board.
The board is being represented by a square matrix. For the moment a board only support one player.
- The Board object is the base object to build a level :
- you create a Board and then you add BoardItems (or objects derived from BoardItem).
Parameters: - name (str) – the name of the Board
- size (list) – array [width,height] with width and height being int. The size of the board.
- player_starting_position (list) – array [row,column] with row and column being int. The coordinates at which Game will place the player on change_level().
- ui_borders (str) – To set all the borders to the same value
- ui_border_left (str) – A string that represents the left border.
- ui_border_right (str) – A string that represents the right border.
- ui_border_top (str) – A string that represents the top border.
- ui_border_bottom (str) – A string that represents the bottom border.
- ui_board_void_cell (str) – A string that represents an empty cell. This
option is going to be the model of the BoardItemVoid
(see
gamelib.BoardItem.BoardItemVoid
) - parent (
Game
) – The parent object (usually the Game object). - DISPLAY_SIZE_WARNINGS (bool) – A boolean to show or hide the warning about boards bigger than 80 rows and columns.
-
check_sanity
()¶ Check the board sanity.
This is essentially an internal method called by the constructor.
-
clear_cell
(row, column)¶ Clear cell (row, column)
This method clears a cell, meaning it position a void_cell BoardItemVoid at these coordinates.
Parameters: - row (int) – The row of the item to remove
- column (int) – The column of the item to remove
Example:
myboard.clear_cell(3,4)
Warning
This method does not check the content before, it will overwrite the content.
-
display
()¶ Display the entire board.
This method display the Board (as in print()), taking care of displaying the borders, and everything inside.
It uses the __str__ method of the item, which by default is BoardItem.model. If you want to override this behavior you have to subclass BoardItem.
-
display_around
(object, row_radius, column_radius)¶ Display only a part of the board.
This method behaves like display() but only display a part of the board around an object (usually the player). Example:
# This will display only a total of 30 cells vertically and # 60 cells horizontally. board.display_around(player, 15, 30)
Parameters: - object (
BoardItem
) – an item to center the view on (it has to be a subclass of BoardItem) - row_radius (int) – The radius of display in number of rows showed. Remember that it is a radius not a diameter…
- column_radius (int) – The radius of display in number of columns showed. Remember that… Well, same thing.
It uses the same display algorithm than the regular display() method.
- object (
-
get_immovables
(**kwargs)¶ Return a list of all the Immovable objects in the Board.
- See
gamelib.Immovable.Immovable
for more on - an Immovable object.
Parameters: **kwargs – an optional dictionnary with keys matching Immovables class members and value being something contained in that member. Returns: A list of Immovable items Example:
for m in myboard.get_immovables(): print(m.name) # Get all the Immovable objects that type contains "wall" AND name contains fire walls = myboard.get_immovables(type="wall",name="fire")
- See
-
get_movables
(**kwargs)¶ Return a list of all the Movable objects in the Board.
See
gamelib.Movable.Movable
for more on a Movable object.Parameters: **kwargs – an optional dictionnary with keys matching Movables class members and value being something contained in that member. Returns: A list of Movable items Example:
for m in myboard.get_movables(): print(m.name) # Get all the Movable objects that has a type that contains "foe" foes = myboard.get_movables(type="foe")
-
init_board
()¶ Initialize the board with BoardItemVoid that uses ui_board_void_cell as model.
Example:
myboard.init_board()
-
init_cell
(row, column)¶ Initialize a specific cell of the board with BoardItemVoid that uses ui_board_void_cell as model.
Parameters: - row (int) – the row coordinate.
- column (int) – the column coordinate.
Example:
myboard.init_cell(2,3)
-
item
(row, column)¶ Return the item at the row, column position if within board’s boundaries.
Return type: gamelib.BoardItem.BoardItem Raises: HacOutOfBoardBoundException – if row or column are out of bound.
-
move
(item, direction, step)¶ Move an item in the specified direction for a number of steps.
Example:
board.move(player,Constants.UP,1)
Parameters: - item (gamelib.Movable.Movable) – an item to move (it has to be a subclass of Movable)
- direction (gamelib.Constants) – a direction from Constants
- step (int) – the number of steps to move the item.
If the number of steps is greater than the Board, the item will be move to the maximum possible position.
If the item is not a subclass of Movable, an HacObjectIsNotMovableException exception (see
gamelib.HacExceptions.HacObjectIsNotMovableException
).Important
if the move is successfull, an empty BoardItemVoid (see
gamelib.BoardItem.BoardItemVoid
) will be put at the departure position (unless the movable item is over an overlappable item). If the movable item is over an overlappable item, the overlapped item is restored.Note
It could be interesting here, instead of relying on storing the overlapping item in a property of a Movable (
gamelib.Movable.Movable
) object, to have another dimension on the board matrix to push and pop objects on a cell. Only the first item would be rendered and it would avoid the complicated and error prone logic in this method. If anyone feel up to the challenge, PR are welcome ;-).Todo
check all types!
-
place_item
(item, row, column)¶ Place an item at coordinates row and column.
If row or column are our of the board boundaries, an HacOutOfBoardBoundException is raised.
If the item is not a subclass of BoardItem, an HacInvalidTypeException
Warning
Nothing prevents you from placing an object on top of another. Be sure to check that. This method will check for items that are both overlappable and restorable to save them, but that’s the extend of it.