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.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 colission detection of tokens, which can be queried via the sensors of the tokens.

You can create a custom board by inherit from Board or one of Board subclasses (e.g. TiledBoard or PhysicsBoard) or by creating a board-object:

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:
  • columns – columns of new board (default: 40)

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

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

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.

Inherited from : py: class:BaseBoard

subclasses

container_width

The width of the container

container_height

The height of the container

has_background

bool

window

Gets the parent window

class_name

str

registered_events

set

image

The current displayed image

tile_size

Tile size of each tile, if board has tiles

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:

default_fill(value)

Set default fill color for borders and lines

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.

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.

play_sound(path)

plays sound from path

play_music(path)

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

screenshot([filename])

Creates a screenshot in given file.

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

Inherited from : py: class:BaseBoard

__init__([view_x, view_y, tile_size])

get_token_connector(token)

rtype:

TokenConnector

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.

__str__()

Return str(self).

remove_from_board(token)

Removes a token from board.

add_to_board(token, position)

Adds a Token to the board.

blit_surface_to_window_surface()

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

Implemented in subclasses

act_all()

handle_event(event[, data])

Event handling

find_colors(rect, color[, threshold])

register(method)

Used as decorator e.g.

unregister(method)

add_container(container, dock[, size])

on_started()

The on_started method is executed after starting the board.

borders(value)

Gets all borders a rect is touching

is_on_board()

rtype:

bool

set_tile_size(value)

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

Implemented in subclasses

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:

_get_token_connector_class()

needed by get_token_connector in parent class

Inherited from : py: class:BaseBoard

_get_camera_manager_class()

_get_token_connector_class()

needed by get_token_connector in parent class

_run_next_line_in_started_method()

_run_line(method, line_number)

_tick_timed_objects()

_filter_tokens_by_type(token_list, token_type)

Inherited from : py: class:Container

_add_to_window(app, dock[, size])


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.

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 columns: int

int

Type:

rtype

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)

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

Position

Returns:

The pixel position, if position is on 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 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

property height: int

Gets height of board in pixels.

(for tiledboard: rows * tile_size)

Return type:

int

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

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

remove_background(background=None)[source]

Removes a background from board

Parameters:

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

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
screenshot(filename='screenshot.jpg')[source]

Creates a screenshot in given file.

Parameters:

filename – The location of the file. The folder must exist. Defaults to “screenshot.jpg”.

send_message(message, data=None)[source]

Sends broadcast message

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

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.

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.

switch_background(background)[source]

Switches the background of board

Parameters:

index – The index of the new background. 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_base.BaseBoard) – _description_

to_pixel(position)[source]
property tokens: pygame.sprite.LayeredDirty

A list of all tokens registered to the board.

Return type:

LayeredDirty

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