sge.Game

class sge.Game(width=640, height=480, fullscreen=False, scale=0, scale_proportional=True, scale_smooth=False, fps=60, delta=False, delta_min=15, grab_input=False, window_text=None, window_icon=None)

Class which handles the game.

This class handles most parts of the game which operate on a global scale, such as global game events. Before anything else is done with the SGE, an object either of this class or of a class derived from it must be created.

When an object of this class is created, it is automatically assigned to sge.game.

Note: Do not create multiple sge.Game objects. Doing so may cause errors.

width

The width of the game’s display.

height

The height of the game’s display.

fullscreen

Whether or not the game should be in fullscreen.

scale

A number indicating a fixed scale factor (e.g. 1 for no scaling, 2 for doubled size). If set to None or 0, scaling is automatic (causes the game to fit the window or screen).

scale_proportional

If set to True, scaling is always proportional. If set to False, the image will be distorted to completely fill the game window or screen. This has no effect unless scale is None or 0.

scale_smooth

Whether or not a smooth scaling algorithm (as opposed to a simple scaling algorithm such as nearest-neighbor) should be used.

fps

The rate the game should run in frames per second.

Note

This is only the maximum; if the computer is not fast enough, the game may run more slowly.

delta

Whether or not delta timing should be used. Delta timing affects object speeds, animation rates, and alarms.

delta_min

Delta timing can cause the game to be choppy. This attribute limits this by pretending that the frame rate is never lower than this amount, resulting in the game slowing down like normal if it is.

grab_input

Whether or not all input should be forcibly grabbed by the game. If this is True and the mouse cursor is invisible, the mouse will enter relative mode.

window_text

The text for the OS to display as the window title, e.g. in the frame of the window. If set to None, the SGE chooses the text.

window_icon

The name of the image file located in one of the directories specified in sge.image_directories to use as the window icon. If set to None, the SGE chooses the icon.

registered_classes

A list containing all classes which have been registered with sge.Game.register_class(). (Read-only)

sprites

A dictionary containing all loaded sprites, indexed by the sprites’ sge.Sprite.id attributes. (Read-only)

background_layers

A dictionary containing all loaded background layers, indexed by the layers’ sge.BackgroundLayer.id attributes. (Read-only)

backgrounds

A dictionary containing all loaded backgrounds, indexed by the backgrounds’ sge.Background.id attributes. (Read-only)

fonts

A dictionary containing all loaded fonts, indexed by the fonts’ sge.Font.id attributes. (Read-only)

sounds

A dictionary containing all loaded sounds, indexed by the sounds’ sge.Sound.id attributes. (Read-only)

music

A dictionary containing all loaded music, indexed by the music objects’ sge.Music.id attributes. (Read-only)

objects

A dictionary containing all sge.StellarClass objects in the game, indexed by the objects’ sge.StellarClass.id attributes. (Read-only)

rooms

A list containing all rooms in order of their creation. (Read-only)

current_room

The room which is currently active. (Read-only)

mouse

A sge.StellarClass object which represents the mouse cursor. Its sge.StellarClass.id attribute is "mouse" and its bounding box is a one-pixel square. Speed variables are determined by averaging all mouse movement during the last quarter of a second. Assigning to its sge.StellarClass.visible attribute controls whether or not the mouse cursor is shown. Setting its sprite sets the mouse cursor to that sprite.

sge.Game Methods

Game.__init__(width=640, height=480, fullscreen=False, scale=0, scale_proportional=True, scale_smooth=False, fps=60, delta=False, delta_min=15, grab_input=False, window_text=None, window_icon=None)

Constructor method.

Arguments set the respective initial attributes of the game. See the documentation for sge.Game for more information.

The created sge.Game object is automatically assigned to sge.game.

Game.start()

Start the game at the first room.

Can be called in the middle of a game to start the game over. If you do this, everything will be reset to its original state.

Game.end()

Properly end the game.

Game.pause(sprite=None)

Pause the game.

Arguments:

  • sprite – The sprite to show in the center of the screen while the game is paused. If set to None, the SGE chooses the image.

Normal events are not executed while the game is paused. Instead, events with the same name, but prefixed with event_paused_ instead of event_ are executed. Note that not all events have these alternative “paused” events associated with them.

Game.unpause()

Unpause the game.

Game.register_class(cls)

Register a class with the SGE.

Registered classes can be used to index objects by, e.g. for sge.Room.objects_by_class. A list of all currently registered classes can be found in registered_classes.

Game.project_dot(x, y, color)

Project a single-pixel dot onto the game window.

Arguments:

  • x – The horizontal location relative to the window to project the dot.
  • y – The vertical location relative to the window to project the dot.

See the documentation for sge.Sprite.draw_dot() for more information.

Game.project_line(x1, y1, x2, y2, color, thickness=1, anti_alias=False)

Project a line segment onto the game window.

Arguments:

  • x1 – The horizontal location relative to the window of the first endpoint of the projected line segment.
  • y1 – The vertical location relative to the window of the first endpoint of the projected line segment.
  • x2 – The horizontal location relative to the window of the second endpoint of the projected line segment.
  • y2 – The vertical location relative to the window of the second endpoint of the projected line segment.

See the documentation for sge.Sprite.draw_line() for more information.

Game.project_rectangle(x, y, width, height, fill=None, outline=None, outline_thickness=1)

Project a rectangle onto the game window.

Arguments:

  • x – The horizontal location relative to the window to project the rectangle.
  • y – The vertical location relative to the window to project the rectangle.

See the documentation for sge.Sprite.draw_rectangle() for more information.

Game.project_ellipse(x, y, width, height, fill=None, outline=None, outline_thickness=1, anti_alias=False)

Project an ellipse onto the game window.

Arguments:

  • x – The horizontal location relative to the window to position the imaginary rectangle containing the ellipse.
  • y – The vertical location relative to the window to position the imaginary rectangle containing the ellipse.
  • width – The width of the ellipse.
  • height – The height of the ellipse.
  • fill – The color of the fill of the ellipse.
  • outline – The color of the outline of the ellipse.
  • outline_thickness – The thickness of the outline of the ellipse.
  • anti_alias – Whether or not anti-aliasing should be used.

See the documentation for sge.Sprite.draw_ellipse() for more information.

Game.project_circle(x, y, radius, fill=None, outline=None, outline_thickness=1, anti_alias=False)

Project a circle onto the game window.

Arguments:

  • x – The horizontal location relative to the window to position the center of the circle.
  • y – The vertical location relative to the window to position the center of the circle.

See the documentation for sge.Sprite.draw_circle() for more information.

Game.project_sprite(sprite, image, x, y, blend_mode=None)

Project a sprite onto the game window.

Arguments:

  • x – The horizontal location relative to the window to project sprite.
  • y – The vertical location relative to the window to project sprite.

See the documentation for sge.Sprite.draw_sprite() for more information.

Game.project_text(font, text, x, y, width=None, height=None, color='black', halign=2, valign=8, anti_alias=True)

Project text onto the game window.

Arguments:

  • x – The horizontal location relative to the window to project the text.
  • y – The vertical location relative to the window to project the text.

See the documentation for sge.Sprite.draw_text() for more information.

sge.Game Event Methods

Game.event_game_start()

Game start event.

Called when the game starts. This is only called once (it is not called again when the game restarts) and it is always the very first event method called.

Game.event_game_end()

Game end event.

Called when the game ends. This is only called once and it is always the very last event method called.

Game.event_step(time_passed)

Global step event.

Called once each frame.

Arguments:

  • time_passed – The number of milliseconds that have passed during the last frame.
Game.event_key_press(key, char)

Key press event.

Called when a key on the keyboard is pressed.

Arguments:

  • char – The Unicode character associated with the key press, or an empty Unicode string if no Unicode character is associated with the key press.

See the documentation for sge.get_key_pressed() for more information.

Game.event_key_release(key)

Key release event.

Called when a key on the keyboard is released.

See the documentation for sge.get_key_pressed() for more information.

Game.event_mouse_move(x, y)

Mouse move event.

Called when the mouse moves.

Arguments:

  • x – The horizontal relative movement of the mouse.
  • y – The vertical relative movement of the mouse.
Game.event_mouse_button_press(button)

Mouse button press event.

Called when a mouse button is pressed.

See the documentation for sge.get_mouse_button_pressed() for more information.

Game.event_mouse_button_release(button)

Mouse button release event.

Called when a mouse button is released.

See the documentation for sge.get_mouse_button_pressed() for more information.

Game.event_joystick_axis_move(name, ID, axis, value)

Joystick axis move event.

Called when an axis on a joystick changes position.

Arguments:

  • name – The name of the joystick.
  • ID – The number of the joystick, where 0 is the first joystick.
  • value – The tilt of the axis as a float from -1 to 1, where 0 is centered, -1 is all the way to the left or up, and 1 is all the way to the right or down.

See the documentation for sge.get_joystick_axis() for more information.

Game.event_joystick_hat_move(name, ID, hat, x, y)

Joystick HAT move event.

Called when a HAT switch (also called the POV hat, POV switch, or d-pad) changes position.

Arguments:

  • name – The name of the joystick.
  • ID – The number of the joystick, where 0 is the first joystick.
  • x – The horizontal position of the HAT, where 0 is centered, -1 is left, and 1 is right.
  • y – The vertical position of the HAT, where 0 is centered, -1 is up, and 1 is down.

See the documentation for sge.get_joystick_hat() for more information.

Game.event_joystick_trackball_move(name, ID, ball, x, y)

Joystick trackball move event.

Called when a trackball on a joystick moves.

Arguments:

  • name – The name of the joystick.
  • ID – The number of the joystick, where 0 is the first joystick.
  • ball – The number of the trackball, where 0 is the first trackball on the joystick.
  • x – The horizontal relative movement of the trackball.
  • y – The vertical relative movement of the trackball.
Game.event_joystick_button_press(name, ID, button)

Joystick button press event.

Called when a joystick button is pressed.

Arguments:

  • name – The name of the joystick.
  • ID – The number of the joystick, where 0 is the first joystick.

See the documentation for sge.get_joystick_button_pressed() for more information.

Game.event_joystick_button_release(name, ID, button)

Joystick button release event.

Called when a joystick button is released.

Arguments:

  • name – The name of the joystick.
  • ID – The number of the joystick, where 0 is the first joystick.

See the documentation for sge.get_joystick_button_pressed() for more information.

Game.event_gain_keyboard_focus()

Gain keyboard focus event.

Called when the game gains keyboard focus. Keyboard focus is normally needed for key press and release events to be received.

Note

On some window systems, such as the one used by Windows, no distinction is made between keyboard and mouse focus, but on some other window systems, such as the X Window System, a distinction is made: one window can have keyboard focus while another has mouse focus. Be careful to observe the difference; failing to do so may result in annoying bugs, and you won’t notice these bugs if you are testing on a window manager that doesn’t recognize the difference.

Game.event_lose_keyboard_focus()

Lose keyboard focus event.

Called when the game loses keyboard focus. Keyboard focus is normally needed for key press and release events to be received.

Note

See the note in the documentation for event_gain_keyboard_focus().

Game.event_gain_mouse_focus()

Gain mouse focus event.

Called when the game gains mouse focus. Mouse focus may be needed for mouse motion, button press, and button release events to be received.

Note

See the note in the documentation for event_gain_keyboard_focus().

Game.event_lose_mouse_focus()

Lose mouse focus event.

Called when the game loses mouse focus. Mouse focus may be needed for mouse motion, button press, and button release events to be received.

Note

See the note in the documentation for event_gain_keyboard_focus().

Game.event_close()

Close event.

Called when the operating system tells the game to close, e.g. when the user presses the close button in the window frame. It is always called after any sge.Room.event_close() occurring at the same time.

Game.event_mouse_collision(other)

Default mouse collision event.

Proxy for sge.game.mouse.event_collision(). See the documentation for sge.StellarClass.event_collision() for more information.

Game.event_mouse_collision_left(other)

Left mouse collision event.

Proxy for sge.game.mouse.event_collision_left(). See the documentation for sge.StellarClass.event_collision_left() for more information.

Game.event_mouse_collision_right(other)

Right mouse collision event.

Proxy for sge.game.mouse.event_collision_right(). See the documentation for sge.StellarClass.event_collision_right() for more information.

Game.event_mouse_collision_top(other)

Top mouse collision event.

Proxy for sge.game.mouse.event_collision_top(). See the documentation for sge.StellarClass.event_collision_top() for more information.

Game.event_mouse_collision_bottom(other)

Bottom mouse collision event.

Proxy for sge.game.mouse.event_collision_bottom(). See the documentation for sge.StellarClass.event_collision_bottom() for more information.

Game.event_paused_key_press(key, char)

Key press event when paused.

See the documentation for sge.Game.event_key_press() for more information.

Game.event_paused_key_release(key)

Key release event when paused.

See the documentation for sge.Game.event_key_release() for more information.

Game.event_paused_mouse_move(x, y)

Mouse move event when paused.

See the documentation for sge.Game.event_mouse_move() for more information.

Game.event_paused_mouse_button_press(button)

Mouse button press event when paused.

See the documentation for sge.Game.event_mouse_button_press() for more information.

Game.event_paused_mouse_button_release(button)

Mouse button release event when paused.

See the documentation for sge.Game.event_mouse_button_release() for more information.

Game.event_paused_joystick_axis_move(name, ID, axis, value)

Joystick axis move event when paused.

See the documentation for sge.Game.event_joystick_axis_move() for more information.

Game.event_paused_joystick_hat_move(name, ID, hat, x, y)

Joystick HAT move event when paused.

See the documentation for sge.Game.event_joystick_hat_move() for more information.

Game.event_paused_joystick_trackball_move(name, ID, ball, x, y)

Joystick trackball move event when paused.

See the documentation for sge.Game.event_joystick_trackball_move() for more information.

Game.event_paused_joystick_button_press(name, ID, button)

Joystick button press event when paused.

See the documentation for sge.Game.event_joystick_button_press() for more information.

Game.event_paused_joystick_button_release(name, ID, button)

Joystick button release event when paused.

See the documentation for sge.Game.event_joystick_button_release() for more information.

Game.event_paused_gain_keyboard_focus()

Gain keyboard focus event when paused.

See the documentation for sge.Game.event_gain_keyboard_focus() for more information.

Game.event_paused_lose_keyboard_focus()

Lose keyboard focus event when paused.

See the documentation for sge.Game.event_lose_keyboard_focus() for more information.

Game.event_paused_gain_mouse_focus()

Gain mouse focus event when paused.

See the documentation for sge.Game.event_gain_mouse_focus() for more information.

Game.event_paused_lose_mouse_focus()

Lose mouse focus event when paused.

See the documentation for sge.Game.event_lose_mouse_focus() for more information.

Game.event_paused_close()

Close event when paused.

See the documentation for sge.Game.event_close() for more information.

Table Of Contents

Previous topic

Classes

Next topic

sge.Sprite

This Page