-1

I'm making a game in python using pygame and some other libraries. I have the main menu as one python file, and when a player is selected, it starts the game file. If you choose to go back to the menu while playing the game, it starts the menu up again. However from the new menu I can't start/open the game file anymore it doesn't do anything at all. (after each time I open a file I close the previous one)

eg:

Menu-->playerSelect-->gameStartUp-->Menu-->playerSelect-->Break/crash.

so actual code this would be for the first file that is the menu name "FlappyBirdMAIN, "HappyBrid" is the name of the second game file.

if startGui == 2:
    screen.blit(background, [0, 0])
    import HappyBird

    done=True
    pygame.quit()

for the second file "happyBird" i have it open the menu connected to pressing down the "m" key:

            if event.key == K_m:
                pygame.mixer.fadeout(1)
                import FlappybirdMAIN
                done=True

so it import FlappyBirdMAIN then done=True closes the "HappyBird" file

i have figured out that by making copies of the same files("FlappyBirdMAIN", and "HappyBird") then making them import each other in series i can have the menu come up then go down again(more than once) but when i link it back to the origonal file it crash/breaks eg:

EX1:

FlappyBirdMAIN-->HappyBird-->FlappyBirdMAIN2-->HappyBird2-->FlappyBirdMAIN3-->HappyBird3

EX2:

FlappyBirdMAIN-->HappyBird-->FlappyBirdMAIN2-->HappyBird2-->FlappyBirdMAIN3-->HappyBird3-->FlappyBirdMAIN

The problem was using the import command. exec(open("HappyBird.py").read())

using import cuases python to think its is already open, as it is a smart import system. (or something along the lines of that).

Repeating Import Module - Python / Pygame

^the link to how i figured it out.

Thanks.

Community
  • 1
  • 1
Doc
  • 11
  • 3
  • Your current question has enough information if all you're looking for is somewhere to vent (which is not on-topic for SO), but, if you want to fix your program, don't you think you should show some of the code that doesn't work? It's kind of like actually bringing your car to the mechanic if you want him to fix it. – TigerhawkT3 Jan 16 '16 at 06:48
  • wasnt sure where to add code but thanks il do that – Doc Jan 16 '16 at 07:03
  • You might want to [take the Tour](http://stackoverflow.com/tour) and read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask). – TigerhawkT3 Jan 16 '16 at 07:04
  • something like this? – Doc Jan 16 '16 at 07:16
  • figured it out. under this page http://stackoverflow.com/questions/23459281/repeating-import-module-python-pygame?rq=1 – Doc Jan 16 '16 at 07:23
  • If you no longer need an answer, you can delete your question (the link is at the end of the question's content, below its tags). – TigerhawkT3 Jan 16 '16 at 07:38

1 Answers1

0

Using import to start menu is not good solution.

If you have menu.py

print("hello world")

it works only when you import it first time in game.py

import menu

but you can put this in function

def run_it():
    print("hello world")

and then you can use many times

import menu

menu.run_it()

# and again

menu.run_it()

# and again

menu.run_it()

If you use class

class Menu():

   def update():
       pass

   def draw():
       pass

   def event_handler():
       pass

   def mainloop():
       print("hello world")

then you can run it many times too

menu = Menu()

menu.mainloop()

# and again

menu.mainloop()

# and again

menu.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148