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 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')
Parameters: - image (str or pyglet.image.AbstractImage) – name of the image resource or a pyglet image.
- 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.
-
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.
Returns: int
-
image_anchor
¶ tuple[float] – Point from where the image will be positioned, rotated and scaled in pixels.
-
image_anchor_x
¶ float – x coordinate from where the image will be positioned, rotated and scaled in pixels.
-
image_anchor_y
¶ float – y coordinate from where the image will be positioned, rotated and scaled in pixels.
-
opacity
= None¶ opacity of the sprite where 0 is transparent and 255 is solid
-
position
¶ position of the sprite in (x, y) coordinates
-
rotation
¶ rotation in degrees of the sprite. Default – 0 degrees
-
scale
¶ scale of the sprite where 1.0 is the default value
-
scale_x
¶ additional horizontal-only scale of the sprite where 1.0 is the default value
-
scale_y
¶ additional vertical-only scale of the sprite where 1.0 is the default value
-
width
¶ Scaled width of the sprite.
Read-only. Invariant under rotation.
Type: int
-
x
¶ The x coordinate of the CocosNode
-
y
¶ The y coordinate of the CocosNode