cocos.sprite module

Sprites allows to display a image in a rectangular area, which can be rotated, scaled and moved. The placement in the scene follows the standard CocosNode rules. Also, all stock actions will work with sprites.

Animating a sprite

Animation as in cartoon style animation, that is, replacing the image fast enough to give the illusion of movement, can be accomplished by:

  • using an animated .gif file as source for the image
  • passing a pyglet.image.Animation as image, which collects a number of images
  • have an array of images and let your code assign to the sprite image member

Changing a sprite by way of actions

To execute any action you need to create an action:

move = MoveBy( (50,0), 5 )

In this case, move is an action that will move the sprite 50 pixels to the right (x coordinate) and 0 pixel in the y coordinate in 5 seconds.

And now tell the sprite to execute it:

sprite.do( move )
class Sprite(image, position=(0, 0), rotation=0, scale=1, opacity=255, color=(255, 255, 255), anchor=None)

Bases: cocos.batch.BatchableNode, pyglet.sprite.Sprite

A CocosNode that displays a rectangular image.

Example:

sprite = Sprite('grossini.png')
supported_classes

alias of Sprite

contains(x, y)

Test whether this (untransformed) Sprite contains the pixel coordinates given.

draw()

When the sprite is not into a batch it will be draw with this method. If in a batch, this method is not called, and the draw is done by the batch.

get_AABB()

Returns a local-coordinates Axis aligned Bounding Box

Returns a cocos.rect.Rect instance.

get_rect()

Get a cocos.rect.Rect for this sprite.

Note that this rect’s position is most likely NOT the same as the Sprite’s position - in fact by default the rect’s center is the Sprite’s position. If you move the rect around and wish to reflect this change in the Sprite, you will probably have to do something like (again with the default image anchor in the center):

rect = sprite.get_rect()
rect.midbottom = (0, 100)
sprite.position = rect.center

Returns a cocos.rect.Rect instance.

color = None

color of the sprite in R,G,B format where 0,0,0 is black and 255,255,255 is white

height

Scaled height of the sprite.

Read-only. Invariant under rotation.

Type:int
image_anchor
image_anchor_x
image_anchor_y
opacity = None

opacity of the sprite where 0 is transparent and 255 is solid

position = None

position of the sprite in (x,y) coordinates

rotation = None

rotation degrees of the sprite. Default: 0 degrees

scale = None

scale of the sprite where 1.0 the default value

scale_x = None

additional horizontal-only scale of the sprite where 1.0 the default value

scale_y = None

additional vertical-only scale of the sprite where 1.0 the default value

width

Scaled width of the sprite.

Read-only. Invariant under rotation.

Type:int