[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Fwd: Collision Detection



> Ok all, I've come to the part of my breakout game where I need to write the
> collision detection code.
> 
> #update the balls.
> sprite_rects = []
> for x in self.vis_sprites:
>     sprite_rects.append(x.rect)
> for ball in self.balls:
>     ball.Move()
>     for target in ball.rect.collidelistall(sprite_rects):
> self.vis_sprites[target].Ball_hit()
> ball.Bounce()
> del sprite_rects

hi chris. well the easiest method might be to check that
the current target from collidelistall() is not the ball.

    for target in ball.rect.collidelistall(sprite_rects):
        if self.vis_sprites[target] is ball: continue  #FIX?
        ball.Bounce()

the "is" comparison isn't really an equality comparison, but
a nice quick check to see if both variables are referencing
the same object. if you plan on more than one ball being active
and you don't want any balls to collide with each other, you can
do another simple test...
    for target in ball.rect.collidelistall(sprite_rects):
        if self.vis_sprites[target] in self.balls: continue #FIX?
        ball.Bounce()


also, i don't know if you disagree with the new python list
comprehensions, but i love them. your first several lines
to create the rect list can be reduced to...

sprite_rects = [x.rect for x in self.vis_sprites]

you may find this code more readable or less readable, but it
is an option. unfortunately, the testing i've heard of says this
doesn't run a whole lot quicker than your current version.



btw, your original post failed to make it to the list. remember
only subscribed email addresses can post to the list, so make sure
you're using the right account when posting to the list. :]


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org