Board#

`Board is the base class for boards.

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

Warning

If you instanciate a Board, it will be handled as PixelBoard`. You can’t create instances of BaseBoard

Board#

class miniworldmaker.boards.board_templates.pixel_board.board.Board(view_x=400, view_y=400, tile_size=1)[source]#

A board is a playing field on which tokens can move.

A board has a background and provides basic functions for the positioning of tokens and for the collision detection of tokens, which can be queried via the sensors of the tokens.

You can create your own board by creating a class that inherits from Board or you can directly create a board object of type Board or one of its child classes (TiledBoard, PhysicsBoard, …).

Board

A board for pixel accurate games.

  • The position of a token on a Board is the pixel at topleft of token.

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

  • Two tokens collide when their sprites overlap.

Asteroids

Other Boards:

  • TiledBoard: For Boards using Tiles, like rogue-like rpgs, see TiledBoard)

  • PhysicsBoard: For Boards using the PhysicsEngine, see PhysicsBoard)

Examples

Creating a TiledBoard Object:

from miniworldmaker import *

my_board = TiledBoard()
my_board.columns = 30
my_board.rows = 20
my_board.tile_size = 20

Creating a TiledBoard-Subclass.

import miniworldmaker

class MyBoard(miniworldmaker.TiledBoard):

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

Creating a Board Object:

from miniworldmaker import *

my_board = Board()
my_board.columns = 300
my_board.rows = 200

Creating a Board Subclass

import miniworldmaker

class MyBoard(miniworldmaker.Board):

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

See also

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

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

  • tile_size – Size of tiles (1 for normal Boards, can differ for TiledBoards)

Public Data Attributes:

speed

speed defines how often the method act() will be called.

fps

Frames per second shown on the screen.

width

Gets width of board in pixels.

height

Gets height of board in pixels.

boundary_x

The x-boundary (defaults to view_size)

boundary_y

The y-boundary (defaults to view_size)

viewport_width

int

viewport_height

The y-boundary (defaults to view_size)

columns

int

rows

int

camera_x

camera_y

tile_size

Tile size of each tile, if board has tiles

size

Set the size of board

default_fill_color

Set default fill color for borders and lines

default_is_filled

default_stroke_color

Set default stroke color for borders and lines.

default_border_color

Set default border color for borders and lines.

default_border

Sets default border color for tokens

tokens

A list of all tokens registered to the board.

backgrounds

Returns all backgrounds of the board as list.

background

Returns the current background

container_width

The width of the container

container_height

The height of the container

has_background

bool

registered_events

set

image

The current displayed image

fill_color

color

Inherited from BaseBoard

subclasses

surface

class_name

str

window

Gets the parent window

Inherited from Container

surface

container_width

The width of the container

container_height

The height of the container

window

Gets the parent window

size

Set the size of board

rect

topleft

window_docking_position

width

Gets width of board in pixels.

height

Gets height of board in pixels.

Public Methods:

__init__([view_x, view_y, tile_size])

is_position_on_the_board(pos)

Checks if position is on the board.

is_position_on_the_board(pos)

Checks if position is on the board.

is_position_on_the_board(pos)

Checks if position is on the board.

contains_rect(rect)

Detects if rect is completely on the board.

setup_board()

borders(value)

Gets all borders from a source (Position or Rect).

set_tile_size(value)

default_fill(value)

Set default fill color for borders and lines

get_background()

Returns the current background

switch_background(background)

Switches the background

remove_background([background])

Removes a background from board

set_background(source)

Adds a new background to the board

add_background(source)

Adds a new background to the board

start()

Starts the board, if board is not running.

stop([frames])

Stops the board.

start_listening()

stop_listening()

clear()

Alias of clean

clean()

removes all tokens

run([fullscreen, fit_desktop, replit, ...])

The method show() should always be called at the end of your program.

init_display()

play_sound(path)

plays sound from path

stop_sounds()

play_music(path)

plays a music from path

stop_music()

stops a music

get_mouse_position()

Gets the current mouse_position

get_mouse_x()

Gets x-coordinate of mouse-position

get_mouse_y()

Gets y-coordinate of mouse-position

get_prev_mouse_position()

gets mouse-position of last frame

is_mouse_pressed()

Returns True, if mouse is pressed

is_mouse_left_pressed()

Returns True, if mouse left button is pressed

is_mouse_right_pressed()

Returns True, if mouse right button is pressed

send_message(message[, data])

Sends broadcast message

quit([exit_code])

quits app and closes the window

reset()

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

switch_board(new_board)

Switches to another board

get_color_from_pixel(position)

Returns the color at a specific position

get_from_pixel(position)

Gets Position from pixel

get_board_position_from_pixel(pixel)

Alias for get_from_pixel

to_pixel(position)

on_setup()

Overwrite or register this method to call on_setup-Actions

__str__()

Return str(self).

add_to_board(token, position)

Adds a Token to the board.

detect_tokens(position)

Gets all tokens which are found at a specific position.

detect_tokens(position)

Gets all tokens which are found at a specific position.

get_tokens_from_pixel(pixel)

repaint()

Implemented in subclasses

update()

The mainloop, called once per frame.

handle_event(event[, data])

Event handling

register(method)

Used as decorator e.g.

unregister(method)

direction(point1, point2)

distance_to(pos1, pos2)

direction_to(pos1, pos2)

rtype

Direction

Inherited from BaseBoard

__init__()

get_token_connector(token)

rtype

TokenConnector

add_container(container, dock[, size])

add_board(position, board[, width])

remove_container(container)

load_board_from_db(file)

Loads a sqlite db file.

load_tokens_from_db(file, token_classes)

Loads all tokens from db.

save_to_db(file)

Saves the current board an all actors to database.

screenshot([filename])

Creates a screenshot in given file.

get_background()

Implemented in subclass

get_columns_by_width(width)

get_rows_by_height(height)

Inherited from Container

__init__()

on_change()

implemented in subclasses

add_to_window(app, dock[, size])

update_width_and_height()

repaint()

Implemented in subclasses

blit_surface_to_window_surface()

remove()

Implemented in subclasses

handle_event(event, data)

get_event(event, data)

Implemented in subclasses

is_in_container(x[, y])

rtype

bool

position_is_in_container(pos)

rtype

bool

update()

Implemented in subclasses

get_local_position(position)

rtype

tuple

Private Data Attributes:

_abc_impl

Inherited from BaseBoard

_abc_impl

Inherited from ABC

_abc_impl

Private Methods:

_create_event_manager()

_act_all()

Overwritten in subclasses, e.g.

_tick_timed_objects()

Inherited from BaseBoard

_get_camera_manager_class()

_get_token_connector_class()

needed by get_token_connector in parent class


add_background(source)[source]#

Adds a new background to the board

If multiple backgrounds are added, the last adds background will be set as active background.

Parameters

source – The path to the first image of the background or a color (e.g. (255,0,0) for red or “images/my_background.png” as path to a background.

Examples

Add multiple Backgrounds:

from miniworldmaker import *

board = Board()
board.add_background((255, 0 ,0)) # red
board.add_background((0, 0 ,255)) # blue
board.run() # Shows a blue board.
Returns

The new created background.

Return type

Background

property background: Background#

Returns the current background

Return type

Background

property backgrounds: list#

Returns all backgrounds of the board as list.

Return type

list

borders(value)[source]#

Gets all borders from a source (Position or Rect).

Parameters

value – Position or rect

Returns

A list of borders, e.g. [“left”, “top”], if rect is touching the left a top border.

Return type

list

property boundary_x: int#

The x-boundary (defaults to view_size)

Return type

int

property boundary_y: int#

The y-boundary (defaults to view_size)

Return type

int

property camera_x#
property camera_y#
clean()[source]#

removes all tokens

Examples

Clear a board:

from miniworldmaker import *
import random

board = Board()

for i in range(50):
    Token((random.randint(0,board.width), random.randint(0,board.height)))

@timer(frames = 50)
def clean():
    board.clear()

board.run()

Output:

Clean board
clear()[source]#

Alias of clean

see: Board.clean()

property color#
property columns: int#

int

Type

rtype

property container_height: int#

The height of the container

Return type

int

property container_width: int#

The width of the container

Return type

int

contains_position(pos)#

Checks if position is on the board.

Returns

True, if Position is on the board.

contains_rect(rect)[source]#

Detects if rect is completely on the board.

Parameters

rect – A rectangle as tuple (top, left, width, height)

property default_border#

Sets default border color for tokens

Note

You must also set a border for token.

Examples

Set default border for tokens:

from miniworldmaker import *

board = Board(210,80)
board.default_border_color = (0,0, 255)
board.default_border = 1

t = Token((10,10))

board.run()
property default_border_color#

Set default border color for borders and lines.

Note

board.default_border_color does not have an effect, if no border is set.

You must also set board.border > 0.

Examples

Create tokens with and without with border

from miniworldmaker import *

board = Board(210,80)
board.default_border_color = (0,0, 255)
board.default_border = 1

t = Token((10,10))

t2 = Token ((60, 10))
t2.border_color = (0,255, 0)
t2.border = 5 # overwrites default border

t3 = Token ((110, 10))
t3.border = None # removes border

t4 = Token ((160, 10))
t4.add_costume("images/player.png") # border for sprite

board.run()

Output:

borders
default_fill(value)[source]#

Set default fill color for borders and lines

property default_fill_color#

Set default fill color for borders and lines

property default_is_filled#
property default_stroke_color#

Set default stroke color for borders and lines. (equivalent to border-color)

detect_tokens(position)[source]#

Gets all tokens which are found at a specific position.

Parameters

position – Position, where tokens should be searched.

Returns

A list of tokens

Examples

Get all tokens at mouse position:

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

List[Token]

direction(point1, point2)[source]#
static direction_to(pos1, pos2)[source]#
Return type

Direction

static distance_to(pos1, pos2)[source]#
property fill_color#
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
    if board.frame == 120:
        test_instance.assertEqual(i, 13)
        test_instance.assertEqual(board.frame, 120)
Return type

int

get_background()[source]#

Returns the current background

Return type

Background

get_board_position_from_pixel(pixel)[source]#

Alias for get_from_pixel

get_color_from_pixel(position)[source]#

Returns the color at a specific position

Examples

from miniworldmaker import *

board = Board((100,60))

@board.register
def on_setup(self):
    self.add_background((255,0,0))
    print(self.get_color_from_pixel((5,5)))

board.run()

Output: (255, 0, 0, 255)

get color of red screen
Parameters

position – The position to search for

Returns

The color

Return type

tuple

get_from_pixel(position)[source]#

Gets Position from pixel

PixelBoard: the pixel position is returned TiledBoard: the tile-position is returned

Parameters

position (Union[Position, Tuple]) – Position as pixel coordinates

Return type

Optional[Position]

Returns

The pixel position, if position is on the board, None if position is not on Board.

get_mouse_position()[source]#

Gets the current mouse_position

Returns

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

Examples

Create circles at current mouse position:

from miniworldmaker import *

board = PixelBoard()

@board.register
def act(self):
    c = Circle(board.get_mouse_position(), 40)
    c.color = (255,255,255, 100)
    c.border = None

board.run()

Output:

Circles at mouse-position
Return type

Optional[Position]

get_mouse_x()[source]#

Gets x-coordinate of mouse-position

Return type

int

get_mouse_y()[source]#

Gets y-coordinate of mouse-position

Return type

int

get_prev_mouse_position()[source]#

gets mouse-position of last frame

get_tokens_at_position(position)#

Gets all tokens which are found at a specific position.

Parameters

position – Position, where tokens should be searched.

Returns

A list of tokens

Examples

Get all tokens at mouse position:

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

List[Token]

get_tokens_from_pixel(pixel)[source]#
property has_background: bool#

bool

Type

rtype

property height: int#

Gets height of board in pixels.

(for tiledboard: rows * tile_size)

Return type

int

property image: Surface#

The current displayed image

Return type

Surface

init_display()[source]#
is_mouse_left_pressed()[source]#

Returns True, if mouse left button is pressed

Return type

bool

is_mouse_pressed()[source]#

Returns True, if mouse is pressed

Return type

bool

is_mouse_right_pressed()[source]#

Returns True, if mouse right button is pressed

Return type

bool

is_position_on_board(pos)#

Checks if position is on the board.

Returns

True, if Position is on the board.

is_position_on_the_board(pos)[source]#

Checks if position is on the board.

Returns

True, if Position is on the board.

on_setup()[source]#

Overwrite or register this method to call on_setup-Actions

play_music(path)[source]#

plays a music from path

Parameters

path – The path to the music

Returns:

play_sound(path)[source]#

plays sound from path

quit(exit_code=0)[source]#

quits app and closes the window

register(method)[source]#

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

Return type

callable

property registered_events: set#

set

Type

rtype

remove_background(background=None)[source]#

Removes a background from board

Parameters

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

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#

int

Type

rtype

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

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

Examples

A minimal miniworldmaker-program:

from miniworldmaker import *
board = TiledBoard()
token = Token()
board.run()

Output:

Minimal program
send_message(message, data=None)[source]#

Sends broadcast message

A message can be received by the board or any token on board

set_background(source)[source]#

Adds a new background to the board

If multiple backgrounds are added, the last adds background will be set as active background.

Parameters

source – The path to the first image of the background or a color (e.g. (255,0,0) for red or “images/my_background.png” as path to a background.

Examples

Add multiple Backgrounds:

from miniworldmaker import *

board = Board()
board.add_background((255, 0 ,0)) # red
board.add_background((0, 0 ,255)) # blue
board.run() # Shows a blue board.
Returns

The new created background.

Return type

Background

set_tile_size(value)[source]#
setup_board()[source]#
property size: tuple#

Set the size of board

Examples

Create a board with 800 columns and 600 rows:

board = miniworldmaker.PixelBoard()
board.size = (800, 600)
Return type

tuple

property speed: int#

speed defines how often the method act() will be called.

If e.g. speed = 30, the game logic will be called every 30th-frame.

Note

You can adjust the frame-rate with board.fps

Examples

Set speed and fps.

from miniworldmaker import *

board = Board()
board.size = (120,210)

@board.register
def on_setup(self):
    board.fps = 1
    board.speed = 3

@board.register
def act(self):

board.run()

Output:

` 3 6 9 12 15 `

Return type

int

start()[source]#

Starts the board, if board is not running.

start_listening()[source]#
stop(frames=0)[source]#

Stops the board.

Parameters

frames (int, optional) – If frames is set, board will be stopped in n frames. . Defaults to 0.

stop_listening()[source]#
stop_music()[source]#

stops a music

Returns:

stop_sounds()[source]#
switch_background(background)[source]#

Switches the background

Parameters

background – The index of the new background or an Appearance. If index = -1, the next background will be selected

Examples

Switch between different backgrounds:

from miniworldmaker import *

board = Board()
token = Token()

board.add_background("images/1.png")
board.add_background((255, 0, 0, 255))
board.add_background("images/2.png")

@timer(frames = 40)
def switch():
    board.switch_background(0)

@timer(frames = 80)
def switch():
    board.switch_background(1)

@timer(frames = 160)
def switch():
    board.switch_background(2)

board.run()

Output:

Switch background
Returns

The new background

Return type

Background

switch_board(new_board)[source]#

Switches to another board

Parameters

new_board (Board) – _description_

property tile_size: int#

Tile size of each tile, if board has tiles

Returns

The tile-size in pixels.

Return type

int

to_pixel(position)[source]#
property tokens: LayeredDirty#

A list of all tokens registered to the board.

Return type

LayeredDirty

unregister(method)[source]#
update()[source]#

The mainloop, called once per frame.

Called in app.update() when update() from all containers is called.

property viewport_height: int#

The y-boundary (defaults to view_size)

Return type

int

property viewport_width: int#

int

Type

rtype

property width: int#

Gets width of board in pixels.

(for tiledboard: columns * tile_size)

Return type

int