Skip to content

Global

This page provides a list of the interface details in the game entry point main.py.

Called once after the loop ends, just before pygame shuts down.

Called once, before the loop starts.

Called every frame.

  • dt (float): seconds since the last frame.

Starts the game: initialises pygame, resolves the project folder, calls on_start() and then loops until the window is closed.

on_key_down(key, mod, unicode, scancode, window)

Section titled “on_key_down(key, mod, unicode, scancode, window)”

Fired when a key is pressed.

  • key (int): one of the studio.K_* constants.
  • mod (int): the pressed modifiers (studio.KMOD_*).
  • unicode (str): the character (may be empty for e.g. arrows).
  • scancode (int): the physical key.
  • window (pygame.Window | None): the window of the event, None when there is none.

on_key_up(key, mod, unicode, scancode, window)

Section titled “on_key_up(key, mod, unicode, scancode, window)”

Fired when a key is released.

  • key (int): one of the studio.K_* constants.
  • mod (int): the pressed modifiers (studio.KMOD_*).
  • unicode (str): the character (may be empty for e.g. arrows).
  • scancode (int): the physical key.
  • window (pygame.Window | None): the window of the event, None when there is none.

on_mouse_button_down(pos, btn, touch, clicks, window)

Section titled “on_mouse_button_down(pos, btn, touch, clicks, window)”

Fired when a mouse button goes down.

  • pos ((int, int)): pointer position in window coordinates.
  • btn (int): 1 left, 2 middle, 3 right, 4/5 the scroll wheel.
  • touch (int): 0 for a mouse.
  • clicks (int): counts a double click.
  • window (pygame.Window | None): the window of the event.

on_mouse_button_up(pos, btn, touch, clicks, window)

Section titled “on_mouse_button_up(pos, btn, touch, clicks, window)”

Fired when a mouse button is released.

  • pos ((int, int)): pointer position in window coordinates.
  • btn (int): 1 left, 2 middle, 3 right, 4/5 the scroll wheel.
  • touch (int): 0 for a mouse.
  • clicks (int): counts a double click.
  • window (pygame.Window | None): the window of the event.

on_mouse_motion(pos, rel, buttons, touch, window)

Section titled “on_mouse_motion(pos, rel, buttons, touch, window)”

Fired when the pointer moves.

  • pos ((int, int)): pointer position in window coordinates.
  • rel ((int, int)): movement since the last event.
  • buttons (tuple): state of the buttons.
  • touch (int): 0 for a mouse.
  • window (pygame.Window | None): the window of the event.

on_mouse_wheel(flipped, x, y, touch, precise_x, precise_y, window)

Section titled “on_mouse_wheel(flipped, x, y, touch, precise_x, precise_y, window)”

Fired when the wheel is turned.

  • flipped (bool): whether the scroll direction is flipped.
  • x (int): horizontal scroll.
  • y (int): positive when scrolling up.
  • touch (int): 0 for a mouse.
  • precise_x (float): precise horizontal scroll of touch pads.
  • precise_y (float): precise vertical scroll of touch pads.
  • window (pygame.Window | None): the window of the event.

A game controller (Game Controller API) was plugged in.

  • device_index (int): index of the device.

A game controller’s button mapping changed.

  • instance_id (int): index of the device instance.

A game controller was removed.

  • instance_id (int): index of the device instance.

on_joy_axis_motion(instance_id, axis, value)

Section titled “on_joy_axis_motion(instance_id, axis, value)”

An analogue stick/trigger moved.

  • instance_id (int): index of the device instance.
  • axis (int): index of the axis.
  • value (float): position of the axis, -1.0-1.0.

on_joy_ball_motion(instance_id, ball, rel)

Section titled “on_joy_ball_motion(instance_id, ball, rel)”

A trackball moved.

  • instance_id (int): index of the device instance.
  • ball (int): index of the trackball.
  • rel ((int, int)): relative movement.

A joystick button was pressed.

  • instance_id (int): index of the device instance.
  • button (int): index of the button.

A joystick button was released.

  • instance_id (int): index of the device instance.
  • button (int): index of the button.

A joystick was plugged in.

  • device_index (int): index of the device.

A joystick was unplugged.

  • instance_id (int): index of the device instance.

on_joy_hat_motion(instance_id, hat, value)

Section titled “on_joy_hat_motion(instance_id, hat, value)”

A d-pad changed.

  • instance_id (int): index of the device instance.
  • hat (int): index of the hat.
  • value ((int, int)): direction of the hat.

The window received a close request.

The window moved to another display.

  • display_index (int): index of the new display.

The pointer entered the window.

The window needs to be redrawn.

The window gained the input focus.

The window lost the input focus.

The window was hidden.

A hit-test request for the window (click-through).

The window’s ICC colour profile changed.

The pointer left the window.

The window was maximised.

The window was minimised.

The window moved.

  • x (int): new x position.
  • y (int): new y position.

A window resize finished.

  • x (int): new width.
  • y (int): new height.

The window was restored from being minimised or maximised.

The window was shown.

The window size changed.

  • x (int): new width.
  • y (int): new height.

The window was asked to take the focus.

A file drag from the operating system enters the window.

  • window (pygame.Window | None): the window of the event.

The file drag leaves the window.

  • window (pygame.Window | None): the window of the event.

A file was dropped onto the window.

  • file_path (str): path of the dropped file.
  • window (pygame.Window | None): the window of the event.

Text was dropped onto the window.

  • text (str): the dropped text.
  • window (pygame.Window | None): the window of the event.

on_finger_down(touch_id, finger_id, x, y, dx, dy, window)

Section titled “on_finger_down(touch_id, finger_id, x, y, dx, dy, window)”

Touch began.

  • touch_id (int): the touch device.
  • finger_id (int): the finger.
  • x (float): touch position.
  • y (float): touch position.
  • dx (float): movement since the last event.
  • dy (float): movement since the last event.
  • window (pygame.Window | None): the window of the event.

on_finger_motion(touch_id, finger_id, x, y, dx, dy, window)

Section titled “on_finger_motion(touch_id, finger_id, x, y, dx, dy, window)”

A touch moved.

  • touch_id (int): the touch device.
  • finger_id (int): the finger.
  • x (float): touch position.
  • y (float): touch position.
  • dx (float): movement since the last event.
  • dy (float): movement since the last event.
  • window (pygame.Window | None): the window of the event.

on_finger_up(touch_id, finger_id, x, y, dx, dy, window)

Section titled “on_finger_up(touch_id, finger_id, x, y, dx, dy, window)”

A touch ended.

  • touch_id (int): the touch device.
  • finger_id (int): the finger.
  • x (float): touch position.
  • y (float): touch position.
  • dx (float): movement since the last event.
  • dy (float): movement since the last event.
  • window (pygame.Window | None): the window of the event.

on_multi_gesture(touch_id, x, y, pinched, rotated, num_fingers, window)

Section titled “on_multi_gesture(touch_id, x, y, pinched, rotated, num_fingers, window)”

Pinch/rotate gesture.

  • touch_id (int): the touch device.
  • x (float): centre of the gesture.
  • y (float): centre of the gesture.
  • pinched (float): how much the fingers pinched.
  • rotated (float): rotation of the gesture in degrees.
  • num_fingers (int): number of fingers.
  • window (pygame.Window | None): the window of the event.

on_text_editing(text, start, length, window)

Section titled “on_text_editing(text, start, length, window)”

An IME composition is in progress.

  • text (str): the text being composed.
  • start (int): start of the selection being edited.
  • length (int): length of the selection being edited.
  • window (pygame.Window | None): the window of the event.

A character was typed. Input boxes that have focus consume the text first.

  • text (str): the typed character.
  • window (pygame.Window | None): the window of the event.

The app went to the background (mobile).

The app returned to the foreground (mobile).

The OS is low on memory.

The OS is shutting the app down.

The app is about to go to the background (mobile).

The app is about to return to the foreground (mobile).

An audio device appeared.

  • which (int): index of the device.
  • is_capture (bool): True for a recording device.

An audio device disappeared.

  • which (int): index of the device.
  • is_capture (bool): True for a recording device.

The clipboard changed.

The keyboard layout changed.

The system language changed.

The render device was lost — reload textures if you use them directly.

The render target was lost — reload textures if you use them directly.

Any event that is not handled above, e.g. one posted with pygame.event.post().

  • event (pygame.event.Event): the raw pygame event.

The duration of the last frame in seconds — the same dt that on_update(dt) receives. Handy inside event hooks, which have no dt parameter.

Seconds since the game started.

The frame rate the main loop targets (default 60).

Number of frames drawn since the game started.

The pointer position (x, y) inside the window.

Pressed state of the mouse buttons: (left, middle, right).

Tuple-like key state of the keyboard, indexed with the studio.K_* constants.

The surface the game renders to. Pass it to studio.load_scene(screen); after studio.set_window_size() it returns the new surface automatically.

True while that key is held, e.g. studio.is_key_pressed(studio.K_SPACE).

  • key (int): a studio.K_* constant.

True while that mouse button is held.

  • button (int): 1 left, 2 middle, 3 right.

True while the main loop is running.

Ends the game loop (on_quit() still runs).

Sets the target frame rate.

  • value (int): frames per second.