Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking for collision against a large group of sprites all belonging to the same class?
    text
    copied!<p>I'm in the process of writing a small little survival game. Wave by wave more enemies come and it is your job to mow them down before they do likewise to you.</p> <p>The problem I'm having is with the collision detection. I have an 'Enemy' class and I want to make sure that none of the enemies can walk through each other. Pygame's collision facilities don't seem to accomodate what I need. The function in particular is the 'no_merge' function within the enemy class. At the moment, it is blank but I have tried all the different variants of collision detection that I could find in the pygame docs, but I can't seem to find any that test for each sprite belonging to the same group.</p> <p>I know why the no_merge() function in my code doesn't work, it's constantly checking for collision against itself which will always remain true, I just can't seem to find (or think) of the correct way.</p> <p>Some extra info: It will be a large number of sprites with the smalles number being around 20 and the largest at roughly 100.</p> <p>Here is the function as it stands right now:</p> <pre><code> def no_merge(self): """Makes sure that the enemies cannot walk through each other""" if pygame.sprite.spritecollide(self,enemy_list,False) and enemy != self: do collision stuff. </code></pre> <p>This is inserted in the main loop like so:</p> <pre><code>while done == False (this line hasn't indented properly on stack overflow) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True for bullet in bullet_list: bullet.rect.x +=4 for enemy in enemy_list: enemy.chase() enemy.in_screen enemy.destroy() enemy.no_merge() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload