How to Use Sprite Collide in Pygame

落花浮王杯 提交于 2019-11-29 16:15:35
def checkCollision(sprite1, sprite2):
    col = pygame.sprite.collide_rect(sprite1, sprite2)
    if col == True:
        sys.exit()

should be

def checkCollision(self, sprite1, sprite2):
    col = pygame.sprite.collide_rect(sprite1, sprite2)
    if col == True:
        sys.exit()

since it's a method bound to an object.

Change

def checkCollision(sprite1, sprite2):

To

def checkCollision(self, sprite1, sprite2):

And you don't have to check the collision on every event, reduce the indent of rock.checkCollision(bird.image_b, rock.image_b) by 1.

You have this:

col = pygame.sprite.collide_rect(sprite1, sprite2)

But an easier way to do this would be to simply use colliderect which is a function of rect. It might be easier to try this:

col=sprite1.rect.colliderect(sprite2.rect)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!