Package pype :: Module sprite :: Class ScaledSprite
[frames] | no frames]

Class ScaledSprite

source code

object --+
         |
        ScaledSprite

Instance Methods
 
__init__(self, width=None, height=None, rwidth=None, rheight=None, x=0, y=0, depth=0, fname=None, name=None, fb=None, on=1, image=None, icolor='black', ifill='yellow', centerorigin=False, rotation=0.0, contrast=1.0)
ScaledSprite Object -- pype's main tool for graphics generation.
source code
 
refresh(self) source code
 
__del__(self)
Sprite clean up.
source code
 
__repr__(self)
repr(x)
source code
 
XY(self, xy)
Respects centerorigin flag.
source code
 
X(self, x)
Respects centerorigin flag.
source code
 
Y(self, y)
Respects centerorigin flag.
source code
 
set_rotation(self, deg)
Sets GL-pipeline rotation term - fast rotation on video card.
source code
 
set_contrast(self, c)
Sets GL-pipeline contrast term - uses video hardware for alpha.
source code
 
asPhotoImage(self, alpha=None, xscale=None, yscale=None)
Convert sprite to a Tkinter displayable PhotoImage.
source code
 
asImage(self, xscale=None, yscale=None)
Convert sprite to a PIL Image.
source code
 
set_alpha(self, a)
Set global alpha value.
source code
 
line(self, x1, y1, x2, y2, color, width=1)
Draw line of specified color in sprite.
source code
 
fill(self, color)
Fill sprite with specficied color.
source code
 
clear(self, color=(1,1,1))
Clear sprite to specified color.
source code
 
noise(self, thresh=0.5)
Fill sprite with white noise.
source code
 
circlefill(self, color, r=None, x=0, y=0, width=0)
Draw filled circle in sprite.
source code
 
circle(self, color, r=None, x=0, y=0, mask=True)
Draw filled circle into sprite.
source code
 
rect(self, x, y, w, h, color, mask=False)
Draw a filled rectangle of the specifed color on a sprite.
source code
 
rotateCCW(self, angle, preserve_size=1)
Counter clockwise (CCW) rotation.
source code
 
rotateCW(self, angle, preserve_size=1)
Clockwise (CW) rotation.
source code
 
rotate(self, angle, preserve_size=True)
Lossy rotation of spite image data.
source code
 
scale(self, new_width, new_height)
Fast resizing of sprite using pygame.rotozoom.
source code
 
hard_aperture(self, r, x=0, y=0)
Apply hard-edged circular vignette/mask in place.
source code
 
alpha_aperture(self, r, x=0, y=0)
Apply hard-edged circular vignette/mask in place using alpha channel
source code
 
alpha_gradient(self, r1, r2, x=0, y=0)
Add soft-edged apperature (gradient vignette)
source code
 
alpha_gradient2(self, r1, r2, bg, x=0, y=0)
Add soft-edged apperature (gradient vignette)
source code
 
dim(self, mult, meanval=128.0)
Reduce sprite contrast
source code
 
thresh(self, threshval) source code
 
threshold(self, threshval)
Threshold sprite image data
source code
 
on(self)
Make sprite visible.
source code
 
off(self)
Make sprite invisible.
source code
 
toggle(self)
Flip on/off flag.
source code
 
state(self)
Get current on/off flag state.
source code
 
moveto(self, x, y)
Move sprite to new absolute location.
source code
 
rmove(self, dx=0, dy=0)
Relative move.
source code
 
blit(self, x=None, y=None, fb=None, flip=None, force=0, textureonly=None)
Copy sprite to screen (block transfer).
source code
 
fastblit(self) source code
 
render(self, clear=False)
Render image data into GL texture memory for speed.
source code
 
subimage(self, x, y, w, h, center=None)
Extract sub-region of sprite into new sprite.
source code
 
clone(self)
Duplicate sprite (aka deepcopy).
source code
 
setdir(self, angle, vel) source code
 
displayim(self) source code
 
save(self, fname, mode='w')
Write sprite as image to file
source code
 
save_ppm(self, fname, mode='w') source code
 
save_alpha_pgm(self, fname, mode='w') source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, width=None, height=None, rwidth=None, rheight=None, x=0, y=0, depth=0, fname=None, name=None, fb=None, on=1, image=None, icolor='black', ifill='yellow', centerorigin=False, rotation=0.0, contrast=1.0)
(Constructor)

source code 

ScaledSprite Object -- pype's main tool for graphics generation.

This is a basic sprite object. The improvement here is that the sprite has both a real size (rwidth x rheight) and a display size (dwidth x dheight). If these match, it's a standard Sprite object. If they don't match, the coordinate system for sprite makes it look like it's display size, so gratings etc are computed properly. But only rwidth x rheight pixels are kepts around, so you can use coarse sprites plus OpenGL scaling to get more speed.

The standard Sprite class is now a subclass of ScaledSprite with rwidth==dwidth and rheight==dheight..

Parameters:
  • width, height - (int) sprite size on display in pixels -- this is the physical size once the sprite gets drawn on the display
  • rwidth, rheight - (int/float) virtual sprite size -- this is the one that actual computations get done on, so smaller is faster! Ff these are <1.0, then they're assumed to be x and y-scaling factors and applied to width and height
  • x, y - (int) sprite position in pixels
  • depth - (int; default 0) depth order for DisplayList (0=top)
  • fb - (FrameBuffer object) associated FrameBuffer object
  • on - (boolean) on/off (for DisplayList)
  • image - (Sprite object) seed sprite to get data from
  • fname - (string) Filename of image to load sprite data from.
  • name - (string) Sprite name/info
  • icolor - (string) icon outline (for UserDisplay)
  • ifill - (string) icon fille (for UserDisplay)
  • centerorigin - (boolean; default=0) This defines the internal coordinate system used for the sprite (ie, if you're doing math on the sprite axes with numpy). If False (default), then the upper left corner of the sprite is (0,0) and increasing y-values go DOWN, increasing x-values go RIGHT. If True, then (0,0) corresponds to sprite's center increasing y-values go UP and increasing y-values go RIGHT.
  • rotation - (float degrees) GL-pipeline rotation factor. Very fast pre-blit rotation.
  • contrast - (float 0-1.0) GL-pipeline contrast scaling factor. Very fast pre-blit scaling of all pixel values.
Overrides: object.__init__

__del__(self)
(Destructor)

source code 

Sprite clean up.

Remove the name of the sprite from the global list of sprites to facilitate detection of un-garbage collected sprites

__repr__(self)
(Representation operator)

source code 
repr(x)
Overrides: object.__repr__
(inherited documentation)

set_contrast(self, c)

source code 
Sets GL-pipeline contrast term - uses video hardware for alpha.
Parameters:
  • c - contrast value - [0-1] or [0-100]

asPhotoImage(self, alpha=None, xscale=None, yscale=None)

source code 

Convert sprite to a Tkinter displayable PhotoImage.

This depends on the python imaging library (PIL)

Parameters:
  • alpha - (optional) alpha value for the PhotoImage
  • xscale, yscale - (optional) x and y scale factor for resizing image. If you specify one, you must specify both!
Returns:
PhotoImage represenation of the sprite's pixels.

asImage(self, xscale=None, yscale=None)

source code 

Convert sprite to a PIL Image.

This depends on the python imaging library (PIL)

Parameters:
  • xscale, yscale - (optional) x and y scale factor for resizing image. If you specify one, you must specify both!
Returns:
Image represenation of the sprite's pixels.

set_alpha(self, a)

source code 

Set global alpha value.

Set transparency of the entire sprite to this value.

Parameters:
  • a - alpha value (0-255; 0 is fully transparent, 255 is fully opaque).
Returns:
nothing

line(self, x1, y1, x2, y2, color, width=1)

source code 

Draw line of specified color in sprite.

NB not the same syntax as the FrameBuffer.line() method!!

Parameters:
  • x1, y1 - starting coords of line in world coords
  • x2, y2 - ending coords of line in world coords
  • color - line color in C() parseable format
  • width - (pixels) line width
Returns:
nothing

fill(self, color)

source code 

Fill sprite with specficied color.

Like clear method above, but requires color to be specified

Parameters:
  • color - C() parseable color specification
Returns:
nothing

clear(self, color=(1,1,1))

source code 

Clear sprite to specified color.

Set's all pixels in the sprite to the indicated color, or black if no color is specified.

Parameters:
  • color - C() parseable color specification
Returns:
nothing

noise(self, thresh=0.5)

source code 

Fill sprite with white noise.

Fill sprite with binary white noise. Threshold can be used to specified the DC level of the noise.

Parameters:
  • thresh - threshold value [0-1] for binarizing the noise signal. Default's to 0.5 (half black/half white)
Returns:
nothing

circlefill(self, color, r=None, x=0, y=0, width=0)

source code 

Draw filled circle in sprite.

Only pixels inside the circle are affected.

Parameters:
  • color - C() parseable color spec
  • r - (pixels) circle radius
  • x, y - circle center position in world coords (defaults to 0,0)
  • width - (pixels) pen width; if 0, then draw a filled circle
Returns:
nothing

circle(self, color, r=None, x=0, y=0, mask=True)

source code 

Draw filled circle into sprite.

Pixels inside the specified circle are filled to specified color. If mask is True (default), the rest of the sprite is set to be 100% transparent (alpha=0).

Parameters:
  • color - C() parseable color spec
  • r - radius of circular mask
  • x, y - (pixels) center coords of circular mask (world coordinates)
  • mask - (bool; def=True) mask off rest of sprite?
Returns:
nothing

rect(self, x, y, w, h, color, mask=False)

source code 

Draw a filled rectangle of the specifed color on a sprite.

NB - parameter sequence is not the same order as circlefill()

Parameters:
  • x, y - world coords for rectangle's center
  • w, h - (pixels) width and height of rectangle
  • color - C() parseable color spec
  • mask - (bool; def=True) mask off rest of sprite?
Returns:
nothing

rotate(self, angle, preserve_size=True)

source code 

Lossy rotation of spite image data.

CW rotation of sprite about the center using pygame.transform primitives. This is a non-invertable deformation -- multiple rotations will accumulate errors, so if you're doing that, keep an original copy and rotate the copy each time.

Parameters:
  • angle - angle of rotation in degrees (CW)
  • preserve_size - (boolean; def True) if true, the rotated sprite is clipped back down to the size of the original image. Otherwise, it'll end up whatever size is necessary to preserve all the pixels.
Returns:
nothing - works on sprite in-place

scale(self, new_width, new_height)

source code 

Fast resizing of sprite using pygame.rotozoom.

Can scale up or down equally well. Changes the data within the sprite, so it's not really invertable. If you want to save the original image data, first clone() and then scale().

Warning: in general, this is obsolete -- you should use xscale/yscale to do scaling through the GL pipeline

Parameters:
  • new_width - (pixels) new width
  • new_height - (pixels) new height
Returns:
nothing

hard_aperture(self, r, x=0, y=0)

source code 

Apply hard-edged circular vignette/mask in place.

This works by setting pixels outside the specified region to (0,0,0,0). Formerly known as image_circmask and circmask.

alpha_aperture(self, r, x=0, y=0)

source code 
Apply hard-edged circular vignette/mask in place using alpha channel
Parameters:
  • r - (pixels) radius
  • x, y - (pixels) optional aperture center (0,0 is center)
Returns:
nothing

alpha_gradient(self, r1, r2, x=0, y=0)

source code 

Add soft-edged apperature (gradient vignette)

Vignette the sprite using a soft, linear, circular aperture. This draws a linearly ramped mask of the specified size into the alpha channel.

Parameters:
  • r1, r2 - (pixels) inner,outer radii of ramp
  • x, y - (pixels) optional aperture center (0,0 is center)
Returns:
nothing

alpha_gradient2(self, r1, r2, bg, x=0, y=0)

source code 

Add soft-edged apperature (gradient vignette)

Like alpha_gradient() above, but mask is applied directly to the image data by doing alpha blending in software, under the assumption that the background is a solid fill of value 'bg'.

Parameters:
  • r1, r2 - (pixels) inner,outer radii
  • bg - background color in C() compatible format
  • x, y - (pixels) coords of mask center in world coords
Returns:
nothing

dim(self, mult, meanval=128.0)

source code 

Reduce sprite contrast

Reduce sprite's contrast. Modifies sprite's image data. v = (1-mult)*(v-mean), where v is the pixel values.

NB This assumes the mean value of the image data is 'meanval', which is not always the case. If it's not, then you need to compute and pass in the mean value explicitly.

Parameters:
  • mult - scale factor
  • meanval - assumed mean pixel value [0-255]
Returns:
nothing

threshold(self, threshval)

source code 

Threshold sprite image data

Threshold (binarize) sprite's image data v = (v > thresh) ? 255 : 1, where v is pixel value

Parameters:
  • threshval - threshold (0-255)
Returns:
nothing

toggle(self)

source code 
Flip on/off flag.
Returns:
current state of flag.

state(self)

source code 
Get current on/off flag state.
Returns:
boolean on/off flag

moveto(self, x, y)

source code 
Move sprite to new absolute location.
Parameters:
  • x, y - (pixels) framebuffer coords of new location
Returns:
nothing

rmove(self, dx=0, dy=0)

source code 

Relative move.

Shifts sprite by dx,dy pixels relative to current position.

Parameters:
  • dx, dy - (pixels) framebuffer coords of new location
Returns:
nothing

blit(self, x=None, y=None, fb=None, flip=None, force=0, textureonly=None)

source code 

Copy sprite to screen (block transfer).

  1. x,y,fb etc are all internal properties of each sprite. You don't need to supply them here unless you want to change them at the same time you blit
  2. Don't forget to flip the framebuffer after blitting!
Parameters:
  • x, y - (pixels) optional new screen location
  • fb - frame buffer
  • flip - (boolean) flip after blit?
  • force - (boolean) override on/off flag?

subimage(self, x, y, w, h, center=None)

source code 

Extract sub-region of sprite into new sprite.

Generates a new sprite containing image data from specified sub-region of this sprite. Image data is copied, not shared or referenced -- changes to result will not affet original.

Parameters:
  • x, y - (pixels) coordinates of subregion (0,0) is upper left corner of parent/src sprite
  • w, h - (pixels) width and height of subregion
  • center - (boolean) Does (x,y) refer to the center or upper left corner of sprite?
Returns:
new sprite

clone(self)

source code 

Duplicate sprite (aka deepcopy).

Create no sprite with all the same data, including image and alpha, duplicated. Like subimage, data are copied, not referenced, so changes to clone will not affect parent.

save(self, fname, mode='w')

source code 

Write sprite as image to file

Image is saved to file using pygame image tools. Filename extension determines the output format (png and jpg ok). If the sprite is scaled or anything like that, it will be saved as displayed, which may not match the in-memory image data.