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)?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 Why Is My Collision Test Always Returning 'True' And Why Is The Position Of The Rectangle Of
01:12 Accepted Answer Score 4
02:26 Thank you

--

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

--

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)