[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Sprites false positive collision - newbie alert



On lun, 2014-05-05 at 09:19 +0100, AdamC wrote:
> self.rect = self.image.get_rect()

You assign that rect, but then use completely separate x and y
attributes for the position. You then use the rect for collision, which
because you haven't updated it, is still at position (0,0). Therefore
all the rects are colliding. Use self.rect.[x|y|topleft] for the
position and remove your self.[x|y] attributes.

It looks like you've used this tutorial to get started:
http://programarcadegames.com/index.php?chapter=introduction_to_sprites

If you look at the end of 14.1, you'll notice the line:
        all_sprites_list.draw(screen)
You should use this in your code, and get rid of the drawing code you've
currently done, including the draw() method in the classes.


On a side note, (in my opinion) that tutorial teaches some bad style
habits that you've started to pick up. For example, if a variable is
called all_sprites_list, I expect that variable to represent a list, and
when you call it's add() method, I expect your code to fail because
that's not a list method. It would be better to call it
all_sprites_group or simply all_sprites.

When you're more comfortable with python/pygame, if you're interested in
improving the readability of you code I would recommend reading through
PEP8, and also a book called "The Art of Readable Code".

Attachment: signature.asc
Description: This is a digitally signed message part