0
\$\begingroup\$

In my code, I wanted to use the spritecollides method but I got the following error message:

Traceback (most recent call last):

File "/Users/lanik2009/Documents/pygame testing.py", line 76, in <module>
hits = pygame.sprite.spritecollide(player, obstacles, False)

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pygame/sprite.py", line 1645, in spritecollide
default_sprite_collide_func = sprite.rect.colliderect

AttributeError: 'pygame.Rect' object has no attribute 'rect'

My code:

import pygame

pygame.init()
  
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
black = (0, 0, 0)
red = (255, 0, 0)

w = 500
h = 500

x = 15
y = 480

vel = 5
screen = pygame.display.set_mode((500, 500))

square = pygame.image.load(r'/Users/lanik2009/Downloads/square.PNG')
square = pygame.transform.scale(square, (46, 40))
triangle = pygame.image.load(r'/Users/lanik2009/Downloads/triangle.PNG')
triangle = pygame.transform.scale(triangle, (46, 40))

squareSprite = pygame.sprite.Sprite()
squareSprite.image = square
squareSprite.rect = square.get_rect()

triangleSprite = pygame.sprite.Sprite()
triangleSprite.image = triangle
triangleSprite.rect = triangle.get_rect()

def drawer():
    screen.fill(black)
    screen.blit(triangle, (100, 460))
    screen.blit(square, (300, 460))
    
jump = False

obstacles = pygame.sprite.Group()

obstacles.add(squareSprite)
obstacles.add(triangleSprite)

running = True

while running:
      
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT]:
        x -= vel

    if keys[pygame.K_RIGHT]:
        x += vel

    if jump == False:
        y = 480
        
    if keys[pygame.K_UP]:
        y -= 40
        jump = False
        
    if event.type == pygame.KEYUP:
        jump = False
        
    drawer()
    
    player = pygame.draw.circle(screen, white, (x, y), 10, 500)
    pygame.display.update()

    hits = pygame.sprite.spritecollide(player, obstacles, False)
    if hits:
        running = False

pygame.quit()
\$\endgroup\$
0
0
\$\begingroup\$

As in your previous question, your error message tells you exactly what is wrong.

AttributeError: 'pygame.Rect' object has no attribute 'rect'

This means that pygame expected you to give it an object that has an attribute called "rect". Instead, you gave it a pygame.Rect. pygame.Rect, naturally, does not have an attribute called "rect". It is a Rect, it doesn't have a Rect.

Where did this Rect come from? If you read the documentation for pygame.draw.circle, you'll see it says:

Returns:
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the center parameter value (float values will be truncated) and its width and height will be 0

So this line:

player = pygame.draw.circle(screen, white, (x, y), 10, 500)

...sets player to be a Rect object.

The spritecollide function expects you to pass it a Sprite and a SpriteGroup. You passed it a Rect and a SpriteGroup.

You can draw your player as a sprite instead of a circle if you want to use the spritecollide function, or you can use a different collision checking function to test a Rect against members of a SpriteGroup.

\$\endgroup\$
1
  • \$\begingroup\$ For context: I have never written a single line of Python code in my life. This doesn't take any special knowledge of Python to understand. You just need to read your error messages and read the documentation. Please get in the habit of doing so, and you'll find you're able to solve simple issues like this on your own, much faster than posting here. \$\endgroup\$ – DMGregory May 18 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.