Calico Sprites

From IPRE Wiki
Jump to: navigation, search

Calico Sprites is a library designed to allow the creation of animated stories and video games. Future updates will provide Pygame-like functionality. Please feel free to suggest enhancements or report bugs to the issue tracker. See documentation for the next version here.

from Sprites import *

init()
s = getSprite("Sprite1")
s.changeCostume("pinkHair")
s.glideForward(30, 1)
s.glideTo(170, 220, 1)

Using the Sprites Module

The five original Sprite costumes. See addCostume() for more details.
The three original Sprite backdrops. See addBackground() for more details.


Use the Sprites module to create animations and video games! Sprites appear as stick figures on the sketch window and can move around, speak, change their appearance, and interact with each other. A Sprite could be thought of as a person, or by changing its costume, it could take on the role of a tree, cloud, door, car, etc. in a sketch.

The Sprites module may be used in the transition from non-object oriented programing to object oriented programming / programming using dot notation. Both these styles of programming can even be used within the same script.


Non Object Oriented Programming

Create as many Sprites as you want. They may be given customized names or assigned names in a "Sprite#" format (like "Sprite1" or "Sprite2"). Any static methods called will be applied to the current Sprite. The current Sprite starts off as "Sprite1" which is automatically created when the sketch is initialized. In order to control multiple Sprites, use selectSprite(spriteName) to change the current Sprite and then call the static methods.

from Sprites import *

init()                     #automatically creates "Sprite1"
makeSprite("Cally")
selectSprite("Sprite1")    #changes the current Sprite to "Sprite1"
moveTo(400, 200)
selectSprite("Cally")      #changes the current Sprite to "Cally"
changeCostume("pinkHair")
glideForward(30, 1.5)
makeSprite()               #creates "Sprite2"
selectSprite("Sprite2")    #changes the current Sprite to "Sprite2"
changeCostume("greenHair")
glideTo(170, 220, 1)

ExampleCode.gif



Object Oriented Programming

Create Sprites as before, but instead of using selectSprite(spriteName) to change the current Sprite, assign each Sprite to a variable using getSprite(spriteName). Then use dot-notation on these variables. See dotNotation.py for a more detailed example.

from Sprites import *

init()                                  #automatically creates Sprite1
defaultSprite = getSprite("Sprite1")
makeSprite("Goldy", "goldHair.png")     #creates Goldy
goldy = getSprite("Goldy")

goldy.moveTo(400, 220)

listOfSprites = []
listOfSprites.append(defaultSprite)
listOfSprites.append(goldy)

for sprite in listOfSprites:
    sprite.printStatus()
    print()

DotNotationInJigsaw.gif

Examples

As of Calico Version 2.3.9, there are six examples using the Sprites module already in Calico. Jigsaw examples can be found at Calico/examples/jigsaw/Sprites while Python examples can be found at Calico/examples/python/Sprites (or by using Calico's built in Example menu).

Jigsaw

SimpleDemo.jig - the basics of how to use the Sprites module.

DanceParty.jig - how imitateSprite() works.

InitDemo.jig - how init(spritesVisibleOnCreation) and makeSpritesVisibleOnCreation() work.


Python

simpleDemo.py - the basics of how to use the Sprites module.

soundDemo.py - how play() and playFor() work.

dotNotation.py - how to use the Sprites module in an object-oriented fashion.

Sprites tab

containsSprite(string spriteName) - Returns True if the specified Sprite is in the sketch.


getSprite(string spriteName) - Returns the specified Sprite.


init() - Creates a new sketch window if none exists or resets the current sketch window. A default Sprite named "Sprite1" is automatically created and drawn on the left side of the window.


init(bool spritesVisibleOnCreation) - init(True) works the same way as init(). init(False) will create a default Sprite named "Sprite1," but this Sprite will not be automatically drawn to the window. Instead, you may choose when you want the Sprite to appear by using show(). See InitDemo.jig for details.


makeSprite() - Creates a new Sprite. It's name will be automatically generated in a "Sprite{number}" format, like "Sprite1," "Sprite2," etc.


makeSprite(string spriteName) - Creates a new Sprite with the specified name.


makeSprite(string spriteName, string filename) - Creates a new Sprite with the specified name. Instead of its default costume being a plain stick figure, its default will be the picture specified by filename (which should be placed in examples/images/SpriteCostumes).


makeSpritesVisibleOnCreation(bool visible) - After setting visible to True, Sprites will automatically be drawn when they are created. Setting visible to False will allow the creation of Sprites, but when made they will not be automatically drawn. See InitDemo.jig for details.


printAllSpriteNames() - Prints the names of all the existing Sprites to the console.


printSpriteStatus() - Prints the current Sprite's coordinate position, rotation, and visibility setting to the console.


removeSprite(string spriteName) - Removes the specified Sprite from the sketch.


selectSprite(string spriteName) - Changes the current Sprite (useful when using multiple Sprites without dot-notation programming). Also returns the specified Sprite.

Sprites.init()                               //automatically creates "Sprite1"
Sprites.makeSprite("Cally")

Sprites.selectSprite("Sprite1")              //changes the current Sprite to "Sprite1," moves it to the hill on the right, and changes its costume to greenHair.  
Sprites.moveTo(450, 200)
Sprites.changeCostume("greenHair")

Sprites.selectSprite("Cally")                //changes the current Sprite to "Cally" and changes its costume to pinkHair.  
Sprites.changeCostume("pinkHair")


wait(double seconds) - Pauses for the given amount of seconds. Seconds can be a decimal number.

Sprites Interact tab

imitateSprite(string spriteName) - Makes the current Sprite imitate the specified Sprite when the specified Sprite moves, glides, rotates, scales, flips, and hides/shows. See DanceParty.jig for details.


stopImitating() - Makes the current Sprite stop imitating the Sprite it was following.


Note: future update will allow multiple Sprites to follow one Sprite, however this version only allows a one-imitates-another format.

Sprites Look tab

addBackground(string filename) - The Sprites module will automatically load two different backgrounds, landscape.png and sun.png, but this method may be used to add more background options to the sketch. (Make sure to add the image to examples/images/SpritesCostumes first.) The stage image is already in this folder so that you may see how this works.

Sprites.addBackground("stage.png")

Note: Images that are 640 x 480 pixels will fit the sketch window best, but this is not required.



addCostume(string filename) - Each Sprite will automatically load three different costumes, default.png, pinkHair.png, and greenHair.png, but this method may be used to add more costume options to the current Sprite. (Make sure to add the image to examples/images/SpritesCostumes first.) redHair.png and goldHair.png have already been added to this folder so that you may see how this works.

Sprites.addCostume("redHair.png")
Sprites.addCostume("goldHair.png")

Note: Images that are 50 x 70 pixels will match the original Sprite costumes, but this is not required.



changeBackground(string backdropName) - Changes the background of the sketch. The backdrops are accessed by name without their extension ("stage" instead of "stage.png").

Sprites.changeBackground("stage")


changeCostume(string costumeName) - Changes the costume of the current Sprite. The costumes are accessed by name without their extension ("goldHair" instead of "goldHair.png").

Sprites.changeCostume("goldHair")


flipHorizontal() - Flips the current Sprite horizontally.


flipVertical() - Flips the current Sprite vertically.


hide() - Undraws the current Sprite from the sketch window. Use show() to make the Sprite reappear.


isVisible() - Returns True if the current Sprite is drawn on the sketch window, returns False otherwise.


show() - Draws the current Sprite on the sketch window. Use hide() to make the Sprite disappear.

Sprites Move tab

backward(double pixels) - Moves the current Sprite backward by the specified number of pixels. (This usually means to the left unless the Sprite has been rotated.)


changeXPositionBy(double pixels) - Changes the current Sprite's X position by the specified number of pixels.

//Moves the current Sprite left by 50 pixels

Sprites.changeXPositionBy(-50)


changeYPositionBy(double pixels) - Changes the current Sprite's Y position by the specified number of pixels.

//Moves the current Sprite up by 50 pixels

Sprites.changeYPositionBy(-50)


forward(double pixels) - Moves the current Sprite forward by the specified number of pixels. (This usually means to the right unless the Sprite has been rotated.)


glideBackward(double pixels, double seconds) - Animates the current Sprite to move backwards by the specified number of pixels. This movement will be completed over the specified number of seconds.


glideForward(double pixels, double seconds) - Animates the current Sprite to move forwards by the specified number of pixels. This movement will be completed over the specified number of seconds.


glideTo(double x, double y, double seconds) - Animates the current Sprite to move to the specified (x, y) coordinate. This movement will be completed over the specified number of seconds.


move(double x, double y) - Changes the current Sprite's x position by x and its y position by y.

//Moves the current Sprite down by 5 pixels and right by 5 pixels

Sprites.move(5, 5)


moveTo(double x, double y) - Moves the current Sprite to the specified (x, y) coordinate.


rotate(double degrees) - Rotates the current Sprite counter-clockwise by the specified number of degrees.

//Results in a final rotation of 60.

Sprites.rotate(30)
Sprites.rotate(30)


rotateTo(double degrees) - Rotates the current Sprite clockwise to the specified number of degrees.

//Results in a final rotation of 60.

Sprites.rotateTo(-60)
Sprites.rotateTo(-60)


scale(double factor) - Scales the current Sprite by a percent (.1 is 10% of original; 1.1 is 10% larger).


scaleTo(double factor) - Scales the current Sprite to the specified factor (1 is 100% of original).


Note: The origin (0,0) is at the top left of the sketch window. X and Y values increase as you move right and down the sketch window respectively.

Note: Control waits for the glide functions to complete before moving on. Future updates will allow control to continue to the next action before glide completes, letting glide continue to animate in the background.

Sprites Sound tab

play(string filename) - Plays the specified file. Choose from the .wav files in examples/sounds. See soundDemo.py for a demonstration.


playFor(string filename, double seconds) - Plays the specified file on repeat for the specified number of seconds. Control continues on after the audio file begins playing. See soundDemo.py for a demonstration.


printVoiceOptions() - Prints all available voice accents.


setVoice(string voiceID) - Sets the current Sprite's voice accent.


speak(string message, double seconds) - Speaks the message while displaying a speech bubble above the current Sprite.