I am making a pygame game with python 3.8.3, In my game I have a starting screen, that is inside this while loop:
def game_intro():
intro = True
mixer.music.load("musicIntro.mp3")
mixer.music.set_volume(0.02)
mixer.music.play(-1)
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
intro = False
screen.blit(startscherm_img, bordrect)
clock.tick(30)
button("Start",302,517,94,44,donkerOranje,lichtOranje,"start")
button("Music",553,517,94,44,donkerOranje,lichtOranje,"toggleMusic")
button("Quit",803,517,94,44,donkerOranje,lichtOranje,"quit")
pygame.display.flip()
game_intro()
I am calling for my button function that I have defined above:
def button(msg,x,y,w,h,ic,ac,action=None):
mousePos = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mousePos[0] > x and y+h > mousePos[1] > y:
pygame.draw.rect(screen, ac, (x,y,w,h))
if click[0] == 1 and action != None:
if action == "toggleMusic":
PAUSE.toggle()
time.sleep(0.3)
elif action == "quit":
pygame.quit()
time.sleep(0.3)
elif action == "start":
time.sleep(0.3)
intro = False
else:
pygame.draw.rect(screen, ic, (x,y,w,h))
smallText = pygame.font.SysFont("Bauhaus 93",30)
textSurf, textRect = text_objecten(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
screen.blit(textSurf, textRect)
It basically just checks for mouse clicking within a certain area, and if it detects that it executes code.
Now on my starting screen I have a button start, I would like it to end the game_intro() loop, however trying intro = False does not result in anything.
trying break does not work, as it would have to be in the loop itself.
So my question is: How do I make this start button actually end the game_intro() loop ?
P.S I am quite new to Python