cocos.sprite module¶
Sprites allows to display an 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 imageshave 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, **kwargs)¶ Bases:
cocos.batch.BatchableNode
,pyglet.sprite.Sprite
A CocosNode that displays a rectangular image.
Example:
sprite = Sprite('grossini.png')
- Parameters
image (str, pyglet.image.AbstractImage or pyglet.image.Animation) – name of the image resource, a pyglet image or a pyglet animation
position (tuple[float]) – position of the anchor. Defaults to (0,0)
rotation (float) – the rotation (in degrees). Defaults to 0.
scale (float) – the zoom factor. Defaults to 1.
scale_x (float) – additional horizontal-only zoom factor. Defaults to 1.
scale_y (float) – additional vertical-only zoom factor. Defaults to 1.
opacity (int) – the opacity (0=transparent, 255=opaque). Defaults to 255.
color (tuple[int]) – the color to colorize the child (RGB 3-tuple). Defaults to (255,255,255).
anchor (tuple[float]) – (x, y) - point from where the image will be positioned, rotated and scaled in pixels. For example
(image.width/2, image.height/2)
is the center (default).
-
contains
(x, y)¶ Test if the point is in the area covered by the (untransformed)
Sprite
bounding box.- Returns
bool
-
draw
()¶ When the sprite is not into a batch it will be drawn with this method. If in a batch, this method is not called, and the draw is done by the batch.
-
get_AABB
()¶ - Returns
cocos.rect.Rect
– Local-coordinates Axis Aligned Bounding Box.
-
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
cocos.rect.Rect
– The bounding box for this sprite.
-
property
height
¶ Scaled height of the sprite.
Read-only. Invariant under rotation.
- Returns
int
-
property
image_anchor
¶ Point from where the image will be positioned, rotated and scaled in pixels.
- Type
tuple[float]
-
property
image_anchor_x
¶ x coordinate from where the image will be positioned, rotated and scaled in pixels.
- Type
float
-
property
image_anchor_y
¶ y coordinate from where the image will be positioned, rotated and scaled in pixels.
- Type
float
-
property
position
¶ position of the sprite in (x, y) coordinates
-
property
rotation
¶ 0 degrees
- Type
rotation in degrees of the sprite. Default
-
property
scale
¶ scale of the sprite where 1.0 is the default value
-
property
scale_x
¶ additional horizontal-only scale of the sprite where 1.0 is the default value
-
property
scale_y
¶ additional vertical-only scale of the sprite where 1.0 is the default value
-
property
width
¶ Scaled width of the sprite.
Read-only. Invariant under rotation.
- Type
int
-
property
x
¶ The x coordinate of the CocosNode
-
property
y
¶ The y coordinate of the CocosNode