Board

classDiagram Board <|-- PixelBoard Board <|-- TiledBoard class Board{ } class PixelBoard{ } class TiledBoard{ }

Warning

You do not need instances of this class. Use the subclasses TiledBoard and PixelBoard

Board

class miniworldmaker.boards.board.Board(columns=40, rows=40, tile_size=1, tile_margin=0, background_image=None)[source]

Board is the Base Class for Boards, e.g. TiledBoard, PixelBoard or PhysicsBoard.

You can create a custom board by inherit one of Board subclasses (TiledBoard, PixelBoard or PhysicsBoard) or by creating a board-object:

Tiledboard

A Board for Games based on Tiles (Like Rogue-Like RPGs).

  • Every token on a TiledBoard has the size of exactly on one Tile. (If your tile_size is 40, every token has the size 40x40. )

  • The position of a token (mytoken.position) corresponds to the tile on which it is placed.

  • Two tokens collide when they are on the same tile.

TiledBoard

PixelBoard

A board for pixel accurate games.

Asteroids
  • The position of a token on a PixelBoard is the pixel at toplef of token.

  • New tokens are created with top-left corner of token rect at position.

  • Two tokens collide when their sprites overlap.

Examples

Creating a TiledBoard Object:


myboard = miniworldmaker.TiledBoard() myboard.columns = 30 myboard.rows = 20 myboard.tile_size = 20

Creating a TiledBoard-Subclass as Class:

class MyBoard(miniworldmaker.TiledBoard):

    def on_setup(self):
        self.columns = 30
        self.rows = 20
        self.tile_size = 20

Creating a PixelBoard Object:

myboard = miniworldmaker.PixelBoard()
myboard.columns = 300
myboard.rows = 200

Example: A PixelBoard Subclass

class MyBoard(miniworldmaker.PixelBoard):

    def on_setup(self):
        self.columns = 300
        self.rows = 200

See also

Parameters
  • columns – columns of new board (default: 40)

  • rows – rows of new board (default:40)

add_background(source)[source]

Adds a new background to the board

Parameters

source – The path to the first image of the background or a color

Examples

Multiple Backgrounds:

board = miniworldmaker.TiledBoard()
...
board.add_background("images/soccer_green.jpg")
board.add_background("images/space.jpg")
Returns

The new created background.

Return type

Background

property columns: int

The number of columns

Return type

int

property container_height: int

The height of the container

Return type

int

property container_width: int

The width of the container

Return type

int

property fps: int

Frames per second shown on the screen.

This controls how often the screen is redrawn. However, the game logic can be called more often or less often independently of this with board.speed.

Examples:

board.speed = 10
board.fps = 24
def act(self):
    nonlocal i
    i = i + 1
    print(board.frame, i)
    if board.frame == 120:
        test_instance.assertEqual(i, 13)
        test_instance.assertEqual(board.frame, 120)
Return type

int

get_colors_at_line(line)[source]

Gets all colors in a line. A line is a list of board_positions

Parameters

line – the line

Returns

A list of all colors found at the line

get_mouse_position()[source]

Gets the current mouse_position

Returns

Returns the mouse position if mouse is on board. Returns “None” otherwise

Examples

This example shows you how to use the mouse_position

>>> def act(self):
>>>     mouse = self.board.get_mouse_position()
>>>     if mouse:
>>>         self.point_towards_position(mouse)
Return type

Optional[BoardPosition]

get_tokens_at_rect(rect)[source]

Gets all Tokens which are colliding with a given rect.

Parameters
  • rect – The rect

  • singleitem – Should the method return a single token (faster) or all tokens at rect (slower)

  • exclude – Exclude a token

  • token_type – Filter return values by token type

Returns: A single token or a list of tokens at rect

Return type

Union[Token, list]

get_tokens_by_pixel(pixel)[source]

Gets all tokens by Pixel.

Parameters

pixel – the pixel-coordinates

Returns

A list of tokens

Examples

>>> position = board.get_mouse_position()
>>> tokens = board.get_tokens_by_pixel(position)
Return type

list

property height: int

See container_height

Return type

int

property image: pygame.Surface

The current displayed image

Return type

Surface

load_board_from_db(file)[source]

Loads a sqlite db file.

load_tokens_from_db(file, token_classes)[source]

Loads all tokens from db. Usually you load the tokens in __init__() or in on_setup()

Parameters
  • file (str) – reference to db file

  • token_classes (list) – a list of all Token Classes which should be imported.

Returns

All Tokens

Return type

[type]

Return type

List[Token]

on_started()[source]

The on_started method is executed after starting the board. Afterwards the individual lines are executed step by step with some delay (depending on board.speed).

Examples:

Registering a on_started-Method

@karaboard.register
def on_started(self):
    self.kara.move_right()
    self.kara.move_right()
Raises

NotImplementedOrRegisteredError – The error is raised when method is not overwritten or registered.

play_music(path)[source]

plays a music by path

Parameters

path – The path to the music

Returns:

register(method)[source]

Used as decorator e.g. @register def method…

Return type

callable

remove_background(background=None)[source]

Removes a background from board

Parameters

index – The index of the new background. Defaults to -1 (last background)

remove_from_board(token)[source]

removes a token from board. The method is called with token.remove()

Parameters

token – The token to remove from board.

Returns:

remove_tokens_from_rect(rect, token_class=None, exclude=None)[source]

Removes all tokens in an area

Parameters
  • rect – A rectangle or a tuple (which is automated converted to a rectangle with tile_size

  • token_class – The class of the tokens which should be removed

  • exclude – A token which should not be removed e.g. the actor itself

Returns: all tokens in the area

repaint()[source]

Implemented in subclasses

reset()[source]

Resets the board Creates a new board with init-function - recreates all tokens and actors on the board.

Examples

Restarts flappy the bird game after collision with pipe:

>>> def on_sensing_collision_with_pipe(self, other, info):
>>>    self.board.is_running = False
>>>    self.board.reset()
property rows: int

The number of rows

Return type

int

run(fullscreen=False, fit_desktop=False, replit=False, event=None, data=None)[source]

The method show() should always called at the end of your program. It starts the mainloop.

Examples

>>> my_board = Board() # or a subclass of Board
>>> my_board.show()
save_to_db(file)[source]

Saves the current board an all actors to database. The file is stored as db file and can be opened with sqlite.

Parameters

file – The file as relative location

Returns:

property speed: int

How often are events and game logic processed?

The game logic can be called more often or less often independently from board.fps

Examples:

board.speed = 10
board.fps = 24
def act(self):
    nonlocal i
    i = i + 1
    print(board.frame, i)
    if board.frame == 120:
        test_instance.assertEqual(i, 13)
        test_instance.assertEqual(board.frame, 120)
Return type

int

stop(frames=1)[source]

stops the board in n-frames

switch_background(background)[source]

Switches the background of costume

Parameters

index – The index of the new costume. If index=-1, the next costume will be selected

Returns

The new costume

Return type

Background

property tile_margin: int

The number of columns

Return type

int

property tile_size: int

The number of columns

Return type

int

property tokens: pygame.sprite.LayeredDirty

A list of all tokens registered to the grid.

Return type

LayeredDirty

update()[source]

Implemented in subclasses

property width: int

See container_width

Return type

int

property window: app.app.App

Gets the parent window

Returns

The window

Return type

App