I made my first game with python. The structure of program is roughly this: import pygame
class Game:
def __init__(self):
pygame.init()
... rest of the code
def new_game(self):
Game()
...rest of the code
if __name__ == "__main__":
#while True: ###This line was added to original by mistake
Game()
When I finished project I realised that by starting new game it does create new Game
object and starts game from scratch, but it probably still keeps the old games variables, sprites, etc. in memory even though anything isn't happening there anymore.
Is my assumption correct and if so how should I structure my code instead?
EDIT: From what I gathered from comments this would be better structure:
class Game:
def __init__(self):
pygame.init()
def __exit__(self):
#Code here?
... rest of the code
...rest of the code
if __name__ == "__main__":
while True:
game = Game()
game.run()