Class Transform
The part of pygame.transform Gymnasium's envs use: Flip(Surface, bool, bool), which turns a y-up drawing into a
frame and mirrors sprites, and Scale(Surface, int, int)/Smoothscale(Surface, int, int), which size the toy_text tiles and
Pendulum's torque arrow.
public static class Transform
- Inheritance
-
Transform
- Inherited Members
Remarks
Scale(Surface, int, int) and Smoothscale(Surface, int, int) are Gym.NET's own implementations of pygame's behaviour (decision D4: no pygame code is used), derived from and proven against live pygame-ce 2.5.8 pixel for pixel, alpha included.
Reference: src_c/transform.c (pygame-ce 2.5.8).
Methods
Flip(Surface, bool, bool)
pygame.transform.flip(surface, flip_x, flip_y): a new surface, mirrored left-right and/or top-bottom. The
source is unchanged. Gymnasium's classic envs draw in a y-up world and flip vertically before returning a frame;
Taxi and Pendulum mirror their sprites. Per-pixel alpha is kept.
public static Surface Flip(Surface surface, bool flipX, bool flipY)
Parameters
surfaceSurfaceThe surface to mirror.
flipXboolMirror the columns (left becomes right).
flipYboolMirror the rows (top becomes bottom).
Returns
- Surface
The mirrored copy.
Exceptions
- ArgumentNullException
Thrown when
surfaceis null.
Scale(Surface, int, int)
pygame.transform.scale(surface, (width, height)): a new surface of the given size, each pixel a copy of
one source pixel (nearest neighbour, no blending), per-pixel alpha kept. Gymnasium's toy_text envs size their
tiles and sprites with it.
public static Surface Scale(Surface surface, int width, int height)
Parameters
surfaceSurfaceThe surface to scale.
widthintThe new width; 0 gives an empty surface. pygame truncates a float size (
int()), as callers must.heightintThe new height; 0 gives an empty surface.
Returns
- Surface
The scaled copy.
Remarks
Destination column x takes source column (x * step + step / 2) >> 16 with
step = (sourceWidth << 16) / width in integer arithmetic, and rows likewise: sampling at the middle of each
16.16 fixed-point step, which is what pygame-ce 2.5.8 picks (measured on 2,011 size pairs, shrinking and growing).
Exceptions
- ArgumentNullException
Thrown when
surfaceis null.- ValueError
Thrown for a negative size, with pygame's message (
Cannot scale to negative size).
Smoothscale(Surface, int, int)
pygame.transform.smoothscale(surface, (width, height)) for a shrinking size: a new surface whose pixels
average the source pixels they cover (area averaging), each channel of each pixel separately, per-pixel alpha
included. Pendulum shrinks its torque arrow with it every frame.
public static Surface Smoothscale(Surface surface, int width, int height)
Parameters
surfaceSurfaceThe surface to scale.
widthintThe new width, at most the source's; 0 gives an empty surface. pygame truncates a float size (
int()).heightintThe new height, at most the source's; 0 gives an empty surface.
Returns
- Surface
The scaled copy.
Remarks
The filter, as measured against pygame-ce 2.5.8 (exact on 800 random rows and 300 random images, shrinking one or
both axes): the horizontal pass runs first (every row narrowed), then the vertical one, each pass writing 8-bit
channels. Along an axis of n
pixels shrunk to d, a destination pixel spans span = 0x4000 * n / d source pixels in 2.14 fixed
point; a whole source pixel adds its value, the pixel straddling a boundary adds (v * w) >> 14 to each
side for its 14-bit weight w, and a finished pixel is (sum * (2^30 / span)) >> 16. The 14-bit
weights and the truncations are what make the result differ from an exact average (a constant 255 row shrinks
to 254), and they are kept on purpose.
Exceptions
- ArgumentNullException
Thrown when
surfaceis null.- ValueError
Thrown for a negative size, with pygame's message (
Cannot scale to negative size).- NotSupportedException
Thrown when either dimension grows: pygame expands with a different (interpolating) filter, which no env in this release needs, so it is deferred rather than approximated.