I starting to code in python and using pyzero to make a simple game. After the end of the game, I want to delete all the existing instances of certain types of classes, to allow the game to start again. I have a list of all the instances of that class, but using remove(self) seems to cause a problem in the logic that I can't work out.
class Ball(Actor):
ball_list = []
def __init__(self, actor):
Actor.initiate_actor(self,"ball")
Ball.ball_list.append(self)
self.alive = True
def kill(self):
if self.alive:
self.alive = False
Ball.ball_list.remove(self)
def new_game():
global game_over, score
for actor in Ball.ball_list:
actor.kill()
score = 0
game_over = False
def draw():
global game_over
if game_over:
screen.clear()
screen.draw.text("Game Over", center = (WIDTH/2, HEIGHT/2), color = 'white')
else:
screen.clear()
backdrop.draw()
for actor in Ball.ball_list:
if actor.alive:
actor.draw()