Contents
This module provides classes related to rendering graphics.
This class stores color information.
Objects of this class can be converted to iterables indicating the object’s red, green, blue, and alpha values, respectively; to integers which can be interpreted as a hexadecimal representation of the color, excluding alpha transparency; and to strings which indicate the English name of the color (in all lowercase) if possible, and hex_string otherwise.
The red component of the color as an integer, where 0 indicates no red intensity and 255 indicates full red intensity.
The green component of the color as an integer, where 0 indicates no green intensity and 255 indicates full green intensity.
The blue component of the color as an integer, where 0 indicates no blue intensity and 255 indicates full blue intensity.
This class stores images and information about how the SGE is to use those images.
What image formats are supported depends on the implementation of the SGE, but image formats that are generally a good choice are PNG and JPEG. See the implementation-specific information for a full list of supported formats.
The width of the sprite.
Note
Changing this attribute will cause the sprite to be scaled horizontally. This is a destructive transformation: it can result in loss of pixel information, especially if it is done repeatedly. Because of this, it is advised that you do not adjust this value for routine scaling. Use the image_xscale attribute of a sge.dsp.Object object instead.
The height of the sprite.
Note
Changing this attribute will cause the sprite to be scaled vertically. This is a destructive transformation: it can result in loss of pixel information, especially if it is done repeatedly. Because of this, it is advised that you do not adjust this value for routine scaling. Use the image_yscale attribute of a sge.dsp.Object object instead.
Whether or not the image should be partially transparent, based on the image’s alpha channel. If an image does not have an alpha channel, a colorkey will be used, with the transparent color being the color of the top-rightmost pixel of the respective frame.
If this is a sge.gfx.Color object, the indicated color will be used as a colorkey.
The suggested horizontal location of the origin relative to the left edge of the images.
The suggested vertical location of the origin relative to the top edge of the images.
The suggested rate in frames per second to animate the image at. Can be negative, in which case animation will be reversed.
The suggested rate to animate the image at as a factor of sge.game.fps. Can be negative, in which case animation will be reversed.
The horizontal location relative to the sprite of the suggested bounding box to use with it. If set to None, it will become equal to -origin_x (which is always the left edge of the image).
The vertical location relative to the sprite of the suggested bounding box to use with it. If set to None, it will become equal to -origin_y (which is always the top edge of the image).
The width of the suggested bounding box. If set to None, it will become equal to width - bbox_x (which is always everything on the image to the right of bbox_x).
The height of the suggested bounding box. If set to None, it will become equal to height - bbox_y (which is always everything on the image below bbox_y).
The name of the sprite given when it was created. (Read-only)
Reserved dictionary for internal use by the SGE. (Read-only)
Arguments:
name – The base name of the image files, used to find all individual image files that make up the sprite’s animation`. One of the following rules will be used to find the images:
If none of the above rules can be used, IOError is raised.
directory – The directory to search for image files in.
All other arguments set the respective initial attributes of the sprite. See the documentation for Sprite for more information.
Insert a new blank frame into the sprite.
Arguments:
Extend this sprite with the frames of another sprite.
If the size of the frames added is different from the size of this sprite, they are scaled to this sprite’s size.
Arguments:
Delete a frame from the sprite.
Arguments:
Return a sge.gfx.Color object indicating the color of a particular pixel on the sprite.
Arguments:
Return a two-dimensional list of :class`sge.gfx.Color` objects indicating the colors of a particular frame’s pixels.
A returned list given the name pixels is indexed as pixels[x][y], where x is the horizontal location of the pixel and y is the vertical location of the pixel.
Arguments:
Draw a single-pixel dot on the sprite.
Arguments:
x – The horizontal location relative to the sprite to draw the dot.
y – The vertical location relative to the sprite to draw the dot.
color – A sge.gfx.Color object representing the color of the dot.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Draw a line segment on the sprite.
Arguments:
x1 – The horizontal location relative to the sprite of the first end point of the line segment.
y1 – The vertical location relative to the sprite of the first end point of the line segment.
x2 – The horizontal location relative to the sprite of the second end point of the line segment.
y2 – The vertical location relative to the sprite of the second end point of the line segment.
color – A sge.gfx.Color object representing the color of the line segment.
thickness – The thickness of the line segment.
anti_alias – Whether or not anti-aliasing should be used.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Draw a rectangle on the sprite.
Arguments:
x – The horizontal location relative to the sprite to draw the rectangle.
y – The vertical location relative to the sprite to draw the rectangle.
width – The width of the rectangle.
height – The height of the rectangle.
fill – A sge.gfx.Color object representing the color of the fill of the rectangle.
outline – A sge.gfx.Color object representing the color of the outline of the rectangle.
outline_thickness – The thickness of the outline of the rectangle.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Draw an ellipse on the sprite.
Arguments:
x – The horizontal location relative to the sprite to position the imaginary rectangle containing the ellipse.
y – The vertical location relative to the sprite to position the imaginary rectangle containing the ellipse.
width – The width of the ellipse.
height – The height of the ellipse.
fill – A sge.gfx.Color object representing the color of the fill of the ellipse.
outline – A sge.gfx.Color object representing 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.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Draw a circle on the sprite.
Arguments:
x – The horizontal location relative to the sprite to position the center of the circle.
y – The vertical location relative to the sprite to position the center of the circle.
radius – The radius of the circle.
fill – A sge.gfx.Color object representing the color of the fill of the circle.
outline – A sge.gfx.Color object representing the color of the outline of the circle.
outline_thickness – The thickness of the outline of the circle.
anti_alias – Whether or not anti-aliasing should be used.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Draw a polygon on the sprite.
Arguments:
points – A list of points relative to the sprite to position each of the polygon’s angles. Each point should be a tuple in the form (x, y), where x is the horizontal location and y is the vertical location.
fill – A sge.gfx.Color object representing the color of the fill of the polygon.
outline – A sge.gfx.Color object representing the color of the outline of the polygon.
outline_thickness – The thickness of the outline of the polygon.
anti_alias – Whether or not anti-aliasing should be used.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Draw another sprite on the sprite.
Arguments:
sprite – The sge.gfx.Sprite or sge.gfx.TileGrid object to draw with.
image – The frame of sprite to draw with, where 0 is the first frame.
x – The horizontal location relative to self to position the origin of sprite.
y – The vertical location relative to self to position the origin of sprite.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Draw text on the sprite.
Arguments:
font – The font to use to draw the text.
text – The text (as a string) to draw.
x – The horizontal location relative to the sprite to draw the text.
y – The vertical location relative to the sprite to draw the text.
width – The width of the imaginary rectangle the text is drawn in; set to None to make the rectangle as wide as needed to contain the text without additional line breaks. If set to something other than None, a line which does not fit will be automatically split into multiple lines that do fit.
height – The height of the imaginary rectangle the text is drawn in; set to None to make the rectangle as tall as needed to contain the text.
color – A sge.gfx.Color object representing the color of the text.
halign – The horizontal alignment of the text and the horizontal location of the origin of the imaginary rectangle the text is drawn in. Can be set to one of the following:
valign – The vertical alignment of the text and the vertical location of the origin of the imaginary rectangle the text is drawn in. Can be set to one of the following:
anti_alias – Whether or not anti-aliasing should be used.
frame – The frame of the sprite to draw on, where 0 is the first frame; set to None to draw on all frames.
blend_mode – The blend mode to use. Possible blend modes are:
None is treated as sge.BLEND_NORMAL.
Erase part of the sprite.
Arguments:
Erase everything from the sprite.
Arguments:
Lock the sprite for continuous drawing.
Use this method to “lock” the sprite for being drawn on several times in a row. What exactly this does depends on the implementation and it may even do nothing at all, but calling this method before doing several draw actions on the sprite in a row gives the SGE a chance to make the drawing more efficient.
After you are done with continuous drawing, call draw_unlock() to let the SGE know that you are done drawing.
Warning
Do not cause a sprite to be used while it’s locked. For example, don’t leave it locked for the duration of a frame, and don’t draw it or project it on anything. The effect of using a locked sprite could be as minor as graphical errors and as severe as crashing the program, depending on the SGE implementation. Always call draw_unlock() immediately after you’re done drawing for a while.
Unlock the sprite.
Use this method to “unlock” the sprite after it has been “locked” for continuous drawing by draw_lock().
Mirror the sprite horizontally.
Arguments:
Flip the sprite vertically.
Arguments:
Resize the sprite by adding empty space instead of scaling.
After resizing the canvas:
Arguments:
Scale the sprite to a different size.
Unlike changing width and height, this function does not result in the actual size of the sprite changing. Instead, any scaled frames are repositioned so that the pixel which was at the origin before scaling remains at the origin.
Arguments:
Note
This is a destructive transformation: it can result in loss of pixel information, especially if it is done repeatedly. Because of this, it is advised that you do not adjust this value for routine scaling. Use the image_xscale and image_yscale attributes of a sge.dsp.Object object instead.
Note
Because this function does not alter the actual size of the sprite, scaling up may result in some parts of the image being cropped off.
Rotate the sprite about the center.
Arguments:
Note
This is a destructive transformation: it can result in loss of pixel information, especially if it is done repeatedly. Because of this, it is advised that you do not adjust this value for routine rotation. Use the image_rotation attribute of a sge.dsp.Object object instead.
Save the sprite to an image file.
Arguments:
If the sprite has multiple frames, the image file saved will be a horizontal reel of each of the frames from left to right with no space in between the frames.
Create a sprite based on tweening an existing sprite.
“Tweening” refers to creating an animation from gradual transformations to an image. For example, this can be used to generate an animation of an object growing to a particular size. The animation generated is called a “tween”.
Arguments:
sprite – The sprite to base the tween on. If the sprite includes multiple frames, all frames will be used in sequence until the end of the tween.
The tween’s origin is derived from this sprite’s origin, adjusted appropriately to accomodate any size changes made. Whether or not the tween is transparent is also determined by whether or not this sprite is transparent.
frames – The number of frames the to make the tween take up.
fps – The suggested rate of animation for the tween in frames per second. If set to None, the suggested animation rate of the base sprite is used.
xscale – The horizontal scale factor at the end of the tween. If set to None, horizontal scaling will not be included in the tweening process.
yscale – The vertical scale factor at the end of the tween. If set to None, vertical scaling will not be included in the tweening process.
rotation – The total clockwise rotation amount in degrees at the end of the tween. Can be negative to indicate counter-clockwise rotation instead. If set to None, rotation will not be included in the tweening process.
blend – A sge.gfx.Color object representing the color to blend with the sprite (using RGBA Multiply blending) at the end of the tween. If set to None, color blending will not be included in the tweening process.
All other arguments set the respective initial attributes of the tween. See the documentation for sge.gfx.Sprite for more information.
Create a sprite, draw the given text on it, and return the sprite. See the documentation for sge.gfx.Sprite.draw_text() for more information.
The sprite’s origin is set based on halign and valign.
Return a sprite based on the tiles in a tileset.
Arguments:
For all other arguments, see the documentation for Sprite.__init__().
Each tile in the tileset becomes a subimage of the returned sprite, ordered first from left to right and then from top to bottom.
Return the current display on the screen as a sprite.
Arguments:
If you only wish to save a screenshot (of the entire screen) to a file, the easiest way to do that is:
sge.gfx.Sprite.from_screenshot().save("foo.png")
This class stores a font for use by text drawing methods.
Note that bold and italic rendering could be ugly. It is better to choose a bold or italic font rather than enabling bold or italic rendering, if possible.
Whether or not bold rendering is enabled.
Note
Using this option can be ugly. It is better to choose a bold font rather than enabling bold rendering, if possible.
Whether or not italic rendering is enabled.
Note
Using this option can be ugly. It is better to choose an italic font rather than enabling italic rendering, if possible.
The name of the font as specified when it was created. (Read-only)
Reserved dictionary for internal use by the SGE. (Read-only)
Arguments:
name – The name of the font. Can be one of the following:
If none of the above methods return a valid font, the SGE will choose the font.
All other arguments set the respective initial attributes of the font. See the documentation for sge.gfx.Font for more information.
Note
It is generally not a good practice to rely on system fonts. There are no standard fonts; a font which you have on your system is probably not on everyone’s system. Rather than relying on system fonts, choose a font which is under a libre license (such as the GNU General Public License or the SIL Open Font License) and distribute it with your game; this will ensure that everyone sees text rendered the same way you do.
Return the width of a certain string of text when rendered.
See the documentation for sge.gfx.Sprite.draw_text() for more information.
Return the height of a certain string of text when rendered.
See the documentation for sge.gfx.Sprite.draw_text() for more information.
Return a font derived from a sprite.
Arguments:
All other arguments set the respective initial attributes of the font. See the documentation for sge.gfx.Font for more information.
The font’s name attribute will be set to the name of the sprite the font is derived from.
The font’s size attribute will indicate the height of the characters in pixels. The width of the characters will be adjusted proportionally.
This class stores a sprite and certain information for a layer of a background. In particular, it stores the location of the layer, whether the layer tiles horizontally, vertically, or both, and the rate at which it scrolls.
The sprite used for this layer. It will be animated normally if it contains multiple frames.
The horizontal location of the layer relative to the background.
The vertical location of the layer relative to the background.
The Z-axis position of the layer in the room.
The horizontal rate that the layer scrolls as a factor of the additive inverse of the horizontal movement of the view.
The vertical rate that the layer scrolls as a factor of the additive inverse of the vertical movement of the view.
Whether or not the layer should be repeated (tiled) to the left.
Whether or not the layer should be repeated (tiled) to the right.
Whether or not the layer should be repeated (tiled) upwards.
Whether or not the layer should be repeated (tiled) downwards.
Reserved dictionary for internal use by the SGE. (Read-only)
Arguments set the respective initial attributes of the layer. See the documentation for sge.gfx.BackgroundLayer for more information.
This class stores the layers that make up the background (which contain the information about what images to use and where) as well as the color to use where no image is shown.
A list containing all sge.gfx.BackgroundLayer objects used in this background. (Read-only)
A sge.gfx.Color object representing the color used in parts of the background where no layer is shown.
Reserved dictionary for internal use by the SGE. (Read-only)
Arguments set the respective initial attributes of the background. See the documentation for sge.gfx.Background for more information.