Tokens

Token

class miniworldmaker.tokens.token.Token(position=None)[source]
add_costume(path: str) → 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.

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

Examples

Create several tokens with images (in example createrobotworld.py)

>>>  class Robot(Actor):
>>>
>>>    def on_setup(self):
>>>      self.add_image("images/robo_green.png")
>>>
>>>
>>>  class Wall(Token):
>>>    def on_setup(self):
>>>      self.add_image("images/rock.png")
>>>
>>>
>>>  class Gold(Token):
>>>    def on_setup(self):
>>>      self.add_image("images/stone_gold.png")
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

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”, …

../_images/movement3.jpg

Examples

Moving in rpg-basic-example.py:

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

Examples

flip a token in Example flipthefish.py

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

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

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.

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

Moves the actor to the last position (before the last movement”) The last position will always be safed, when a move is executed, so move_back can undo every move.

Examples

move_back as blocking-mechanism (in rpg-basic-template.py):

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

Moves token distance steps into a direction.

../_images/movement3.jpg
Parameters

direction – Direction as angle

Returns

The token itself

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

Moves token distance steps to a specific board_position

Parameters

position – The position to which the actor should move

Returns

The token itself

orientation

The orientation “corrects” a wrongly orientation of a token image. If orientation is != 0, the token is rotated by orientation degrees, before any other operation.

Examples

Sets orientation:

>>>  class Bird(Actor):
>>>
>>>    def on_setup(self):
>>>      self.add_image("images/fly.png")
>>>      self.orientation = 180
>>>      self.flip_x() #flips actor

Instead of using token.orientation, you can also use token.costume.orientation:

>>>  def on_setup(self):
>>>    self.add_image("images/fly.png")
>>>    self.costume.orientation = 180
>>>    self.flip_x() #flips 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

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

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 rectangle describing the 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()
sensing_borders(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 tokens found by Sensor

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

Senses if token is on board

Parameters

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

Returns: True if token is on board

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

Senses if tokens are on tokens position. :param token_type: filter by token type. Enter a class_name of tokens to look for here :param distance: Specifies the distance in front of the actuator to which the sensor reacts.

Returns: First token found by Sensor

Examples

Sensing a fireplace in rpg-basic-template.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)[source]

Senses if tokens are on tokens position :param token_type: filter by token type. Enter a class_name of tokens to look for here :param distance: Specifies the distance in front of the actuator to which the sensor reacts.

Returns: All tokens found by Sensor

size

Size of the token

switch_costume(index=-1) → miniworldmaker.appearances.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

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)

Examples

>>> self.text = TextToken((1,1), "Hello World")
Creates a new text_token
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.
>>> number = self.score.get_number()
Gets the number stored in the NumberToken
>>> self.score.set_number(3)
Sets the number stored in the NumberToken
get_number() → int[source]
Returns

The current number

Examples

>>> number = self.number_token.get_number()
Gets the number stored in the NumberToken
inc()[source]

Increases the number by one

set_number(number)[source]

Sets the number

Parameters

number – The number which should be displayed

Examples

>>> self.number_token.set_number(3)
Sets the number stored in the NumberToken

Shape

class miniworldmaker.tokens.shapes.Shape(position: tuple = None, color: tuple = (255, 255, 255, 255))[source]

Base class for Shapes

Point

class miniworldmaker.tokens.shapes.Point(position=None, thickness: int = 1, color: tuple = (255, 255, 255, 255))[source]

A Point-Shape

Parameters
  • position – The position as 2-tuple

  • thickness – The thickness of the point (1: pixel, >1: The point is rendered as circle)

  • color – The color as 4-tuple (r, g, b, alpha)

Examples

Creates a red point at position (200,100):

>>> Point((200, 100), 1, color=(255,0,0,255))

Circle

class miniworldmaker.tokens.shapes.Circle(position=None, radius: int = 10, thickness: int = 1, color: tuple = (255, 255, 255, 255))[source]

A Circle-Shape.

Parameters
  • position – The position as 2-tuple. The circle is created with its center at the position

  • radius – The radius of the circle

  • thickness – The thickness of the bounding line (0: The object is filled)

  • color – The color as 4-tuple (r, g, b, alpha)

Examples

Example Creation of a circle

>>> Circle((200, 100), 20, 1, color=(255,0,0,255))
Creates a red circle at position (200,100) with radius 20

Example Creation of a filled circle

>>> Circle((200, 100), 20, 0, color=(255,0,0,255))
Creates a red circle at position (200,100) with radius 20. The circle is filled
radius

Gets the radius of the circle. If you change the circle-size (e.g. with self.size = (x, y), the radius value will be changed too.

Returns: The radius

Ellipse

class miniworldmaker.tokens.shapes.Ellipse(position=None, width=20, height=20, thickness: int = 1, color: tuple = (255, 255, 255, 255))[source]

An Ellipse-Shape.

Parameters
  • position – The position as 2-tuple. The circle is created with its center at the position

  • width – The width of the ellipse

  • height – The height of the ellipse

  • thickness – The thickness of the bounding line (0: The object is filled)

  • color – The color as 4-tuple (r, g, b, alpha)

Examples

Example Creation of an ellipse

>>> Circle((200, 100), 20, 40, 1, color=(255,0,0,255))
Creates an red ellise at position (200,100) with width 20 and height 40

Example Creation of a filled ellipse

>>> Circle((200, 100), 20, 40, 0, color=(255,0,0,255))
Creates a red circle at position (200,100) with width 20 and height 40. The ellipse is filled
height

Gets the height of the ellipse If you change the ellipse-size (e.g. with self.size = (x, y), the value will be changed too.

Returns: The height of the ellipse

width

Gets the width of the ellipse If you change the ellipse-size (e.g. with self.size = (x, y), the value will be changed too.

Returns: The width of the ellipse

Line

class miniworldmaker.tokens.shapes.Line(start_position: tuple, end_position: tuple, thickness: int = 1, color: tuple = (255, 255, 255, 255))[source]

A Line-Shape.

Parameters
  • start_position – The start_position as 2-tuple.

  • end_position – The end_position as 2-tuple.

  • thickness – The thickness of the bounding line

  • color – The color as 4-tuple (r, g, b, alpha)

Examples

Example Creation of a line

>>> Line((200, 100), (400,100), 1, color=(255,0,0,255))
Creates a line from (200, 100) to (400, 100)

Rectangle

class miniworldmaker.tokens.shapes.Rectangle(topleft, width, height, thickness=1, color: tuple = (255, 255, 255, 255))[source]

A Rect-Shape.

Parameters
  • topleft – Topleft Position of Rect

  • height – The height of the rect

  • width – The width of the rect

  • thickness – The thickness of the bounding line

  • color – The color as 4-tuple (r, g, b, alpha)

Examples

Example Creation of a polygon

>>> Rectangle((200, 100), 10, 10, 1, color=(255,0,0,255))
Creates a red rect with the topleft position (200, 100), the height 10 and the width 10

Polygon

class miniworldmaker.tokens.shapes.Polygon(pointlist, thickness=1, color: tuple = (255, 255, 255, 255))[source]

A Polygon-Shape.

Parameters
  • point-list – A list of points

  • thickness – The thickness of the bounding line

  • color – The color as 4-tuple (r, g, b, alpha)

Examples

Example Creation of a polygon

>>> Polygon([(200, 100), (400,100), (0, 0)], 1, color=(255,0,0,255))
Creates a red polygon with the vertices (200, 100) , (400, 100) and (0, 0)

Example Creation of a filled polygon

>>> Polygon([(200, 100), (400,100), (0, 0)], 0, color=(255,0,0,255))
Creates a red polygon with the vertices (200, 100) , (400, 100) and (0, 0)