简体   繁体   中英

Issue with the use of collide_rect pygame

I've been trying to use the collide_rect function in pygame. What it says is that "descriptor 'colliderect' requires a 'pygame.Rect' object but received a 'instance'". Here is the way I'm using colliderect:

collision = pygame.Rect.colliderect(player, enemy)
if collision == True:
    keepgoing = False

So what do I do?

colliderect is a method on the Rect class, so you invoke it by calling it on a Rect directly. Also, you should not compare directly to True . Some methods don't return a boolean value and it's generally easier and idiomatic to simply use the return value in the if statement:

if player.colliderect(enemy):
    keepgoing = False

This of course assumes that both player and enemy are Rect objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM