PDA

View Full Version : [SOLVED] Pygame Circular Collision



Penguin Guy
March 1st, 2010, 10:53 PM
SOLVED: Turns out the rect was set wrong - I'd set it using rect = (x, y, w, h) rather than rect = pygame.Rect(x, y, w, h).

----------------------------------------

I've been trying to detect if a object 'player' is in contact with any opponents in Pygame. I'm using the function pygame.sprite.collide_circle() - although obviously incorrectly. I couldn't find much documentation on it, and what documentation I found, I didn't understand. I'd be grateful if somebody could give me a pointer.

Here's the relevant code (full package attached):
[PYTHON]


# Update all opponents
for opponent in opponents:
opponent.update(time_passed, player.size)
if pygame.sprite.collide_circle(player, opponent): # If opponent is in contact with player:
print 'Contest:', opponent.size, player.size
if opponent.size > player.size: # Biggest contestant eats the other
opponent.eat(player)
game_over()
if opponent.size < player.size:
player.eat(opponent)


The output I get is:


Contest: 75 78
Contest: 65 78
Contest: 65 78
Contest: 75 78
Contest: 78 78
Contest: 53 78
Contest: 75 78
Contest: 65 78
Contest: 65 78
Contest: 75 78
Contest: 78 78
Contest: 53 78
...

(a.k.a. recurring output of the highlighted text)

I'm 99% sure that pygame.sprite.collide_circle(player, opponent) is returning True regardless of the positions of player and opponent.

Tony Flury
March 1st, 2010, 11:03 PM
Obvious question - did you set the rect and/or radius attributes correctly on the sprites ?

Penguin Guy
March 2nd, 2010, 08:42 PM
Obvious question - did you set the rect and/or radius attributes correctly on the sprites ?
Ahh, it seems I've set rect incorrectly - thanks!