【发布时间】:2014-10-07 13:09:43
【问题描述】:
如何在 Pygame 中定义一个矩形类?
class square(pygame.Rect)
def __init__(self):
pygame.Rect.__init__(self)
上面用于定义精灵类的代码不起作用。
【问题讨论】:
如何在 Pygame 中定义一个矩形类?
class square(pygame.Rect)
def __init__(self):
pygame.Rect.__init__(self)
上面用于定义精灵类的代码不起作用。
【问题讨论】:
我想你想要的是这样的:
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
【讨论】: