Pygame Rect类

时间:2014-10-07 13:09:43

标签: python class pygame rect

如何在Pygame中定义矩形类?

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

您将用于定义精灵类的上述代码无法正常工作。

1 个答案:

答案 0 :(得分:2)

我认为你想要的是:

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)

你可以这样使用:

# 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

此外,您可以通过制作Point类

来节省一些时间
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y