Token

Token

class miniworldmaker.tokens.token.Token(*args, **kwargs)[source]

Token is the basic class for all kinds of players, pieces and obstacles on the playing field

collision_type

The parameter collision_type specifies how collisions should be checked:

  • “default”: tile for TiledBoards, ‘maask’ for PixelBoards

  • “tile”: Are tokens on the same tile? (only TiledBoard)

  • “rect”: Are tokens colliding when checking their bounding - boxes? (Only PixelBoard)

  • “static-rect”: Are tokens colliding when checking circle with radius = bounding-box-radius.(Only PixelBoard)

  • “circle”: Are tokens colliding when checking circle with radius = bounding-box-radius.(Only PixelBoard)

  • “mask”: Are tokens colliding when checkig if their image masks are overlapping.

Type

string

add_costume(source: Union[str, list, Type[miniworldmaker.appearances.appearance.Appearance]] = (255, 0, 255, 100))miniworldmaker.appearances.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.

classmethod all_subclasses()[source]

Gets all subclasses from Token-Class

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

property center_x

x-value of token center-position

property 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

property direction

Sets direction of the token.

You can use a integer or a string to describe the direction

Options
  • 0, “up” - Look up

  • 90, “right”, - Look right

  • -90, “left”, - Look left

  • -180, 180, “down” - Look down

../_images/direction.png

Examples

Move in a direction with WASD-Keys

>>> def on_key_down(self, keys):
>>>    if "W" in keys:
>>>      self.direction = "up"
>>>    elif "S" in keys:
>>>      self.direction = "down"
>>>    elif "A" in keys:
>>>      self.direction = "left"
>>>    elif "D" in keys:
>>>      self.direction = "right"
>>>    self.move()
property direction_at_unit_circle

Gets the direction as value in unit circle (0° right, 90° top, 180° left…

flip_x()int[source]

Flips the actor by 180° degrees

../_images/flip_x.png

Examples

flip a token.

>>>  def on_sensing_not_on_board(self):
>>>    self.move_back()
>>>    self.flip_x()
classmethod from_center(center_position)[source]

Creates a token with center at center_position

Parameters

center_position – Center of token

property height

The height of the token in pixels

property image

The image of the token:

> Warning: You should not directly draw on the image > as the image will be reloaded during animations

property is_flipped

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

Returns

True, if token is flipped

Examples

flip a token in Example flipthefish.py

>>>  def on_sensing_not_on_board(self):
>>>    self.move_back()
>>>    self.flip_x()
move(distance: int = 0)[source]

Moves actor distance steps in current direction

../_images/move.png
Parameters

distance – Number of steps to move. If distance = 0, the actor speed will be used.

Returns

The moved token

Examples

if sensing_on_board, move forward:

>>> class Robot(Token):
>>>    def act(self):
>>>         if self.sensing_on_board():
>>>             self.move()
move_back()[source]

“Undo” the last move. Moves the actor to the last position and resets direction.

../_images/move_back.png
Returns

The moved token

Examples

move_back when field is blocked:

>>>  def on_sensing_wall(self, wall):
>>>    self.move_back()
move_in_direction(direction: Union[int, str], distance=1)[source]

Moves token distance steps into a direction.

../_images/move_in_direction.png
Options
  • 0, “up” - Look up

  • 90, “right”, - Look right

  • -90, “left”, - Look left

  • -180, 180, “down” - Look down

../_images/direction.png
Parameters

direction – Direction as angle

Returns

The token itself

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

Moves token distance to a specific board_posiition

Parameters
  • position – The position to which the actor should move. The position can be a 2-tuple (x, y)

  • will be converted to a board_position (which) –

../_images/move_to.png
Returns

The token itself

Examples:

move to (3, 2) on mouse_click

>>> def on_clicked_left(self, position):
>>>   self.move_to((3,2))
next_costume()[source]

Switches to the next costume of token

Parameters

next – If next is True, the next costume will be selected

Returns: The new costume

point_in_direction(direction)int[source]

Token points in given direction.

You can use a integer or a string to describe the direction

Parameters

direction as integer or string (The) –

Options
  • 0, “up” - Look up

  • 90, “right”, - Look right

  • -90, “left”, - Look left

  • -180, 180, “down” - Look down

../_images/direction.png

Examples

Move in a direction with WASD-Keys

>>> def on_key_down(self, keys):
>>>    if "W" in keys:
>>>      self.direction = "up"
>>>    elif "S" in keys:
>>>      self.direction = "down"
>>>    elif "A" in keys:
>>>      self.direction = "left"
>>>    elif "D" in keys:
>>>      self.direction = "right"
>>>    self.move()
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

Examples

Point towards mouse_position:

>>>  def act(self):
>>>  mouse = self.board.get_mouse_position()
>>>  if mouse:
>>>    self.point_towards_position(mouse)
>>>  self.move()
point_towards_token(other)int[source]

Token points towards another token.

Parameters

other – The other token

Returns

The new direction

property position

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

property rect

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

remove()[source]

Removes this token from board

Examples

Removes robots in thecrash.py :

>>>    def act(self):
>>>        self.move()
>>>        other = self.sensing_token(distance = 0, token_type=Robot)
>>>    if other:
>>>        explosion = Explosion(position=self.position)
>>>        self.remove()
>>>        other.remove()
remove_costume(costume=None)[source]

Removes a costume from token

Parameters

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

sensing_borders(distance: int = 0)[source]

Senses borders

../_images/sensing_borders.png
Parameters

distance – Specifies the distance in front of the actuator to which the sensor reacts.

Returns

True if border was found.

sensing_bottom_border(distance: int = 0)[source]

Senses borders :param distance: Specifies the distance in front of the actuator to which the sensor reacts.

Returns: True if border was found.

sensing_colors(colors, distance)[source]

Senses colors in board-background at token-position

Parameters
  • colors

  • distance – Specifies the distance in front of the actuator to which the sensor reacts.

Returns: All colors found by Sensor

sensing_left_border(distance: int = 0)[source]

Senses borders :param distance: Specifies the distance in front of the actuator to which the sensor reacts.

Returns: True if border was found.

sensing_on_board(distance: int = 0)bool[source]

Is the token on board if it is moving distance steps forward?

../_images/sensing_on_board.png
Parameters

distance – Specifies the distance in front of the actuator to which the sensor reacts.

Returns

True if token is on board

sensing_point(boardPosition)[source]

Is the token colliding with a specific (global) point?

Returns: True if point is below token

sensing_right_border(distance: int = 0)[source]

Senses borders :param distance: Specifies the distance in front of the actuator to which the sensor reacts.

Returns: True if border was found.

sensing_token(token_type=None, distance: int = 0, collision_type='default')[source]

Senses if tokens are on tokens position. Returns the first found token.

../_images/sensing_token.png
Parameters
  • token_type – filter by token type. Enter a class_name of tokens to look for here

  • distance – Specifies the distance in front of the actuator to which the sensor reacts.

  • collision_type – The type of collision which should be checked:

Returns

First token found by Sensor

Examples

Sensing a fireplace in rpg.py:

>>>  fireplace =  self.player.sensing_token(Fireplace)
>>>    if fireplace:
>>>      self.console.newline("Du zündest die Feuerstelle an.")
>>>      self.fireplace.burn()
sensing_tokens(token_type=None, distance: int = 0, collision_type='default')[source]

Senses if tokens are on tokens position. Returns a list of tokens.

../_images/sensing_tokens.png
Parameters
  • token_type – filter by token type. Enter a class_name of tokens to look for here

  • distance – Specifies the distance in front of the actuator to which the sensor reacts.

  • collision_type – The type of collision which should be checked:

Returns

All tokens found by Sensor

sensing_top_border(distance: int = 0)[source]

Senses borders :param distance: Specifies the distance in front of the actuator to which the sensor reacts.

Returns: True if border was found.

property size

Size of the token

start_physics()[source]

Starts the physics engine.

switch_costume(costume: Union[int, Type[miniworldmaker.appearances.appearance.Appearance]])miniworldmaker.appearances.costume.Costume[source]

Switches the costume of token

Parameters

next – If next is True, the next costume will be selected

Returns: The new costume

property topleft_x

x-value of token topleft-position

property topleft_y

x-value of token topleft-position

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

Turns actor by degrees degrees left

../_images/turn_left.png
Options:
  • You can set the value token.is_rotatable = False if you don’t want the token to be rotated.

Parameters

degrees – degrees in left direction

Returns

New direction

turn_right(degrees: int = 90)[source]

Turns token by degrees degrees right

../_images/turn_right.png
Options:
  • You can set the value token.is_rotatable = False if you don’t want the token to be rotated.

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.

property width

The width of the token in pixels

property x

The x-value of an token

Type

int

property y

The x-value of an token

Type

int