0

I want to make a collision module inside a class, but when I call it with two sprites that have a rect defined with them. Here is the code that contains it

Calling the module:

elif playerCol.checkCollision(playerrect, waterrect):
    if True:
        print('True')

Class and Module:

class collision():
    def checkCollision(self, sprite1, sprite2):
        col = pygame.sprite.collide_rect(sprite1, sprite2)
        if col == True:
            return True

Sprites:

player = pygame.image.load("Textures(Final)\player.bmp").convert()
playerrect = player.get_rect()
player.set_colorkey(WHITE)

grasstile = pygame.image.load("Textures(Final)\Grass_Tile.bmp").convert()
watertile = pygame.image.load("Textures(Final)\Water_Tile.bmp").convert()
waterbeach = pygame.image.load("Textures(Final)\Water_Beach.bmp").convert()
grassrect = grasstile.get_rect()
waterrect = watertile.get_rect()
waterb = waterbeach.get_rect()

The full error is:

Traceback (most recent call last):
  File "F:\Desktop Files\Python\Pygame RPG Type Game\RPG TEST 2.py", line 153, in <module>
    main()
  File "F:\Desktop Files\Python\Pygame RPG Type Game\RPG TEST 2.py", line 102, in main
    elif playerCol.checkCollision(playerrect, waterrect):
  File "F:\Desktop Files\Python\Pygame RPG Type Game\RPG TEST 2.py", line 147, in checkCollision
    col = pygame.sprite.collide_rect(sprite1, sprite2)
  File "F:\Python33\lib\site-packages\pygame\sprite.py", line 1300, in collide_rect
    return left.rect.colliderect(right.rect)
AttributeError: 'pygame.Rect' object has no attribute 'rect'

I hope that is all the code you need to help me identify my problem. I have spent 3 hours on this problem, and unfortunately couldn't find myself a solution. Thank you for your help!

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
TCVM
  • 59
  • 1
  • 9
  • 1
    But `playerrect` and `waterrect` are **not sprites**! `pygame.sprite.collide_rect()` takes sprites, not rectangles; `player.get_rect()` returns such a rectangle, and you cannot pass that to `pygame.sprite.collide_rect()` and have it work. – Martijn Pieters Apr 21 '14 at 07:37
  • 1
    Why not use actual sprites instead of just images? – Martijn Pieters Apr 21 '14 at 07:40

0 Answers0