2014-10-07 41 views
1

如何在Pygame中定義一個矩形類?Pygame Rect class

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

上面的代碼,你將用來定義一個精靈類不起作用。

回答

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 
+0

雖然這不是我正在尋找的,但它確實(幾乎)解決了我的問題。 'rect = new Rectangle((2,4),6,10)'不被python支持,但這也可能是我的python版本的錯誤。 – Berendschot 2014-10-07 17:43:26