1

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()
Heikki
  • 341
  • 2
  • 18
  • Since you have a while loop, why do you need to do that? Just exit `__init__`, then the old `Game` object will be reclaimed and a new one will be created when the loop comes around again. That's not the way I would have written it, but I suppose there's nothing fundamentally wrong. I would have used something like `game = Game()` / `game.run()` . The `__init__` function is really supposed to be for initialization, not for execution. But that's a philosophical issue. – Tim Roberts Dec 14 '21 at 18:58
  • 3
    I would expect the garbage collector to handle this. You don't store Game anywhere when it's created, so as soon as the `__init__` returns, there will be no non-circular references to anything within it and the gc will clear it up. – defladamouse Dec 14 '21 at 19:00
  • I'm with @TimRoberts above. You really should separate game initialization from game execution. But the garbage collector will take care of unused objects in either case. – Frank Yellin Dec 14 '21 at 19:03
  • There is an indentation problem, below the `while` statment and it is not clear where you call `new_game()`. That latter method should really be a class method and not an instance method. If the constructor only completes when the game is over, then this should not need a `Game` class at all. Just make it a simple function. On the other hand, it would be better to split your game logic into different functions, potentially methods of an instance. – trincot Dec 14 '21 at 19:03
  • The run method should return whether the game should continue or not. So You can do `run_game = True` `while run_game:` `game = Game()` `run_game = game.run()`. When the game ends the method `run ()` must return `True` if a new game is to be started, otherwise it must return` False`. – Rabbid76 Dec 14 '21 at 19:57
  • Thanks all for answers. Espesially @Tim Roberts. Exit `__init___` is new consept for me. Does it need any code inside it or simply `pass`? I made adjustemnts to code (above) to my best understanding of your answers. Please correct me if I had the wrong idea. Also to @trincot: that while loop was mistake. I experimented with it before asking and it was left behind. – Heikki Dec 14 '21 at 20:01

1 Answers1

1

I suggest a different approach The run method should return whether the game should continue or not. So You can do:

if __name__ == "__main__":
    run_game = True
    while run_game:
        game = Game()
        run_game = game.run()

When the game ends the method run () must return True if a new game is to be started, otherwise it must return False.

class Game:

    def __init__(self):
        # [...]

    def run(self):

        # application loop
        running = True
        while running:
            # [...]

        # ask to restart game 
        start_new_game = # [...] set True or False 

        return start_new_game
Rabbid76
  • 202,892
  • 27
  • 131
  • 174