Codesters FAQ is the official FAQ site for codesters.com. We will continue to update this blog with the most common questions we receive form teachers using Codesters.

Event and Event Handler

Using an Event is the primary way to make your program interactive. Events are found in the Event Toolkit

The most important thing to understand is the structure of an event.
  1. Event Definition: This defines the actions that will happen when the event occurs.
    Actions need to be indented 4 spaces inside of the definition
    The definition has to be before the Event Handler

  2. Event Handler: This is the part of the event that "listens" for the event to occur and calls the event then it does.
    Note: The Event Handler must come after the event definition. It usually follows immediately after the definition but can be placed anywhere in the program as long as it is after the definition.

Here is an example of the "Sprite Click" event. When the sprite is clicked, it will "disappear" sprite.hide() for 2 seconds and then "reappear" sprite.show(). These actions are indented 4 spaces (tab) in the event definition. The Event Handler is not indented and is always "listening" to see if the sprite is clicked and then calls the event.

sprite = codesters.Sprite("cat")

def click(sprite):
    sprite.hide()
    stage.wait(2)
    sprite.show()

sprite.event_click(click)