Sprites and ICalico Widgets

This example shows an example of the Graphics.Sprite class, and the ICalico Widgets to show animation.

First, we load one of the built-in sprites, "bear":

In [1]:
from Graphics import *
win = Canvas(200, 200)
sprite = Sprite((100, 100), "bear")
sprite.draw(win)
Out[1]:

We can programmatically see what costumes are available:

In [2]:
sprite.getCostumeNames()
Out[2]:
[left, right]

Now, let's build a user interface to show the animation frames for each costume:

In [3]:
frame_count = len(sprite.getFrames())
costume_names = sprite.getCostumeNames()

slider = calico.IntSliderWidget(min=0, max=frame_count-1, description="Frames in Sprite Animation")
pick = calico.DropdownWidget(value_names=costume_names, value_name=sprite.costume)

def change_frame(value_name, value):
    sprite.flipToFrame(value)
    calico.animate(sprite.shape)

def change_costume(value_name, value):
    sprite.changeCostume(value)
    calico.animate(sprite.shape)

slider.on_value_change(change_frame, "value")
pick.on_value_change(change_costume, "value_name")

calico.display(sprite.shape)
calico.ContainerWidget(children=[pick, slider])
In [4]:
# FIXME: this doesn't work yet
pick.value_name = "left"
In [ ]: