I am building a small Space Invaders type game. Or at least trying to. Most things work, however, I’m having a bad recurring bug. The game works perfectly sometimes. However, about every other time or so, it exits out after I’ve exploded a few enemies and gives the following errors:
Traceback (most recent call last): enemies[count].render() and pygame.error:display Surface quit. The second error seems to pop up always, even when the program seems to funciton fine.
This is my render function:
def render(self):
screen.blit(self.bitmap, (self.x, self.y))
I can post and/or link to the full code if needed.
From what I can guess, you are probably getting references to surfaces that have already been garbage collected and pygame is quitting when it cannot blit the surface.
To fix this, go through your code and make sure that you are deleting objects from your ‘enemies’ list when they are removed from the game.
Another solution would be to use PyGame’s built in Group class. It will track deleted objects for you and you won’t need to worry about problems like this one.