The Python Oracle

Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles

--

Chapters
00:00 Question
01:41 Accepted answer (Score 4)
03:37 Thank you

--

Full question
https://stackoverflow.com/questions/5773...

Accepted answer links:
[pygame.Surface.get_rect.get_rect()]: https://www.pygame.org/docs/ref/surface....
[pygame.Rect]: https://www.pygame.org/docs/ref/rect.htm...
[pygame.sprite.Group]: https://www.pygame.org/docs/ref/sprite.h...
[.draw()]: https://www.pygame.org/docs/ref/sprite.h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pygame #collisiondetection #pygamesurface

#avk47



ACCEPTED ANSWER

Score 4


pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position on the display with the blit function.

You've to set the location of the rectangle, either by a keyword argument, e.g:

self.rect = self.image.get_rect(topleft = (self.x, self.y))

or an assignment to a virtual attribute (see pygame.Rect), e.g:

self.rect = self.image.get_rect()
self.rect.topleft = (self.x, self.y)

It is absolutely unnecessary to add some extra attributes self.x and self.y. Use the location of the rectangle instead. e.g:

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("ball.png")
        self.rect = self.image.get_rect(topleft = (280, 475))
        self.col = False
    def update(self):
        gameDisplay.blit(self.image, self.rect)
    def test_collisions(self,sprite):
        self.col = pygame.sprite.collide_rect(self,sprite)

class Obstacle(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("obstacle.png")
        self.time = pygame.time.get_ticks()
        self.rect = self.image.get_rect(topleft = (1000, 483))
    def change_x(self):
        self.time = pygame.time.get_ticks()
        self.rect.x = -(self.time/5) + 800
    def update(self):
        gameDisplay.blit(self.image, self.rect)

Further note, that you can get rid of the methods Ball.update() respectively Obstacle.update() (you can delete them), if you use a pygame.sprite.Group and call .draw(), which uses the .image and .rect properties of the contained sprites, to draw them. e.g.:

obstacle = Obstacle()
ball = Ball()      
all_sprites = pygame.sprite.Group([obstacle, ball])

while not crashed:

    # [...]
  
    gameDisplay.fill((255,255,255))
    
    all_sprites.draw(gameDisplay)
    
    pygame.display.flip()
    clock.tick(1000)