因此,在我的 pygame 游戏中,我创建了一个对象列表,以便更轻松地更新所有对象和进行碰撞检查。所以当我进行碰撞检查时,我必须检查当前对象是否与我们正在进行碰撞检查的对象相同。这是我当前的代码:
def placeMeeting(self, object1, object2):
# Define positioning variables
object1Rect = pygame.Rect(object1.x, object1.y, object1.width, object1.height)
# Weather or not they collided
coll = False
# Loop through all walls to check for possible collision
for i in range(len(self.instances)):
# First check if it's the right object
if (self.instances[i] == object2):
print "yep"
object2Rect = pygame.Rect(self.instances[i].x, self.instances[i].y, self.instances[i].width, self.instances[i].height)
# Check for collision with current wall -- Horizontal
if (object1Rect.colliderect(object2Rect)):
coll = True
# Return the final collision result
return coll
(列表/数组中的所有对象都是 su 的子对象)
最佳答案
简单而强大 => type(a) is type(b)
>>> class A:
... pass
...
>>> a = A()
>>> b = A()
>>> a is b
False
>>> a == b
False
>>> type(a)
<class '__main__.A'>
>>> type(b)
<class '__main__.A'>
>>> type(a) is type(b)
True
>>> type(a) == type(b)
True
>>>
关于python - 如何检查两个实例是否属于同一类 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29837656/