Tokens have sensors, with which they can sense their environment and can e.g. detect other tokens at their position.

Detect an object#

A token can track another token at the same location by using the function on_detect_token.

def on_detect_token(self, other):

What happens here?#

  • The function on_detect_token will be called when the token detects another object at the same location.

  • The parameter other is a reference to the found object, so that so that you can directly access attributes and methods of this object (e.g. with other.move())

Compare with found object#

Often an action should only be executed if a certain object is found. is found.

This goes for example like this:

2def on_detect_token(self, other):
3    global player2
4    if other == player2:
5      print("I found you, player2!")

The comparison in line 6 checks whether the object is the same object as player2.


Excursus: Global Variables: Normally variables are only known only known within a method, e.g. to prevent side-effects when the same side effects when accessing the same variable in different places. variable in different places.

The approach with which variables from other program parts are accessed here is is simple and intuitive - but later you will try to avoid it. try to avoid this.

Extensive example#

The following code shows how you can prevent objects from moving through walls:

from miniworldmaker import *

board = TiledBoard()
board.columns = 8
board.rows = 2
board.speed = 30
player = token()

wall = token((4,0))

def act(self):
    if player.position != (0,4):
        player.direction = "right"

def on_detect_token(self, other):
    if other==wall:

Other sensors#

Check boundaries of the playing field#

You can also check if a character is at the borders of the playing field (or beyond). playing field (or beyond it):

Is the piece not on the playing field?###.#

def on_detecting_not_on_board(self):
  print("Warning: I'm not on the board!!!")


The following program simulates a fish swimming around:

from miniworldmaker import *

board.columns = 4
board.rows = 1
fish = token((0,0))
fish.costume.orientation = - 90
fish.direction = "right
def act(self):

def on_detecting_not_on_board(self):

Is the character at the boundaries of the playing field?

def on_detecting_borders(self, borders):
  print("Borders are here!", str(borders))

If a character is at the position (0,0) the following is printed: Borders are here! is printed: Borders are here! [‘right’, ‘top’]`


  • My collisions are not detected, what can I do?

    First test if the method is called at all, e.g. with:

    ``python @player.register def on_detect_token(self, token): print(token) …

    If the `print` statement is not called, then the sensor does not work.
    the sensor does not work.