Additional sensors#
Checking the field boundaries#
You can also use sensors to check if an actor is at the edges or outside the playing field:
Is the piece no longer on the field?####
This function checks if a figure is still in the current world.
@player3.register
def on_not_detecting_world(self):
print("Warning: I'm not on the world!!!")
Example: A fish that swims back at the edges of the playing field#
The following program simulates a fish that automatically turns around at the edges of the playing field:
from miniworlds import TiledWorld, Actor
world = TiledWorld()
world.columns = 4
world.rows = 1
world.add_background("images/water.png")
fish = Actor((0,0))
fish.add_costume("images/fish.png")
fish.costume.orientation = -90
fish.direction = "right"
@fish.register
def act(self):
self.move()
@fish.register
def on_not_detecting_world(self):
self.move_back()
self.flip_x() # Der Fisch dreht um, wenn er den Spielfeldrand erreicht
world.run()
Ausgabe#
Explanation#
The Methode
on_not_detecting_world
wird nur aufgerufen, wenn erkannt wird, dass sich der Fisch nicht mehr in der Welt befindet. Er wird mit move_back wieder zurückbewegt und anschließend gedreht.
Checking for field boundaries#
You can also check if the boundaries of the playing field are reached or touched:
Is the piece at the edges of the playing field?**#
@player4.register
def on_detecting_borders(self, borders):
print("Borders are here!", str(borders))
Explanation:
If a piece is at the edges of the playing field (e.g., at position
(0,0)
), it will output:Borders are here! ['right', 'top']
.