Activity Team/Sugar-sprites

Sugar sprites library
While not yet an official part of Sugar, there is a simple sprites library, sprites.py, that is used by many activities, including Turtle Art, Visual Match, Slideruler, Cardsort, Erikos, etc.

sprites.py is a simple sprites library for managing graphics objects, 'sprites', on a canvas. It manages multiple sprites with methods such as move, hide, set_layer, etc.

There are two classes:


 * class Sprites maintains a collection of sprites.
 * class Sprite manages individual sprites within the collection.

Example usage
The basic work flow is to first create a Sprites instance to manage the sprite collection. Then create and use individual sprites.

# Import the classes into your program. from sprites import Sprites, Sprite

# Create a new sprite collection for a gtk Drawing Area. my_drawing_area = gtk.DrawingArea self.sprite_list = Sprites(my_drawing_area)

# Create a "pixbuf" (in this example, from SVG). my_pixbuf = svg_str_to_pixbuf(" ...some svg code... ")

# Create a sprite at position x1, y1. my_sprite = sprites.Sprite(self.sprite_list, x1, y1, my_pixbuf)

# Move the sprite to a new position. my_sprite.move((x1+dx, y1+dy))

# Create another "pixbuf". your_pixbuf = svg_str_to_pixbuf(" ...some svg code... ")

# Create a sprite at position x2, y2. your_sprite = sprites.Sprite(self.sprite_list, x2, y2, my_pixbuf)

# Assign the sprites to layers. # In this example, your_sprite will be on top of my_sprite. my_sprite.set_layer(100) your_sprite.set_layer(200)

# Now put my_sprite on top of your_sprite. my_sprite.set_layer(300)

def svg_str_to_pixbuf(svg_string): pl = gtk.gdk.PixbufLoader('svg') pl.write(svg_string) pl.close pixbuf = pl.get_pixbuf return pixbuf
 * 1) method for converting SVG to a gtk pixbuf

The Sprites Class
class Sprites

methods:

def __init__(self, canvas, area=None, gc=None) self.canvas self.area self.gc       self.cm        self.list = []

def get_sprite(self, i)

def length_of_list(self)

def append_to_list(self, spr)

def insert_in_list(self, spr, i)

def remove_from_list(self, spr):

def find_sprite(self, pos):

def redraw_sprites(self):

The Sprite Class
class Sprite

methods:

def __init__(self, sprites, x, y, image): self.layer = 100 self.labels = [] self.images = []

def set_image(self, image, i=0, dx=0, dy=0):

def move(self, pos):

def move_relative(self, pos):

def get_xy(self):

def get_dimensions(self):

def get_layer(self):

def set_shape(self, image, i=0):

def set_layer(self, layer):

def set_label(self, new_label, i=0):

def set_margins(self, l=0, t=0, r=0, b=0):

def set_font(self, font):t)

def set_label_color(self, rgb):

def set_label_attributes(self, scale, rescale=True, horiz_align="center",                            vert_align="middle", i=0):

def hide(self):

def inval(self):

def draw(self):

def hit(self, pos):

def draw_label(self):

def label_width(self):

def label_safe_width(self): def label_safe_height(self): def label_left_top(self):

def get_pixel(self, pos, i=0):