Tobias Laving
Tobias Laving

Reputation: 35

Unable to use pygames collidepoint with other classes

I've had some problems using Pygame's collidepoint method through another class:

if(mouseclick[0]):
    for tile in self.engine.level.levellist:
        if tile.collidepoint(mousepos):

This small and easy code gives me an error:

if tile.collidepoint(mousepos):

AttributeError: TileClass instance has no attribute 'collidepoint'

Anyone know what i'm doing wrong? Been awhile since I used python and I'm probably just missing some easily fixed.

Upvotes: 0

Views: 355

Answers (1)

sloth
sloth

Reputation: 101122

collidepoint is a method of the Rect class.

Your TileClass has no collidepoint (that's what the error tells you), but if it has a rect attribute (since this is needed for Sprites), you're code should probably look like:

if(mouseclick[0]):
    for tile in self.engine.level.levellist:
        if tile.rect.collidepoint(mousepos):

But you didn't show your TileClass class, so this is just a guess.

Upvotes: 3

Related Questions