I made a game but I have two screens to display, the menu page and the game page. On the menu page when i click on a play button, it shows the game without any problem but when i close the running game, it goes back to the menu screen but i want both screens to exit.
Please any help would be greatly appreciated.
sample code:
menu screen
def menu():
running = True
while running:
screen.blit(intros, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
GUI.button("Play game", 24,300, 128,50, violet, blue, screen, MAIN)
necessary things here!
def MAIN():
running = True
while running:
screen.blit(intros, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all action starts here
please can anyone give me a suggestion as to how to run the menu game so that when the play button is clicked and the MAIN code is running, when i click close, the game stops and doesn't return to the menu code.
I've read on several forums on using if __name__ = "__main__"
but i find it useless maybe perhaps i don't know how to implement it correctly
This is happening because you have two while-loops. Setting
running = False
exits only the current while-loop, so you leaveMAIN
only to end up back atmenu
which you called it from.If you want setting
running = False
to exit out of both loops, make it a global variable (and have only one of it).You could also use
import sys
andsys.exit()
if you want to completely quit the program. But I recommend the first one.