Contents

Boards: Board

Contents

Boards: 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

Creating a Board:

from miniworldmaker import *

myboard = Board()

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

Creating a TiledBoard:

from miniworldmaker import *

myboard = TiledBoard()

Examples

Creating a TiledBoard Object:

from miniworldmaker import *

myboard = TiledBoard()
myboard.columns = 30
myboard.rows = 20
myboard.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 *

myboard = Board()
myboard.columns = 300
myboard.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 the background of board.

background

Returns the background of board.

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 : py: class:BaseBoard

subclasses

class_name

str

window

Gets the parent window

Inherited from : py: class:Container

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

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

contains_position(pos)

Checks if position is on the board.

contains_rect(rect)

Detects if rect is completly on the board.

is_position_on_the_board(position)

Returns True if a position is 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()

Implemented in subclass

switch_background(background)

Switches the background of board

remove_background([background])

Removes a background from 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 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()

plays a music from path

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.

get_tokens_from_pixel(pixel)

Gets all tokens by Pixel.

get_tokens_at_position(position)

Alias for get_tokens_from_pixel

repaint()

Implemented in subclasses

update()

The mainloop, called once per frame.

act_all()

Overwritten in subclasses, e.g.

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 : py: class:BaseBoard

__init__([view_x, view_y, tile_size])

get_token_connector(token)

rtype:

TokenConnector

add_container(container, dock[, size])

remove_container(container)

blit_surface_to_window_surface()

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

Inherited from : py: class:Container

__init__([view_x, view_y, tile_size])

update_width_and_height()

repaint()

Implemented in subclasses

blit_surface_to_window_surface()

remove()

Implemented in subclasses

handle_event(event[, data])

Event handling

get_event(event, data)

Implemented in subclasses

is_in_container(x, y)

rtype:

bool

position_is_in_container(pos)

rtype:

bool

update()

The mainloop, called once per frame.

get_local_position(position)

rtype:

tuple

Private Data Attributes:

_abc_impl

Inherited from : py: class:BaseBoard

_abc_impl

Inherited from : py: class:ABC

_abc_impl

Private Methods:

_create_event_manager()

_tick_timed_objects()

Inherited from : py: class:BaseBoard

_get_camera_manager_class()

_get_token_connector_class()

needed by get_token_connector in parent class

Inherited from : py: class:Container

_add_to_window(app, dock[, size])


act_all()[source]

Overwritten in subclasses, e.g. physics_board

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 background

Returns the background of board.

property backgrounds

Returns the background of board.

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)[source]

Checks if position is on the board.

Returns:

True, if Position is on the the board.

contains_rect(rect)[source]

Detects if rect is completly on the board. :param rect: A rect :return: True, if rect is on the board.

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)

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

Direction

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
    print(board.frame, i)
    if board.frame == 120:
        test_instance.assertEqual(i, 13)
        test_instance.assertEqual(board.frame, 120)
Return type:

int

get_background()[source]

Implemented in subclass

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)

../_images/get_color.png

from __future__ import annotations

Alt:

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)[source]

Alias for get_tokens_from_pixel

Return type:

list

get_tokens_from_pixel(pixel)[source]

Gets all tokens by Pixel.

Parameters:

pixel – the pixel-coordinates

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

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: pygame.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_the_board(position)[source]

Returns True if a position is on the board.

Return type:

bool

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 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 received by the board or any token on board

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):
    print(board.frame)

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]

plays a music from path

Parameters:

path – The path to the music

Returns:

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

Switches the background of board

Parameters:
  • background – The index of the new background or an Appearance.

  • index=-1 (If) –

  • selected (the next background will be) –

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

Return type:

int

to_pixel(position)[source]
property tokens: pygame.sprite.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() vom 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