Contents
This module provides classes related to the graphical display.
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
This class is designed to be used as a singleton. Do not create multiple sge.dsp.Game objects. Doing so is unsupported and may cause errors.
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).
If a fixed scale factor is defined and the game is in fullscreen mode, the scale factor multiplied by width and height is used to suggest what resolution to use.
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.
A string indicating the type of scaling method to use. Can be one of the following:
Alternatively, this attribute can be set to one of the values in sge.SCALE_METHODS to request an exact scale method to use.
The value of this attribute is only a request. If this value is either an unsupported value or None, the fastest available scale method is chosen.
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.
Whether or not delta timing should be used. Delta timing affects object speeds, animation rates, and alarms.
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.
Indicates a higher frame rate cap than fps to allow the game to reach by using delta timing to slow object speeds, animation rates, and alarms down. If set to None, this feature is disabled and the game will not be permitted to run faster than fps.
This attribute has no effect unless delta is True.
Whether or not all input should be forcibly grabbed by the game. If this is True and sge.mouse.visible is False, the mouse will be in relative mode. Otherwise, the mouse will be in absolute mode.
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.
The path to the image file to use as the window icon. If set to None, the SGE chooses the icon. If the file specified does not exist, IOError is raised.
Whether or not collision events should be executed. Setting this to False will improve performence if collision events are not needed.
A dictionary containing the global alarms of the game. Each value decreases by 1 each frame (adjusted for delta timing if it is enabled). When a value is at or below 0, sge.dsp.Game.event_alarm() is executed with alarm_id set to the respective key, and the item is deleted from this dictionary.
A list containing all input event objects which have not yet been handled, in the order in which they occurred.
Note
If you handle input events manually, be sure to delete them from this list, preferably by getting them with list.pop(). Otherwise, the event will be handled more than once, which is usually not what you want.
The room which becomes active when the game first starts and when it restarts. Must be set exactly once, before the game first starts, and should not be set again afterwards.
The room which is currently active. (Read-only)
A sge.dsp.Object object which represents the mouse cursor. Its bounding box is a one-pixel square. It is automatically added to every room’s default list of objects.
Some of this object’s attributes control properties of the mouse. See the documentation for sge.mouse for more information.
(Read-only)
Arguments set the respective initial attributes of the game. See the documentation for sge.dsp.Game for more information.
The created sge.dsp.Game object is automatically assigned to sge.game.
Start the game. Should only be called once; the effect of any further calls is undefined.
Pause the game.
Arguments:
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.
Cause the SGE to recieve input from the OS.
This method needs to be called periodically for the SGE to recieve events from the OS, such as key presses and mouse movement, as well as to assure the OS that the program is not locked up.
Upon calling this, each event is translated into the appropriate class in sge.input and the resulting object is appended to input_events.
You normally don’t need to use this function directly. It is called automatically in each frame of the SGE’s main loop. You only need to use this function directly if you take control away from the SGE’s main loop, e.g. to create your own loop.
Regulate the SGE’s running speed and return the time passed.
Arguments:
When this method is called, the program will sleep long enough so that the game runs at fps frames per second, then return the number of milliseconds that passed between the previous call and the current call of this method.
You normally don’t need to use this function directly. It is called automatically in each frame of the SGE’s main loop. You only need to use this function directly if you want to create your own loop.
Refresh the screen.
This method needs to be called for changes to the screen to be seen by the user. It should be called every frame.
You normally don’t need to use this function directly. It is called automatically in each frame of the SGE’s main loop. You only need to use this function directly if you take control away from the SGE’s main loop, e.g. to create your own loop.
Project a single-pixel dot onto the game window.
Arguments:
Window projections are projections made directly onto the game window, independent of the room or any views.
Note
The Z-axis position of a window projection does not correlate with the Z-axis position of anything positioned within the room, such as room projections and sge.dsp.Object objects. Window projections are always positioned in front of such things.
See the documentation for sge.gfx.Sprite.draw_dot() for more information.
Project a line segment onto the game window.
Arguments:
See the documentation for sge.gfx.Sprite.draw_line() and sge.dsp.Game.project_dot() for more information.
Project a rectangle onto the game window.
Arguments:
See the documentation for sge.gfx.Sprite.draw_rectangle() and sge.dsp.Game.project_dot() for more information.
Project an ellipse onto the game window.
Arguments:
See the documentation for sge.gfx.Sprite.draw_ellipse() and sge.dsp.Game.project_dot() for more information.
Project a circle onto the game window.
Arguments:
See the documentation for sge.gfx.Sprite.draw_circle() and sge.dsp.Game.project_dot() for more information.
Draw a polygon on the sprite.
Arguments:
See the documentation for sge.gfx.Sprite.draw_polygon() and sge.dsp.Game.project_dot() for more information.
Project a sprite onto the game window.
Arguments:
See the documentation for sge.gfx.Sprite.draw_sprite() and sge.dsp.Game.project_dot() for more information.
Project text onto the game window.
Arguments:
See the documentation for sge.gfx.Sprite.draw_text() and sge.dsp.Game.project_dot() for more information.
Called once each frame.
Arguments:
Called when the value of an alarm reaches 0.
See the documentation for sge.dsp.Game.alarms for more information.
See the documentation for sge.input.KeyPress for more information.
See the documentation for sge.input.KeyRelease for more information.
See the documentation for sge.input.MouseMove for more information.
See the documentation for sge.input.MouseButtonPress for more information.
See the documentation for sge.input.MouseButtonRelease for more information.
See the documentation for sge.input.JoystickAxisMove for more information.
See the documentation for sge.input.JoystickHatMove for more information.
See the documentation for sge.input.JoystickTrackballMove for more information.
See the documentation for sge.input.JoystickButtonPress for more information.
See the documentation for sge.input.JoystickButtonRelease for more information.
See the documentation for sge.input.KeyboardFocusGain for more information.
See the documentation for sge.input.KeyboardFocusLose for more information.
See the documentation for sge.input.MouseFocusGain for more information.
See the documentation for sge.input.MouseFocusLose for more information.
See the documentation for sge.input.QuitRequest for more information.
This is always called after any sge.dsp.Room.event_close() occurring at the same time.
Proxy for sge.game.mouse.event_collision(). See the documentation for sge.dsp.Object.event_collision() for more information.
See the documentation for sge.dsp.Game.event_step() for more information.
See the documentation for sge.input.KeyPress for more information.
See the documentation for sge.input.KeyRelease for more information.
See the documentation for sge.input.MouseMove for more information.
See the documentation for sge.input.MouseButtonPress for more information.
See the documentation for sge.input.MouseButtonRelease for more information.
See the documentation for sge.input.JoystickAxisMove for more information.
See the documentation for sge.input.JoystickHatMove for more information.
See the documentation for sge.input.JoystickTrackballMove for more information.
See the documentation for sge.input.JoystickButtonPress for more information.
See the documentation for sge.input.JoystickButtonRelease for more information.
See the documentation for sge.input.KeyboardFocusGain for more information.
See the documentation for sge.input.KeyboardFocusLose for more information.
See the documentation for sge.input.MouseFocusGain for more information.
See the documentation for sge.input.MouseFocusLose for more information.
See the documentation for sge.dsp.Game.event_close() for more information.
This class stores the settings and objects found in a room. Rooms are used to create separate parts of the game, such as levels and menu screens.
The width of the room in pixels. If set to None, sge.game.width is used.
The height of the room in pixels. If set to None, sge.game.height is used.
A list containing all sge.dsp.View objects in the room.
The sge.gfx.Background object used.
The horizontal position of the background in the room.
The vertical position of the background in the room.
The width of this room’s object areas in pixels. If set to None, sge.game.width is used. For optimum performance, this should generally be about the average width of objects in the room which check for collisions.
The height of this room’s object areas in pixels. If set to None, sge.game.height is used. For optimum performance, this should generally be about the average height of objects in the room which check for collisions.
A dictionary containing the alarms of the room. Each value decreases by 1 each frame (adjusted for delta timing if it is enabled). When a value is at or below 0, event_alarm() is executed with alarm_id set to the respective key, and the item is deleted from this dictionary.
A list containing all sge.dsp.Object objects in the room. (Read-only)
A 2-dimensional list of object areas, indexed in the following way:
object_areas[x][y]
Where x is the horizontal location of the left edge of the area in the room divided by object_area_width, and y is the vertical location of the top edge of the area in the room divided by object_area_height.
For example, if object_area_width is 32 and object_area_height is 48, then object_areas[2][4] indicates the object area with an x location of 64 and a y location of 192.
Each object area is a set containing sge.dsp.Object objects whose sprites or bounding boxes reside within the object area.
Object areas are only created within the room, i.e. the horizontal location of an object area will always be less than width, and the vertical location of an object area will always be less than height. Depending on the size of collision areas and the size of the room, however, the last row and/or the last column of collision areas may partially reside outside of the room.
Note
It is generally easier to use get_objects_at() than to access this list directly.
A set containing sge.dsp.Object objects whose sprites or bounding boxes reside within any area not covered by the room’s object area.
Note
Depending on the size of object areas and the size of the room, the “void” area may not include the entirety of the outside of the room. There may be some space to the right of and/or below the room which is covered by collision areas.
Reserved dictionary for internal use by the SGE. (Read-only)
Arguments:
All other arguments set the respective initial attributes of the room. See the documentation for sge.dsp.Room for more information.
Add an object to the room.
Arguments:
Remove an object from the room.
Arguments:
Start the room.
Arguments:
transition – The type of transition to use. Should be one of the following:
If an unsupported value is given, default to None.
transition_time – The time the transition should take in milliseconds. Has no effect if transition is None.
transition_arg – An arbitrary argument that can be used by the following transitions:
Return a set of objects near a particular area.
Arguments:
Note
This function does not ensure that objects returned are actually within the given area. It simply combines all object areas that need to be checked into a single set. To ensure that an object is actually within the area, you must check the object manually, or use sge.collision.rectangle() instead.
Project a single-pixel dot onto the room.
Arguments:
See the documentation for sge.gfx.Sprite.draw_dot() for more information.
Project a line segment onto the room.
Arguments:
See the documentation for sge.gfx.Sprite.draw_line() for more information.
Project a rectangle onto the room.
Arguments:
See the documentation for sge.gfx.Sprite.draw_rectangle() for more information.
Project an ellipse onto the room.
Arguments:
See the documentation for sge.gfx.Sprite.draw_ellipse() for more information.
Project a circle onto the room.
Arguments:
See the documentation for sge.gfx.Sprite.draw_circle() for more information.
Draw a polygon on the sprite.
Arguments:
See the documentation for sge.gfx.Sprite.draw_polygon() for more information.
Project a sprite onto the room.
Arguments:
See the documentation for sge.gfx.Sprite.draw_sprite() for more information.
Project text onto the room.
Arguments:
See the documentation for sge.gfx.Sprite.draw_text() for more information.
Called when the room is started for the first time. It is always called after any sge.dsp.Game.event_game_start() and before any sge.dsp.Object.event_create occurring at the same time.
Called when the room is started after it has already previously been started. It is always called before any sge.dsp.Object.event_create() occurring at the same time.
Called when another room is started or the game ends while this room is the current room. It is always called before any sge.dsp.Game.event_game_end() occurring at the same time.
See the documentation for sge.dsp.Game.event_step() for more information.
See the documentation for sge.dsp.Room.alarms for more information.
See the documentation for sge.input.KeyPress for more information.
See the documentation for sge.input.KeyRelease for more information.
See the documentation for sge.input.MouseMove for more information.
Mouse button press event.
See the documentation for sge.input.MouseButtonPress for more information.
See the documentation for sge.input.MouseButtonRelease for more information.
See the documentation for sge.input.JoystickAxisMove for more information.
See the documentation for sge.input.JoystickHatMove for more information.
See the documentation for sge.input.JoystickTrackballMove for more information.
See the documentation for sge.input.JoystickButtonPress for more information.
See the documentation for sge.input.JoystickButtonRelease for more information.
See the documentation for sge.input.KeyboardFocusGain for more information.
See the documentation for sge.input.KeyboardFocusLose for more information.
See the documentation for sge.input.MouseFocusGain for more information.
See the documentation for sge.input.MouseFocusLose for more information.
This is always called before any sge.dsp.Game.event_close() occurring at the same time.
See the documentation for sge.input.QuitRequest for more information.
See the documentation for sge.dsp.Game.event_step() for more information.
See the documentation for sge.input.KeyPress for more information.
See the documentation for sge.input.KeyRelease for more information.
See the documentation for sge.input.MouseMove for more information.
See the documentation for sge.input.MouseButtonPress for more information.
See the documentation for sge.input.MouseButtonRelease for more information.
See the documentation for sge.input.JoystickAxisMove for more information.
See the documentation for sge.input.JoystickHatMove for more information.
See the documentation for sge.input.JoystickTrackballMove for more information.
See the documentation for sge.input.JoystickButtonPress for more information.
See the documentation for sge.input.JoystickButtonRelease for more information.
See the documentation for sge.input.KeyboardFocusGain for more information.
See the documentation for sge.input.KeyboardFocusLose for more information.
See the documentation for sge.input.MouseFocusGain for more information.
See the documentation for sge.input.MouseFocusLose for more information.
See the documentation for sge.dsp.Room.event_close() for more information.
This class controls what the player sees in a room at any given time. Multiple views can exist in a room, and this can be used to create a split-screen effect.
The horizontal position of the view in the room. When set, if it brings the view outside of the room it is in, it will be re-adjusted so that the view is completely inside the room.
The vertical position of the view in the room. When set, if it brings the view outside of the room it is in, it will be re-adjusted so that the view is completely inside the room.
The horizontal position of the view port on the window.
The vertical position of the view port on the window.
The width of the view. When set, if it results in the view being outside of the room it is in, x will be adjusted so that the view is completely inside the room.
The height of the view. When set, if it results in the view being outside the room it is in, y will be adjusted so that the view is completely inside the room.
The width of the view port. Set to None to make it the same as width. If this value differs from width, the image will be horizontally scaled so that it fills the port.
The height of the view port. Set to None to make it the same as height. If this value differs from height, the image will be vertically scaled so that it fills the port.
Reserved dictionary for internal use by the SGE. (Read-only)
Arguments:
All other arugments set the respective initial attributes of the view. See the documentation for sge.dsp.View for more information.
This class is used for game objects, such as the player, enemies, bullets, and the HUD. Generally, each type of object has its own subclass of sge.dsp.Object.
The Z-axis position of the object in the room.
The sprite currently in use by this object. Can be either a sge.gfx.Sprite object or a sge.gfx.TileGrid object. Set to None for no sprite.
Whether or not the object’s sprite should be projected onto the screen.
Indicates whether the object is active (True) or inactive (False). While the object is active, it will exhibit normal behavior; events will be executed normally as will any other automatic functionality, such as adding xvelocity and yvelocity to x and y. If active is False, automatic functionality and normal events will be disabled.
Note
Inactive sge.dsp.Object objects are still visible by default and continue to be involved in collisions. In addition, collision events and destroy events still occur even if the object is inactive. If you wish for the object to not be visible, set visible to False. If you wish for the object to not perform collision events, set tangible to False.
Whether or not the object should check for collisions automatically and cause collision events. If an object is not using collision events, setting this to False will give a boost in performance.
Note
This will not prevent automatic collision detection by other objects from detecting this object, and it will also not prevent this object’s collision events from being executed. If you wish to disable collision detection entirely, set tangible to False.
Whether or not collisions involving the object can be detected. Setting this to False can improve performance if the object doesn’t need to be involved in collisions.
Depending on the game, a useful strategy to boost performance can be to exclude an object from collision detection while it is outside the view. If you do this, you likely want to set active to False as well so that the object doesn’t move in undesireable ways (e.g. through walls).
Note
If this is False, checks_collisions is implied to be False as well regardless of its actual value. This is because checking for collisions which can’t be detected is meaningless.
The horizontal location of the bounding box relative to the object’s position. If set to None, the value recommended by the sprite is used.
The vertical location of the bounding box relative to the object’s position. If set to None, the value recommended by the sprite is used.
The width of the bounding box in pixels. If set to None, the value recommended by the sprite is used.
The height of the bounding box in pixels. If set to None, the value recommended by the sprite is used.
If set to True, the origin is automatically adjusted to be the location of the pixel recommended by the sprite after transformation. This will cause rotation to be about the origin rather than being about the center of the image.
Note
The value of this attribute has no effect on the bounding box. If you wish for the bounding box to be adjusted as well, you must do so manually. As an alternative, you may want to consider using precise collision detection instead.
Whether or not an ellipse (rather than a rectangle) should be used for collision detection.
Whether or not precise (pixel-perfect) collision detection should be used. Note that this can be inefficient and does not work well with animated sprites.
The position of the left side of the bounding box in the room (same as x + bbox_x).
The position of the right side of the bounding box in the room (same as bbox_left + bbox_width).
The position of the top side of the bounding box in the room (same as y + bbox_y).
The position of the bottom side of the bounding box in the room (same as bbox_top + bbox_height).
The direction of the object’s movement in degrees, with 0 being directly to the right and rotation in a positive direction being clockwise.
The acceleration of the object to the right in pixels per frame. If non-zero, movement as a result of xvelocity will be adjusted based on the kinematic equation, v[f]^2 = v[i]^2 + 2*a*d.
The acceleration of the object downward in pixels per frame. If non-zero, movement as a result of yvelocity will be adjusted based on the kinematic equation, v[f]^2 = v[i]^2 + 2*a*d.
Like xacceleration, but its sign is ignored and it always causes the absolute value of xvelocity to decrease.
Like yacceleration, but its sign is ignored and it always causes the absolute value of yvelocity to decrease.
The animation frame currently being displayed, with 0 being the first one.
The horizontal location of the origin relative to the left edge of the images. If set to None, the value recommended by the sprite is used.
The vertical location of the origin relative to the top edge of the images. If set to None, the value recommended by the sprite is used.
The animation rate in frames per second. Can be negative, in which case animation will be reversed. If set to None, the value recommended by the sprite is used.
The animation rate as a factor of sge.game.fps. Can be negative, in which case animation will be reversed. If set to None, the value recommended by the sprite is used.
The horizontal scale factor for the sprite. If this is negative, the sprite will also be mirrored horizontally.
The vertical scale factor for the sprite. If this is negative, the sprite will also be flipped vertically.
The rotation of the sprite in degrees, with rotation in a positive direction being clockwise.
If regulate_origin is True, the image is rotated about the origin. Otherwise, the image is rotated about its center.
The alpha value applied to the entire image, where 255 is the original image, 128 is half the opacity of the original image, 0 is fully transparent, etc.
A sge.gfx.Color object representing the color to blend with the sprite (using RGBA Multiply blending). Set to None for no color blending.
A dictionary containing the alarms of the object. Each value decreases by 1 each frame (adjusted for delta timing if it is enabled). When a value is at or below 0, event_alarm() is executed with alarm_id set to the respective key, and the item is deleted from this dictionary.
The current mask used for non-rectangular collision detection. See the documentation for sge.collision.masks_collide() for more information. (Read-only)
Reserved dictionary for internal use by the SGE. (Read-only)
Arugments set the respective initial attributes of the object. See the documentation for sge.dsp.Object for more information.
Move the object horizontally. This method can be overridden to conveniently define a particular way movement should be handled. Currently, it is used in the default implementation of event_update_position().
Arguments:
The default behavior of this method is the following code:
self.x += move
Move the object vertically. This method can be overridden to conveniently define a particular way movement should be handled. Currently, it is used in the default implementation of event_update_position().
Arguments:
The default behavior of this method is the following code:
self.y += move
Return a list of objects colliding with this object.
Arguments:
Remove the object from the current room. foo.destroy() is identical to sge.game.current_room.remove(foo).
Create an object of this class and add it to the current room.
args and kwargs are passed to the constructor method of cls as arguments. Calling obj = cls.create(*args, **kwargs) is the same as:
obj = cls(*args, **kwargs)
sge.game.current_room.add(obj)
Called in the following cases:
Called right after the object is removed from the current room.
Note
If the object is removed from a room while it is not the current room, this method will not be called.
Called each frame after automatic updates to objects (such as the effects of the speed variables), but before collision events.
See the documentation for sge.dsp.Game.event_step() for more information.
See the documentation for sge.dsp.Object.alarms for more information.
See the documentation for sge.input.KeyPress for more information.
See the documentation for sge.input.KeyRelease for more information.
See the documentation for sge.input.MouseMove for more information.
See the documentation for sge.input.MouseButtonPress for more information.
See the documentation for sge.input.MouseButtonRelease for more information.
See the documentation for sge.input.JoystickAxisMove for more information.
See the documentation for sge.input.JoystickHatMove for more information.
See the documentation for sge.input.JoystickTrackballMove for more information.
See the documentation for sge.input.JoystickButtonPress for more information.
See the documentation for sge.input.JoystickButtonRelease for more information.
Called when it’s time to update the position of the object. This method handles this functionality, so defining this will override the default behavior and allow you to handle the speed variables in a special way.
The default behavior of this method is the following code:
if delta_mult:
vi = self.xvelocity
vf = vi + self.xacceleration * delta_mult
dc = abs(self.xdeceleration) * delta_mult
if abs(vf) > dc:
vf -= math.copysign(dc, vf)
else:
vf = 0
self.xvelocity = vf
self.move_x(((vi + vf) / 2) * delta_mult)
vi = self.yvelocity
vf = vi + self.yacceleration * delta_mult
dc = abs(self.ydeceleration) * delta_mult
if abs(vf) > dc:
vf -= math.copysign(dc, vf)
else:
vf = 0
self.yvelocity = vf
self.move_y(((vi + vf) / 2) * delta_mult)
See the documentation for sge.dsp.Game.event_step() for more information.
Called when this object collides with another object.
Arguments:
Directionless “collisions” (ones with both an xdirection and ydirection of 0) are possible. These are typically collisions which were already occurring in the previous frame (continuous collisions).
See the documentation for sge.dsp.Game.event_step() for more information.
See the documentation for sge.input.KeyPress for more information.
See the documentation for sge.input.KeyRelease for more information.
See the documentation for sge.input.MouseMove for more information.
See the documentation for sge.input.MouseButtonPress for more information.
See the documentation for sge.input.MouseButtonRelease for more information.
See the documentation for sge.input.JoystickAxisMove for more information.
See the documentation for sge.input.JoystickHatMove for more information.
See the documentation for sge.input.JoystickTrackballMove for more information.
See the documentation for sge.input.JoystickButtonPress for more information.
See the documentation for sge.input.JoystickButtonRelease for more information.