SpriteActor

Actor class for Sprite on collision shape objects.

Since I was unable to get the calico Sprite class to work properly, this is my own, very simple, implementation of a Sprite like class.

Constructor

SpriteActor([position, base, shape, files, ...]) Actor class for Sprite on collision shape objects.

Instance Attributes

animationCostumes The list of customes used for the current animation cycle.
animationIndex The index of the current animation costume.
collision The collision function that will be called when this Actor collides with anything in the world.
costumes The list of customes available for this Actor.
currentCostume The current costume of this Actor.
currentCostumeIndex The index of the current costume of this Actor.
cycle Booleans indicating whether the current animation should cycle or not.
obstructs Boolean indicating whether or not this Sprite Actor should obstruct movement Change it before adding the Actor to the world.
separation The separation function that will be called when this Actor separates from anything in the world.
shape The main shape of the sprite actor
talkRadius The radius from where this actor will respond to the talk() command.

Methods

animate() Performs one frame of the animation of this Sprite Actor.
changeCostume(index) Makes a different shape visible.
debugMode() Enables or disables debug mode.
draw(levelWindow) Adds this object to the level window, and thus the world.
flipCostumeHoriz(index) Flips the indicated costume horizontal.
flipCostumeVert(index) Flips the indicated costume vertically.
getAvatar() Returns the first costume in the costumes list or, if the costumes list is empty, returns the shape of this Actor instead.
getRotation() Returns the rotation of this object.
getX() Returns the x coordinate of this object.
getY() Returns the y coordinate of this object.
hide() Hides this object.
hit(x, y) Returns whether the supplied coordinate hits the shape.
inRange(x, y) Checks whether the provided coordinate is within talkRadius of this Actor.
move(x, y) Moves this object by (x, y).
moveTo(x, y) Moves this object to the coordinate (x, y).
rotate(angle) Rotates this actor by a certain angle.
rotateTo(angle) Rotates this actor to a certain angle.
show() Shows this object.
speak([text, action, portrait]) Makes this object speak.
undraw() Removes the shape and speech bubble of this actor from the world.
visible() Returns whether this object is visible.

Details

class SpriteActor(position=(0, 0), base=None, shape=None, files=None, obstructs=False, bodyType=None, collision=None, separation=None, visible=None, scale=1.0, debug=False, rotation=0, **kwargs)[source]

Bases: actors.baseActor.BaseActor

Constructs a SpriteActor.

Parameters:
  • position – Tuple (x, y) indicating the starting position of the shape.
  • base – Main argument. Can send pictures, list of pictures, or a shape.
  • shape – Used if you want a separate collision object for this actor.
  • files – A list of file names, each pointing to an image. Each image will form one ‘costume’ for this ‘Sprite’. Not really needed. Can be supplied to base.
  • obstructs – Boolean indicating whether this object obstructs the movement of other objects. If ‘obstructs’ is set to True with a ‘shape’ that does not have a body, a warning will be printed and the object will not obstruct anything.
  • bodyType – String indicating the type of body that should be added to the physics simulator. Possible values are: ‘static’ and ‘dynamic’.
  • collision – Function of the form func(myfixture, otherfixture, contact), called when the ‘shape’ collides with anything.
  • separation – Function of the form func(myfixture, otherfixture), called when the ‘shape’ separates from anything.
  • visible – Boolean indicating whether this Sprite Actor should be visible.
  • scale – A float, indicating by what factor images should be scaled.
  • debug – Boolean indicating whether to paint this object in debug mode.
  • rotation – Float indicating by how many radians the sprite actor should be rotated.
Keyword Arguments:
 
  • interact – Function of the form func(self, items), called when this object is interacted with.
  • use – Function of the form func(self) called when this object is used.
  • pickup – Anything, generally will be the object that is returned to the player upon pickup. If set to True, will return the Actor object itself instead. If set to True, and a value for returned is povided, will return whatever value returned was set to. (deprecated) If pickup is the string “self”, a reference to this instance will be returned instead, just as if pickup was set to True.
  • returned – Anything, will be the object that is returned to the player if pickup is set to True.
  • onPickup – Function of the form func(self) called when this object is picked up by the player. Note: passing a value for onPickup is NOT enough to enable the player to pickup this object.
  • description – A description of this object, used when this object is printed. If left blank, the class and memory address of this object is printed.
  • tag – The tag of the actor, usually a string helpful for identifying what kind of actor you are dealing with.

Instance Attributes

animationCostumes

The list of customes used for the current animation cycle. Note that this is a list of indices, not costumes. Based on these indices, the actual costumes will be retrieved from the costumes list.

animationIndex

The index of the current animation costume.

collision

The collision function that will be called when this Actor collides with anything in the world. Change it before adding the Actor to the world.

costumes

The list of customes available for this Actor.

currentCostume

The current costume of this Actor.

currentCostumeIndex

The index of the current costume of this Actor.

cycle

Booleans indicating whether the current animation should cycle or not.

obstructs

Boolean indicating whether or not this Sprite Actor should obstruct movement Change it before adding the Actor to the world.

separation

The separation function that will be called when this Actor separates from anything in the world. Change it before adding the Actor to the world.

shape

The main shape of the sprite actor

talkRadius

The radius from where this actor will respond to the talk() command. Not used by default.

Methods

animate()[source]

Performs one frame of the animation of this Sprite Actor.

One frame of animation means that the current costume is changed to the costume at the index at the animationIndex of the animationCostumes list. The animationIndex is then incremented by one. If the animationIndex reaches the end of the animationCostumes list, the animationIndex will either be reset (if cycle is True), or the onEndOfAnimation callback is called.

Returns:True if the animation is still ongoing, or False if this was the last frame of the animation and cycle is False.
changeCostume(index)[source]

Makes a different shape visible.

Parameters:index – The index of the shape.
debugMode()[source]

Enables or disables debug mode.

In debug mode, many objects that are invisible are now visible and transparant.

Parameters:debug – Boolean indicating whether to enable (True) or disable (False) debug mode.
draw(levelWindow)[source]

Adds this object to the level window, and thus the world.

Parameters:levelWindow – A LevelWindow object.
flipCostumeHoriz(index)[source]

Flips the indicated costume horizontal.

Parameters:index – The index of the costume to flip.
flipCostumeVert(index)[source]

Flips the indicated costume vertically.

Parameters:index – The index of the costume to flip.
getAvatar()[source]

Returns the first costume in the costumes list or, if the costumes list is empty, returns the shape of this Actor instead.

Returns:A Calico shape object, usually a Figure.
getRotation()[source]

Returns the rotation of this object.

Returns:rotation of this object.
getX()[source]

Returns the x coordinate of this object.

Returns:A float indicating the x coordinate of this object.
getY()[source]

Returns the y coordinate of this object.

Returns:A float indicating the y coordinate of this object.
hide()[source]

Hides this object.

hit(x, y)[source]

Returns whether the supplied coordinate hits the shape.

Parameters:
  • x – A float indicating an x coordinate.
  • y – A float indicating a y coordinate.
Returns:

True if the coordinate (x, y) falls onto the shape.

inRange(x, y)[source]

Checks whether the provided coordinate is within talkRadius of this Actor.

Parameters:
  • x – Float indicating the horizontal aspect of the coordinate.
  • y – Float indicating the horizontal aspect of the coordinate.
Returns:

Boolean indicating whether the provided coordinate was in talkRadius of this Actor.

move(x, y)[source]

Moves this object by (x, y).

Parameters:
  • x – Float indicating horizontal displacement. Negative values move left, positive values move right.
  • y – Float indicating vertical displacement. Negative values move up, positive values move down.
moveTo(x, y)[source]

Moves this object to the coordinate (x, y).

Parameters:
  • x – Float for the x coordinate.
  • y – Float for the y coordinate.
rotate(angle)[source]

Rotates this actor by a certain angle.

Parameters:angle – Float indicating the angle to rotate.
rotateTo(angle)[source]

Rotates this actor to a certain angle.

Parameters:angle – Float indicating the angle to rotate to.
show()[source]

Shows this object.

speak(text=None, action=None, portrait=None)[source]

Makes this object speak.

User initiated ‘talk()’ callbacks will go directly to the speech bubble, meaning this function is only for if you want to manually make this object say something.

Parameters:
  • text – A string containing the text to be displayed.
  • action – A function executed at the end of this function.
undraw()[source]

Removes the shape and speech bubble of this actor from the world.

Note: be carefull where you remove objects from the world, since removing objects while they are being used can lead to unstable behavior. The level thread is the recommended place to do so.

visible()[source]

Returns whether this object is visible.

Returns:True if this object is visible, False otherwise.