Events#
Events sind Methoden, die Miniworlds automatisch aufruft. Registriere sie mit dem Decorator @...register an einer World oder einem Actor.
Hauptschleife#
@world.register
def on_setup(self):
self.add_background((255, 255, 255))
@world.register
def act(self):
print(self.frame)
on_setup läuft einmal, bevor die Welt startet. act läuft einmal pro Frame, solange die Welt läuft.
Tastatur#
Verwende einen tastenspezifischen Handler, wenn dich nur eine Taste interessiert:
@player.register
def on_key_down_w(self):
self.move_up()
@player.register
def on_key_pressed_space(self):
self.y -= 2
Verwende einen allgemeinen Handler, wenn du die aktuell gedrückten Tasten auswerten musst:
@player.register
def on_key_pressed(self, keys):
if "a" in keys:
self.move_left()
if "d" in keys:
self.move_right()
Tastatur-Events:
on_key_down(self, keys): einmal, wenn eine Taste gedrückt wirdon_key_pressed(self, keys): in jedem Frame, solange eine Taste gehalten wirdon_key_up(self, keys): wenn eine Taste losgelassen wirdon_key_down_<key>(self),on_key_pressed_<key>(self),on_key_up_<key>(self): tastenspezifische Handler ohnekeys-Parameteron_text_input(self, text): zusammengesetzter Text (einschließlich Umlauten und Leerzeichen); verwende dies für Texteingaben, z. B. in einemInput-Widget
Gängige Tastennamen sind w, a, s, d, space, left, right, up und down.
Maus#
@circle.register
def on_mouse_left(self, position):
if self.detect_point(position):
self.center = position
@circle.register
def on_clicked_left(self, position):
self.color = (255, 0, 0)
Maus-Events:
on_mouse_left(self, position),on_mouse_right(self, position),on_mouse_middle(self, position): solange eine Taste gehalten wirdon_mouse_left_down(self, position),on_mouse_left_up(self, position): Drücken und Loslassenon_mouse_motion(self, position): Mausbewegungon_clicked(self, position),on_clicked_left(self, position),on_clicked_right(self, position): Klick-Events für Actorson_mouse_over(self, position),on_mouse_enter(self, position),on_mouse_leave(self, position): Hover-Events für Actorson_wheel_up(self, position),on_wheel_down(self, position): Mausradon_tile_enter(self, tile),on_tile_leave(self, tile): die Maus betritt oder verlässt ein Hexfeld (nurHexWorld)
Sensoren und Ränder#
@player.register
def on_detecting_actor(self, other):
print("touching", other)
@player.register
def on_not_detecting_world(self):
self.remove()
Sensor- und Kollisions-Events:
on_detecting(self, other): irgendein erkannter Actoron_detecting_actor(self, other): irgendein Actor auf derselben Positionon_detecting_<ClassName>(self, other): Actors einer bestimmten Klasse, zum Beispielon_detecting_wallon_not_detecting(self): kein Actor erkannton_not_detecting_<ClassName>(self): kein Actor dieser Klasse erkannton_detecting_world(self): der Actor befindet sich in der Welton_not_detecting_world(self): der Actor befindet sich außerhalb der Welton_detecting_not_on_world(self): Alias füron_not_detecting_worldon_detecting_borders(self, borders): erhält eine Liste wie["left"]on_detecting_left_border(self),on_detecting_right_border(self),on_detecting_top_border(self),on_detecting_bottom_border(self)
Sowohl on_not_detecting_world als auch on_detecting_not_on_world werden akzeptiert.
Nachrichten#
Nachrichten sind nützlich, wenn Actors miteinander kommunizieren sollen, ohne direkte Verweise aufeinander zu halten.
@button.register
def on_clicked_left(self, position):
self.send_message("open_door")
@door.register_message("open_door")
def open(self, sender):
self.switch_costume("open")
Verwende send_message(message, data=None), um eine Nachricht zu senden. Mit @actor.register_message("message") oder @world.register_message("message") reagierst du darauf.
Fokus#
Actors können reagieren, wenn sie nach einem Klick den Fokus erhalten oder verlieren:
on_focus(self)on_focus_lost(self)