【问题标题】:Pygame Rect classPygame 矩形类
【发布时间】:2014-10-07 13:09:43
【问题描述】:

如何在 Pygame 中定义一个矩形类?

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

上面用于定义精灵类的代码不起作用。

【问题讨论】:

    标签: python class pygame rect


    【解决方案1】:

    我想你想要的是这样的:

    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
    

    【讨论】:

    • 虽然这不是我想要的,但它确实(几乎)解决了我的问题。 python不支持'rect = new Rectangle((2, 4), 6, 10)',但这也可能是我的python版本的错误。
    猜你喜欢
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多