From b3ff1a9b72e3b5a6d6303a31c111a3c2d47b90ab Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 30 Jan 2010 00:43:33 +0000 Subject: added support for multi-image sprites --- diff --git a/sprites.py b/sprites.py index 6d5c942..78c42c1 100644 --- a/sprites.py +++ b/sprites.py @@ -150,20 +150,23 @@ class Sprite: self._margins = [0,0,0,0] self.layer = 100 self.labels = [] + self.images = [] self.set_image(image) self._sprites.append_to_list(self) - def set_image(self, image): - if image is None: - self._width, self._height = 0,0 - self.image = None - return - self.image = image - if isinstance(self.image, gtk.gdk.Pixbuf): - self._width = self.image.get_width() - self._height = self.image.get_height() + def set_image(self, image, i=0): + while len(self.images) < i+1: + self.images.append(None) + self.images[i] = image + if isinstance(self.images[i], gtk.gdk.Pixbuf): + _w = self.images[i].get_width() + _h = self.images[i].get_height() else: - self._width, self._height = self.image.get_size() + _w, _h = self.images[i].get_size() + if _w > self._width: + self._width = _w + if _h > self._height: + self._height = _h def move(self, pos): self.inval() @@ -185,9 +188,9 @@ class Sprite: def get_layer(self): return self.layer - def set_shape(self, image): + def set_shape(self, image, i=0): self.inval() - self.set_image(image) + self.set_image(image, i) self.inval() def set_layer(self, layer): @@ -248,12 +251,13 @@ class Sprite: gtk.gdk.Rectangle(self._x,self._y,self._width,self._height), False) def draw(self): - if isinstance(self.image, gtk.gdk.Pixbuf): - self._sprites.area.draw_pixbuf( - self._sprites.gc, self.image, 0, 0, self._x, self._y) - elif self.image is not None: - self._sprites.area.draw_drawable( - self._sprites.gc, self.image, 0, 0, self._x, self._y, -1, -1) + for i in self.images: + if isinstance(i, gtk.gdk.Pixbuf): + self._sprites.area.draw_pixbuf( + self._sprites.gc, i, 0, 0, self._x, self._y) + elif i is not None: + self._sprites.area.draw_drawable( + self._sprites.gc, i, 0, 0, self._x, self._y, -1, -1) if len(self.labels) > 0: self.draw_label() @@ -323,11 +327,11 @@ class Sprite: return((self._width-self._margins[0]-self._margins[2], self._width-self._margins[1]-self._margins[3])) - def get_pixel(self, pos): + def get_pixel(self, pos, i=0): x, y = pos x = x-self._x y = y-self._y - if y > self.image.get_height()-1: + if y > self.images[i].get_height()-1: return (-1,-1,-1,-1) try: array = self.image.get_pixels() diff --git a/tacanvas.py b/tacanvas.py index fe79404..759434e 100644 --- a/tacanvas.py +++ b/tacanvas.py @@ -76,7 +76,7 @@ class TurtleGraphics: (self.cx, self.cy) = self.canvas.get_xy() self.canvas.type = 'canvas' self.canvas.set_layer(CANVAS_LAYER) - self.gc = self.canvas.image.new_gc() + self.gc = self.canvas.images[0].new_gc() self.tw.active_turtle.show() self.shade = 0 self.clearscreen() @@ -84,7 +84,7 @@ class TurtleGraphics: def clearscreen(self): rect = gtk.gdk.Rectangle(0, 0, self.width, self.height) self.gc.set_foreground(self.tw.bgcolor) - self.canvas.image.draw_rectangle(self.gc, True, *rect) + self.canvas.images[0].draw_rectangle(self.gc, True, *rect) self.invalt(0, 0, self.width, self.height) self.setpensize(5) self.setcolor(0) @@ -154,7 +154,7 @@ class TurtleGraphics: x,y = self.width/2+int(cx-r), self.height/2-int(cy+r) w,h = int(2*r), int(2*r) if self.pendown: - self.canvas.image.draw_arc(self.gc, False, x, y, w, h, + self.canvas.images[0].draw_arc(self.gc, False, x, y, w, h, int(180-self.heading-a)*64, int(a)*64) self.invalt(x-self.pensize*self.tw.coord_scale/2-3, y-self.pensize*self.tw.coord_scale/2-3, @@ -172,7 +172,7 @@ class TurtleGraphics: x,y = self.width/2+int(cx-r), self.height/2-int(cy+r) w,h = int(2*r), int(2*r) if self.pendown: - self.canvas.image.draw_arc(self.gc,False, x, y, w, h, + self.canvas.images[0].draw_arc(self.gc,False, x, y, w, h, int(360-self.heading)*64, int(a)*64) self.invalt(x-self.pensize*self.tw.coord_scale/2-3, y-self.pensize*self.tw.coord_scale/2-3, @@ -239,7 +239,7 @@ class TurtleGraphics: self.setcolor(c); self.setshade(s) rect = gtk.gdk.Rectangle(0,0,self.width,self.height) self.gc.set_foreground(self.tw.fgcolor) - self.canvas.image.draw_rectangle(self.gc, True, *rect) + self.canvas.images[0].draw_rectangle(self.gc, True, *rect) self.invalt(0,0,self.width,self.height) self.setcolor(oldc); self.setshade(olds) @@ -264,7 +264,7 @@ class TurtleGraphics: def draw_pixbuf(self,pixbuf,a,b,x,y,w,h): w *= self.tw.coord_scale h *= self.tw.coord_scale - self.canvas.image.draw_pixbuf(self.gc, pixbuf, a, b, x, y) + self.canvas.images[0].draw_pixbuf(self.gc, pixbuf, a, b, x, y) self.invalt(x,y,w,h) def draw_text(self, label, x, y, size, w): @@ -284,7 +284,7 @@ class TurtleGraphics: pl = self.tw.window.create_pango_layout(str(label)) pl.set_font_description(fd) pl.set_width(int(w)*pango.SCALE) - self.canvas.image.draw_layout(self.gc,int(x),int(y),pl) + self.canvas.images[0].draw_layout(self.gc,int(x),int(y),pl) w,h = pl.get_pixel_size() self.invalt(x,y,w,h) @@ -296,7 +296,7 @@ class TurtleGraphics: if y1