Ejemplo n.º 1
0
    def left_of(self, sprite):
        """Is this sprite left of another sprite"""
        (x1, y1, x2, y2) = canvas().bbox(self.spriteid)
        (sx1, sy1, sx2, sy2) = canvas().bbox(sprite.spriteid)

        # Are we overlapped on y axis and left?
        return (y1 < sy2 and y2 > sy1) and (x1 < sx1)
Ejemplo n.º 2
0
    def below(self, sprite):
        """Is this sprite below another sprite (further down the screen)"""
        (x1, y1, x2, y2) = canvas().bbox(self.spriteid)
        (sx1, sy1, sx2, sy2) = canvas().bbox(sprite.spriteid)

        # Are we overlapped on x axis and below?
        return (x1 < sx2 and x2 > sx1) and (y2 > sy2)
Ejemplo n.º 3
0
    def above(self, sprite):
        """Is this sprite above another sprite (further up the screen)?"""
        (x1, y1, x2, y2) = canvas().bbox(self.spriteid)
        (sx1, sy1, sx2, sy2) = canvas().bbox(sprite.spriteid)

        # Are we overlapped on x axis and above?
        return (x1 < sx2 and x2 > sx1) and (y1 < sy1)
Ejemplo n.º 4
0
 def move(self, x, y):
     """Move by x and y pixels"""
     if self.pen:
         cx, cy = self.pos()
         canvas().create_line(cx,
                              cy,
                              cx + x,
                              cy + y,
                              width=self.pen_width,
                              fill=self.pen_colour_hex,
                              tags="pen")
     canvas().move(self.spriteid, x, y)
Ejemplo n.º 5
0
    def __init__(self, imgs, x=100, y=100):
        self.costume_ids = []
        self.photo_images = []

        if isinstance(imgs, list):
            tag = ImageSprite.unique_tagname()
        else:
            imgs = [imgs]
            tag = None  # Just one sprite

        for img in imgs:
            if isinstance(img, str):
                assert img.lower().endswith(
                    ".gif"), "Sorry, ImageSprite only works with GIFs"
                self.photo_images.append(PhotoImage(file=img))
            else:
                self.photo_images.append(img)

            if self.costume_ids:
                # Successive sprites are hidden offscreen
                x, y = offscreen(x, y)

            spriteid = canvas().create_image(x,
                                             y,
                                             image=self.photo_images[-1],
                                             tag=tag)
            self.costume_ids.append(spriteid)

        super(ImageSprite, self).__init__(self.costume_ids[0])
        self.switch_costume(1)
Ejemplo n.º 6
0
 def __init__(self, points, **attribs):
     """Points is a list of (x,y) tuples, with its centre at (0,0)"""
     assert isinstance(points[0][0] + points[0][1],
                       int), "Points should be a list of (x,y) tuples"
     self.points = points
     self.attribs = attribs
     spriteid = canvas().create_polygon(points, attribs)
     super(PolygonSprite, self).__init__(spriteid)
     self.created_at_x, self.created_at_y = self.pos()
Ejemplo n.º 7
0
    def rotate(self, angle):
        """Rotate this polygon by an angle and redraw it"""
        # Capture where this sprite got to
        current_x, current_y = self.pos()
        dx = current_x - self.created_at_x
        dy = current_y - self.created_at_y

        self.points = [rotate_point(x, y, angle) for x, y in self.points]
        self.replace_canvas_object(canvas().create_polygon(
            self.points, self.attribs))
        self.created_at_x, self.created_at_y = self.pos()
        self.move(dx + 1, dy + 1)  # Why is +1 needed?
Ejemplo n.º 8
0
 def delete(self):
     """Delete this sprite from the canvas"""
     canvas().delete(self.spriteid)
Ejemplo n.º 9
0
 def height(self):
     """The height in px of this sprite"""
     box = canvas().bbox(self.spriteid)
     return box[3] - box[1]
Ejemplo n.º 10
0
 def width(self):
     """The width in px of this sprite"""
     box = canvas().bbox(self.spriteid)
     return box[2] - box[0]
Ejemplo n.º 11
0
 def pos(self):
     """Return (x,y) of this sprite"""
     return canvas().bbox(self.spriteid)[0:2]
Ejemplo n.º 12
0
    def right_of(self, sprite):
        (x1, y1, x2, y2) = canvas().bbox(self.spriteid)
        (sx1, sy1, sx2, sy2) = canvas().bbox(sprite.spriteid)

        # Are we overlapped on y axis and right?
        return (y1 < sy2 and y2 > sy1) and (x2 > sx2)
Ejemplo n.º 13
0
    def touching(self, sprite):
        """Is this sprite touching another sprite?"""
        our_box = canvas().bbox(self.spriteid)
        their_box = canvas().bbox(sprite.spriteid)

        return overlapping_rect(our_box, their_box) is not None