Board

Board is the base class of TiledBoard and PixelBoard. It

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

Important

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

Board

class miniworldmaker.boards.board.Board(*args, **kwargs)[source]

Base Class for Boards.

You can create a custom board by inherit one of Board subclasses or by creating a board-object:

Examples

Creating a board object:

board = miniworldmaker.TiledBoard()
board.columns=20
board.rows=8
board.tile_size=40

A pixel-board in follow_the_mouse.py:

class MyBoard(PixelBoard):

def on_setup(self):
    self.add_image(path="images/stone.jpg")
    Robot(position=(50, 50))


board = MyBoard(800, 600)

A tiled-board:

def on_setup(self):
    self.add_image(path="images/soccer_green.jpg")
    self.player = Player(position=(3, 4))
    self.speed = 10
    stone = self.add_background(("images/stone.png"))
    stone.is_textured = True
    stone.is_scaled_to_tile = True


board = MyBoard(columns=20, rows=8, tile_size=42, tile_margin=0)

> See [Example](https://codeberg.org/a_siebel/miniworldmaker/src/branch/main/examples/basics)

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

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

add_background(source: Union[str, tuple])miniworldmaker.appearances.background.Background[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.

property columns

The number of columns

property container_height

The height of the container

property container_width

The width of the container

property fps

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)
classmethod from_db(file)[source]

Loads a sqlite db file.

get_colors_at_line(line: list)[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()Optional[miniworldmaker.board_positions.board_position.BoardPosition][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)
get_tokens_at_rect(rect: pygame.Rect, singleitem=False, exclude=None, token_type=None)Union[miniworldmaker.tokens.token.Token, list][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

get_tokens_by_pixel(pixel: tuple)list[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)
property height

See container_height

property image

The current displayed image

is_position_on_board(position: miniworldmaker.board_positions.board_position.BoardPosition)bool[source]

Tests if area or position is on board

Parameters

position – A rectangle or a position

Returns

True, if area is in grid

play_music(path: str)[source]

plays a music by path

Parameters

path – The path to the music

Returns:

register_sound(path)Sound[source]

Registers a sound effect to board-sound effects library

Parameters

path – The path to sound

Returns

the sound

remove_background()[source]

Removes a background from board

Parameters

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

remove_tokens_from_rect(rect: Union[tuple, pygame.Rect], token=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 – 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

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

The number of rows

run(fullscreen=False)[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

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)
stop(frames=1)[source]

stops the board in n-frames

switch_background(background: Union[int, Type[miniworldmaker.appearances.appearance.Appearance]])miniworldmaker.appearances.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

property tile_margin

The number of columns

property tile_size

The number of columns

property tokens

A list of all tokens registered to the grid.

property width

See container_width

property window

Gets the parent window

Returns

The window