Berendschot
Berendschot

Reputation: 3114

Pygame Rect class

How do I define a rectangular class in Pygame?

class square(pygame.Rect)
    def __init__(self):
        pygame.Rect.__init__(self)

The code above, which you will use to define a sprite class doesn't work.

Upvotes: 1

Views: 1806

Answers (1)

Daniel Serretti
Daniel Serretti

Reputation: 342

I think what you want is this:

class Rectangle(object):
    def __init__(self, top_corner, width, height):
        self._x = top_corner[0]
        self._y = top_corner[1]
        self._width = width
        self._height = height

    def get_bottom_right(self):
        d = self._x + self.width
        t = self._y + self.height
        return (d,t)

You can use this like this:

# Makes a rectangle at (2, 4) with width
# 6 and height 10
rect = new Rectangle((2, 4), 6, 10) 

# Returns (8, 14)
bottom_right = rect.get_bottom_right

Also, you could probably save yourself some time by making a Point class

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

Upvotes: 2

Related Questions