python - Pygame 类型错误:参数必须是矩形样式对象
问题描述
我创建了这个类,所以我可以更好地工作
class Block:
def __init__(self, top, left):
self.Rect = pygame.Rect((top,left),(10,10))
然后我想创建一个像这样剪辑矩形的简单函数
def clip(rect_obj):
return pygame.Rect.clip(rect_obj)
但是当我将 rect 传递给我的函数时,我在标题中得到了错误。我什至打印了类型
my_block = Block()
print(type(my_block.Rect)) --> <Class Pygame.Rect>
my_rect = clip(my_block.Rect) --> TypeError: Argument must be a rect style Object
我想知道矩形样式对象和矩形对象之间是否有区别,因为我很困惑。在屏幕上绘制矩形也是布局井字游戏 GUI 的最佳方式,还是更简单地绘制线条。
解决方案
错误是行pygame.Rect.clip(rect_obj)
def clip(rect_obj): return pygame.Rect.clip(rect_obj)
这条线根本没有任何意义,因为pygame.Rect.clip
它是一个实例方法。 pygame.Rect.clip()
:
返回一个被裁剪为完全在参数 Rect 内的新矩形。如果两个矩形开始时不重叠,则返回大小为 0 的 Rect。
pygame.Rect
该方法与 2 个对象相交:
rect_1 = pygame.Rect(0, 0, 20, 20)
rect_2 = pygame.Rect(10, 10, 30, 30)
rect_and = rect_1.clip(rect_2 )
rect_and
是 (10, 10) 处的矩形,大小为 (10, 10)。
可能您尝试将剪切区域设置为显示表面。这可以通过pygame.Surface.set_clip()
. 例如:
screen = display.set_mode((width, height))
clip_rect = pygame.Rect(x, y, w, h)
screen.set_clip(clip_rect )
推荐阅读
- javascript - Blazor JS 集成
- python - 使用 for 循环创建 kivvy 文本输入/标签
- javascript - 如何访问局部范围内的全局变量?
- python - 我是否从 sympy 获得角度的主要价值?
- r - 如何解释 is() 的输出?
- html - SVG 无法正确旋转
- java - 如何以编程方式从 Log4j2 配置/任何其他 Log4j2 容器中删除 Appender?
- c++ - 具有双精度类型值的数组实现的 C++ 堆栈
- google-cloud-firestore - 在 FireStore 中如何知道客户端是否正在收听特定文档
- javascript - 对唯一的有界数组进行排序的最有效方法?