简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM