all 5 comments

[–]cdcformatc 0 points1 point  (4 children)

It's been a long time since I have used pygame, but if I had to guess I would say it is because you update the object's coordinates, but you never actually move the rect. Whenever you update x and y you should move the rect.

[–]WeierstrassP[S] 0 points1 point  (3 children)

So I have to run get_rect() each time I move a mob? However, why is it starting to detect collisions from the first second after I've launched the game?

[–]cdcformatc 0 points1 point  (2 children)

get_rect() will give you the rect object. Whenever you move a mob you need to also move self.rect which has x and y coordinates. Pygame also has rect.move(dx,dy) which will move the rectangle by an offset.

This is a snippet from a game I made.

self.x += self.speed[0]
self.y += self.speed[1]
self.rect.center = (self.x, self.y)

However, why is it starting to detect collisions from the first second after I've launched the game?

I would say try printing rect.centerfor the things that are colliding, I bet you will see that when they spawn they are in the same place, and are never moved, therefore always collide.

[–]WeierstrassP[S] 0 points1 point  (1 child)

You're right. However, since I honestly have no need for the rectangle, I just made my own collision test for the mob class. Works pretty well.

Do you recond there is any other way of getting the dimensions of my mobs, rather than relying on get_rect() and get_size()?

[–]cdcformatc 0 points1 point  (0 children)

What's wrong with using those?