首页 > 解决方案 > How to check if a variable contains a rect in Pygame?

问题描述

So if I add a rect to a variable like so

box = pygame.Rect(x, y, w, h)

How do I check if the variable rect holds a pygame.Rect? Ideally it would return something similar to this

int = 9

#int is not a rect
#box is a rect

标签: pythonvariablespygamerect

解决方案


Use isinstance(object, classinfo):

Return True if the object argument is an instance of the classinfo argument [...]

if isinstance(box, pygame.Rect):
    print('is a rect')
else:
    print('is not a rect')

推荐阅读