pygame2 – basic features used by pygame2

Base features for pygame2 that are shared shared and used throughout the whole pygame2 package.

Data Fields

pygame2.__version__
The version of the Pygame2 package as string.
pygame2.version_info
The version of the Pygame2 package as value tuple. The tuple is of the form ('major', 'minor', 'micro', 'releaselevel'), where all values except the releaselevel are integers.

BufferProxy

class pygame2.BufferProxy → BufferProxy

Creates a new, empty BufferProxy.

A buffer interface object that acts as a proxy on classes and attributes not featuring the buffer interface. A BufferProxy usually should be constructed from C code, not Python.

Attributes

BufferProxy.length
Gets the size of the buffer data in bytes.
BufferProxy.raw
Gets the raw buffer data as string. The string may contain NUL bytes.

Methods

BufferProxy.write(buffer, offset) → None

Writes raw data to the BufferProxy.

Writes the raw data from buffer to the BufferProxy object, starting at the specified offset within the BufferProxy. If the length of the passed buffer exceeds the length of the BufferProxy (reduced by offset), an IndexError will be raised.

Color

class pygame2.Color(...) → Color

Creates a new Color object.

The Color class represents RGBA color values using a value range of 0-255. It allows basic arithmetic operations to create new colors, supports conversions to other color spaces such as HSV or HSL and lets you adjust single color channels. The following constructors are supported:

  • Color (0xAARRGGBB)
  • Color (0xRRGGBB)
  • Color (r, g, b, a)
  • Color (r, g, b)
  • Color (“0xRRGGBBAA”)
  • Color (“#RRGGBBAA”)
  • Color (“name”)

Example:

from pygame2 import Color

red = Color (255, 0, 0)
green = Color (0x0000FF00)
blue = Color ("blue")

Attributes

Color.a
Gets or sets the alpha value of the Color.
Color.b
Gets or sets the blue value of the Color.
Color.cmy
The CMY representation of the Color. The CMY components are in the ranges C = [0, 1], M = [0, 1], Y = [0, 1]. Note that this will not return the absolutely exact CMY values for the set RGB values in all cases. Due to the RGB mapping from 0-255 and the CMY mapping from 0-1 rounding errors may cause the CMY values to differ slightly from what you might expect.
Color.g
Gets or sets the green value of the Color.
Color.hsla
The HSLA representation of the Color. The HSLA components are in the ranges H = [0, 360], S = [0, 100], L = [0, 100], A = [0, 100]. Note that this will not return the absolutely exact HSL values for the set RGB values in all cases. Due to the RGB mapping from 0-255 and the HSL mapping from 0-100 and 0-360 rounding errors may cause the HSL values to differ slightly from what you might expect.
Color.hsva
The HSVA representation of the Color. The HSVA components are in the ranges H = [0, 360], S = [0, 100], V = [0, 100], A = [0, 100]. Note that this will not return the absolutely exact HSV values for the set RGB values in all cases. Due to the RGB mapping from 0-255 and the HSV mapping from 0-100 and 0-360 rounding errors may cause the HSV values todiffer slightly from what you might expect.
Color.i1i2i3
The I1I2I3 representation of the Color. The I1I2I3 components are in the ranges I1 = [0, 1], I2 = [-0.5, 0.5], I3 = [-0.5, 0.5]. Note that this will not return the absolutely exact I1I2I3 values for the set RGB values in all cases. Due to the RGB mapping from 0-255 and the I1I2I3 from 0-1 rounding errors may cause the I1I2I3 values to differ slightly from what you might expect.
Color.r
Gets or sets the red value of the Color.

Methods

Color.correct_gamma(gamma) → Color

Applies a certain gamma value to the Color.

Applies a certain gamma value to the Color and returns a new Color with the adjusted RGBA values.

Color.normalize() -> (float, float, float, float)

Returns the normalized RGBA values of the Color.

Returns the normalized RGBA values of the Color as floating point values.

Error

class pygame2.Error(...) → Error
A specialized exception class that indicates a misbehavior within the pygame2 modules.

FRect

class pygame2.FRect(...) → FRect

A class for storing rectangular coordinates.

A FRect is used to store and manipulate rectangular coordinates.

Attributes

FRect.bottom
Gets or sets the bottom edge position of the FRect.
FRect.bottomleft
Gets or sets the bottom left corner position of the FRect.
FRect.bottomright
Gets or sets the bottom right corner position of the FRect.
FRect.center
Gets or sets the center position of the FRect.
FRect.centerx
Gets or sets the horizontal center position of the FRect.
FRect.centery
Gets or sets the vertical center position of the FRect.
FRect.h
Gets or sets the height of the FRect.
FRect.height
Gets or sets the height of the FRect.
FRect.left
Gets or sets the left edge position of the FRect.
FRect.midbottom
Gets or sets the mid bottom edge position of the FRect.
FRect.midleft
Gets or sets the mid left edge position of the FRect.
FRect.midright
Gets or sets the mid right edge position of the FRect.
FRect.midtop
Gets or sets the mid top edge position of the FRect.
FRect.right
Gets or sets the right position of the FRect.
FRect.size
Gets or sets the width and height of the FRect as 2-value tuple.
FRect.top
Gets or sets the top edge position of the FRect.
FRect.topleft
Gets or sets the top left corner position of the FRect.
FRect.topright
Gets or sets the top right corner position of the FRect.
FRect.w
Gets or sets the width of the FRect.
FRect.width
Gets or sets the width of the FRect.
FRect.x
Gets or sets the horizontal top left position of the FRect.
FRect.y
Gets or sets the vertical top left position of the FRect.

Methods

FRect.ceil() → :class:`Rect`

Creates a Rect from the specified FRect.

This creates a Rect using the smallest integral values greater or equal to the FRect floating point values.

FRect.clamp(frect) → FRect

Moves the rectangle inside another.

Returns a new rectangle that is moved to be completely inside the argument FRect. If the rectangle is too large to fit inside, it is centered inside the argument FRect, but its size is not changed.

FRect.clamp_ip(frect) → None

Moves the rectangle inside another, in place.

Same as clamp(), but operates in place.

FRect.clip(frect) → FRect

Crops a rectangle inside another.

Returns a new rectangle that is cropped to be completely inside the argument FRect. If the two rectangles do not overlap to begin with, a FRect with 0 size is returned. Thus it returns the area, in which both rects overlap.

FRect.collidedict(dict[, checkvals, key]) → key, value

Test if one rectangle in a dictionary intersects.

Returns the key and value of the first dictionary entry that collides with the FRect. If no collisions are found, None is returned. Depending on the checkvals argument either the keys or values of dict must be FRect objects. By default, the keys are checked.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the FRect itself and a key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
FRect.collidedictall(dict[, checkvals, key]) → [(key, value), ...]

Test if all rectangles in a dictionary intersect.

Returns a list of all the key and value pairs that intersect with the FRect. If no collisions are found an empty list is returned. Depending on the checkvals argument either the keys or values of the dict must be FRect objects. By default, the keys are checked.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the FRect itself and a key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
FRect.collidelist(rects[, key]) → int

Test if one rectangle in a list intersects.

Test whether the rectangle collides with any in a sequence of rectangles. The index of the first collision found is returned. If no collisions are found an index of -1 is returned.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the FRect itself and a key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
FRect.collidelistall(rects[, key]) → [int, ...]

Test if all rectangles in a list intersect.

Returns a list of all the indices that contain rectangles that collide with the FRect. If no intersecting rectangles are found, an empty list is returned.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the FRect itself and a key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
FRect.collidepoint(x, y) → bool
FRect.collidepoint(point) → bool

Test if a point is inside a rectangle.

Returns True if the given point is inside the rectangle. A point along the right or bottom edge is not considered to be inside the rectangle.

FRect.colliderect(frect) → bool

Test if two rectangles overlap.

Returns True if any portion of either rectangle overlap (except the top+bottom or left+right edges).

FRect.contains(frect) → bool

Test if one rectangle is inside another.

Returns True when the argument rectangle is completely inside the FRect.

FRect.copy() → FRect

Creates a copy of the FRect.

Returns a new FRect, that contains the same values as the caller.

FRect.fit(frect) → FRect

Resize and move a rectangle with aspect ratio.

Returns a new rectangle that is moved and resized to fit another. The aspect ratio of the original FRect is preserved, so the new rectangle may be smaller than the target in either width or height.

FRect.floor() → :class:`Rect`

Creates a Rect from the specified FRect.

This creates a Rect using the largest integral values less than or equal to the FRect floating point values.

FRect.inflate(x, y) → FRect
FRect.inflate(size) → FRect

Grow or shrink the rectangle size.

Returns a new rectangle with the size changed by the given offset. The rectangle remains centered around its current center. Negative values will shrink the rectangle.

FRect.inflate_ip(x, y) → None
FRect.inflate_ip(size) → None

Grow or shrink the rectangle size, in place.

Same as inflate(), but operates in place.

FRect.move(x, y) → FRect
FRect.move(point) → FRect

Moves the rectangle.

Returns a new rectangle that is moved by the given offset. The x and y arguments can be any float or integer value, positive or negative.

FRect.move_ip(x, y) → None
FRect.move_ip(point) → FRect

Moves the rectangle, in place.

Same as move(), but operates in place.

FRect.round() → :class`Rect`

Creates a Rect from the specified FRect.

This creates a Rect using the FRect floating point values rounded to the nearest integral value.

FRect.trunc() → :class:`Rect`

Creates a Rect from the specified FRect.

This creates a Rect using truncated integral values from the FRect floating point values.

FRect.union(frect) → FRect

Joins two rectangles into one.

Returns a new rectangle that completely covers the area of the two provided rectangles. There may be area inside the new FRect that is not covered by the originals.

FRect.union_ip(frect) → FRect

Joins two rectangles into one, in place.

Same as union(), but operates in place.

Rect

class pygame2.Rect(...) → Rect

A class for storing rectangular coordinates.

A Rect is used to store and manipulate rectangular coordinates.

Attributes

Rect.bottom
Gets or sets the bottom edge position of the Rect.
Rect.bottomleft
Gets or sets the bottom left corner position of the Rect.
Rect.bottomright
Gets or sets the bottom right corner position of the Rect.
Rect.center
Gets or sets the center position of the Rect.
Rect.centerx
Gets or sets the horizontal center position of the Rect.
Rect.centery
Gets or sets the vertical center position of the Rect.
Rect.h
Gets or sets the height of the Rect.
Rect.height
Gets or sets the height of the Rect.
Rect.left
Gets or sets the left edge position of the Rect.
Rect.midbottom
Gets or sets the mid bottom edge position of the Rect.
Rect.midleft
Gets or sets the mid left edge position of the Rect.
Rect.midright
Gets or sets the mid right edge position of the Rect.
Rect.midtop
Gets or sets the mid top edge position of the Rect.
Rect.right
Gets or sets the right position of the Rect.
Rect.size
Gets or sets the width and height of the Rect as 2-value tuple.
Rect.top
Gets or sets the top edge position of the Rect.
Rect.topleft
Gets or sets the top left corner position of the Rect.
Rect.topright
Gets or sets the top right corner position of the Rect.
Rect.w
Gets or sets the width of the Rect.
Rect.width
Gets or sets the width of the Rect.
Rect.x
Gets or sets the horizontal top left position of the Rect.
Rect.y
Gets or sets the vertical top left position of the Rect.

Methods

Rect.clamp(rect) → Rect

Moves the rectangle inside another.

Returns a new rectangle that is moved to be completely inside the argument Rect. If the rectangle is too large to fit inside, it is centered inside the argument Rect, but its size is not changed.

Rect.clamp_ip(rect) → None

Moves the rectangle inside another, in place.

Same as clamp(), but operates in place.

Rect.clip(rect) → Rect

Crops a rectangle inside another.

Returns a new rectangle that is cropped to be completely inside the argument Rect. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned. Thus it returns the area, in which both rects overlap.

Rect.collidedict(dict[, checkvals, key]) → key, value

Test if one rectangle in a dictionary intersects.

Returns the key and value of the first dictionary entry that collides with the Rect. If no collisions are found, None is returned. Depending on the checkvals argument either the keys or values of the dict must be Rect objects. By default, the keys are checked.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the Rect itself and a key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
Rect.collidedictall(dict[, checkvals, key]) → [(key, value), ...]

Test if all rectangles in a dictionary intersect.

Returns a list of all the key and value pairs that intersect with the Rect. If no collisions are found an empty list is returned. Depending on the checkvals argument either the keys or values of the dict must be Rect objects. By default, the keys are checked.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the Rect itself and an key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
Rect.collidelist(list[, key]) → int

Test if one rectangle in a list intersects.

Test whether the rectangle collides with any in a sequence of rectangles. The index of the first collision found is returned. If no collisions are found an index of -1 is returned.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the Rect itself and an key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
Rect.collidelistall(list[, key]) → [int, ...]

Test if all rectangles in a list intersect.

Returns a list of all the indices that contain rectangles that collide with the Rect. If no intersecting rectangles are found, an empty list is returned.

You can provide your own comparision function as key argument. The comparision function will take two arguments, the Rect itself and an key or value from the passed dictionary. It must return True or False

def cmpfunc (rect1, rect2):
    ...
    return True
Rect.collidepoint(x, y) → bool
Rect.collidepoint(point) → bool

Test if a point is inside a rectangle.

Returns True if the given point is inside the rectangle. A point along the right or bottom edge is not considered to be inside the rectangle.

Rect.colliderect(rect) → bool

Test if two rectangles overlap.

Returns True if any portion of either rectangle overlap (except the top+bottom or left+right edges).

Rect.contains(rect) → bool

Test if one rectangle is inside another.

Returns True when the argument rectangle is completely inside the Rect.

Rect.copy() → Rect

Creates a copy of the Rect.

Returns a new Rect, that contains the same values as the caller.

Rect.fit(rect) → Rect

Resize and move a rectangle with aspect ratio.

Returns a new rectangle that is moved and resized to fit another. The aspect ratio of the original Rect is preserved, so the new rectangle may be smaller than the target in either width or height.

Rect.inflate(x, y) → Rect
Rect.inflate(size) → Rect

Grow or shrink the rectangle size.

Returns a new rectangle with the size changed by the given offset. The rectangle remains centered around its current center. Negative values will shrink the rectangle.

Rect.inflate_ip(x, y) → None
Rect.inflate_ip(size) → Rect

Grow or shrink the rectangle size, in place.

Same as inflate(), but operates in place.

Rect.move(x, y) → Rect
Rect.move(point) → Rect

Moves the rectangle.

Returns a new rectangle that is moved by the given offset. The x and y arguments can be any integer value, positive or negative.

Rect.move_ip(x, y) → None
Rect.move_ip(point) → None

Moves the rectangle, in place.

Same as move(), but operates in place.

Rect.union(rect) → Rect

Joins two rectangles into one.

Returns a new rectangle that completely covers the area of the two provided rectangles. There may be area inside the new Rect that is not covered by the originals.

Rect.union_ip(rect) → Rect

Joins two rectangles into one, in place.

Same as union(), but operates in place.

Surface

class pygame2.Surface → Surface

An abstract base class for arbitrary Surface objects.

You should not instantiate this class directly.

Attributes

Surface.height
Gets the height of the Surface.
Surface.pixels
Gets a buffer with the pixels of the Surface.
Surface.size
Gets the width and height of the Surface.
Surface.width
Gets the width of the Surface.

Methods

Surface.blit(**kwds) → object

Performs a blit operation on the Surface.

The behavior, arguments and return value depend on the concrete Surface implementation.

Surface.copy() → Surface
Creates a copy of this Surface.

Font

class pygame2.Font → Font

An abstract base class for arbitrary Font objects.

You should not instantiate this class directly.

Attributes

Font.height
Gets the standard height of the Font typography.
Font.size
Gets the width and height of the Font typography.
Font.name
Gets the name of the loaded Font.
Font.style
Gets or sets the style used to render the Font.

Methods

Font.render(**kwds) → object
Renders the specified text using the Font object. The exact input arguments and return value is dependand on each specific Font implementation.
Font.copy() → Font
Creates a copy of this Font.