Tokens

Token

class miniworldmaker.tokens.token.Token(position=None)[source]
add_costume(path: str) → miniworldmaker.tokens.costume.Costume[source]

Adds a new costume to token. The costume can be switched with self.switch_costume(index)

Parameters

path – Path to the first image of new costume

Returns

The new costume.

add_image(path: str) → int[source]

Adds an image to the token. Best Practice: Image should me placed in ./images subfolder of your project, because these images will be loaded before running the main-loop.

Loading images is very slow, so this shouldn’t be done in runtime.

Parameters

path – Path to the images.

Returns

The image index

bounce_from_border(borders)[source]

Bounces the actor from a border.

Parameters

borders – A list of borders as strings e.g. [“left”, “right”]

Returns: The actor

center_x

x-value of token center-position

center_y

y-value of token center-position

static dir_to_unit_circle(direction)[source]

Transforms the current direction into standard-unit-circle direction

Parameters

value – The direction in scratch-style

Returns:

direction

Sets direction the token is oriented

0°: East, x degrees clock-wise otherwise You can also set the direction by String (“forward”, “up”, “down”, …

flip_x() → int[source]

Flips the actor by 180° degrees

classmethod from_center(center_position)[source]

Creates a token with center at center_position

Parameters

center_position – Center of token

Returns:

height

The height of the token in pixels

image

The image of the token:

Warning: You should not draw on the image or similar, as the image will be reloaded during animations or other operations and changes will not be applied.

Returns

The image of the token

is_flipped

If a token is flipped, it is mirrored via the y-axis.

Returns

True, if token is flipped

move(distance: int = 0)[source]

Moves actor distance steps.

Parameters
  • distance – Number of steps to move.

  • distance = 0, the actor speed will be used. (If) –

Returns

The actor

Examples

>>> class Robot(Token):
>>>    def act(self):
>>>         if self.sensing_on_board():
>>>             self.move()
move_in_direction(direction: Union[int, str])[source]

Moves actor distance steps into a direction.

Parameters

direction – Direction as angle

Returns

The actor

move_to(position: miniworldmaker.boards.board_position.BoardPosition)[source]

Moves actor distance steps into a direction.

Parameters

position – The position to which the actor should move

Returns

The actor

point_in_direction(direction) → int[source]

Token points in given direction

Parameters

direction – Direction the actor should point to

Returns

The new direction as integer

point_towards_position(destination) → int[source]

Token points towards a given position

Parameters

destination – The position to which the actor should pointing

Returns

The new direction

point_towards_token(other) → int[source]

Token points towards a given position

Parameters

other – The other token

Returns

The new direction

position

The position of the token as tuple (x, y)

rect

The surrounding Rectangle as pygame.Rect. Warning: If the token is rotated, the rect vertices are not the vertices of the token image.

Returns

The surrounding image

remove()[source]

Removes this actor from board

size

Size of the token

switch_costume(index=-1) → miniworldmaker.tokens.costume.Costume[source]

Switches the costume of token

Parameters

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

Returns: The new costume

turn_left(degrees: int = 90) → int[source]

Turns actor by degrees degrees left

Parameters

degrees – degrees in left direction

Returns

New direction

turn_right(degrees: int = 90)[source]

Turns token by degrees degrees right

Parameters

degrees – degrees in left direction

Returns

New direction

static unit_circle_to_dir(direction)[source]

Transforms the current direction from standard-unit-circle direction into scratch-style coordinates

Parameters

value – The direction in math unit circle style.

Returns:

width

The width of the token in pixels

x

The x-value of an token

Type

int

y

The x-value of an token

Type

int

Actor

class miniworldmaker.tokens.actor.Actor(position=None)[source]

Initializes a new actor

An actor is a specialized token.

Parameters
  • position – The position on the board as tuple.

  • None, the actor will not be placed on the board. (If) –

TextToken

class miniworldmaker.tokens.text_token.TextToken(position=None, text='', font_size=80, color=(255, 255, 255, 255))[source]

A Text-Token is a token which contains a Text.

You have to set the size of the token with self.size() manually so that the complete text can be seen.

Parameters
  • position – Top-Left position of Text

  • text – The initial text

  • font-size – The size of the font (default: 80)

  • color – The color of the font (default: white)

get_text()[source]

Gets the currently displayed tex

Returns: The currently displayed text

set_text(text)[source]

Sets the text of the token

Parameters

text – The text

NumberToken

class miniworldmaker.tokens.number_token.NumberToken(position, number=0, font_size=80, color=(255, 255, 255, 255))[source]

A number token shows a Number.

You have to set the size of the token with self.size() manually so that the complete text can be seen.

Parameters
  • position – Top-Left position of Number

  • number – The initial number

  • font-size – The size of the font (default: 80)

  • color – The color of the font (default: white)

Examples

>>> self.score = NumberToken(position = (0, 0), number=0)
Sets a new NumberToken to display the score.
get_number() → int[source]

Returns the current number

Returns: The current number

inc()[source]

Increases the number by one

set_number(number)[source]

Sets the number

Parameters

number – The number which should be displayed