1

So I am fairly new to python but I am wondering if there is a way to exit a defined function and return a defined function that comes after the function I am trying to get out of

I have tried to learn some functions that do this but have not found some yet

I am trying to make an options function when I press the key C it returns to a gameloop that comes after

Here is my code I have a problem with -

def options():
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_b:
                    paused = False
                    playing = True
                    return
                if event.key == pygame.K_c:
                    playing = True
                    print("Continue")
                    game()
                if event.key == pygame.K_ESCAPE:
                    return
                
                    

                elif event.key == pygame.K_q:
                    print("Quitting game...")
                    time.sleep(3)
                    pygame.quit()
                    quit()
                elif event.key == pygame.K_f:
                    choices()

             

            screen.fill(white)
            pygame.mixer.music.pause()
            totalText = set_text("Options", 400, 250, 60)
            
            pausecon = controls2("Press C to continue", black)
            b = controlsr("Press B to go back to previous screen",black)
            newpa = controls3("Press F to go back to choices or Press Q to quit",black)
            screen.blit(totalText[0], totalText[1])
            pygame.display.update()
        


        pygame.display.update()

game loop I am trying to go to when user clicks the C key that comes after "def option():"

def game():
    while not paused:
        
        clock = pygame.time.Clock()
        #circles = pygame.sprite.Group(Circle((600, 0), screen))

        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit()
                if event.type == pygame.VIDEORESIZE:
                    print("Resized window")
                    screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)


                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        paused == True
                        playing == False
                        print("Paused")
                        pygame.mixer.music.play(1)

                        pause()
                    if event.key == pygame.K_ESCAPE:
                        paused == True
                        playing == False
                        print("Paused")
                        pause()
                    if event.key == pygame.K_SPACE:
                       print("shot")
                       attacking.play()
                       screen.blit(attack, (x,y) )

                    if event.key == pygame.K_LEFT:
                        x_change = -1
                        y_change = 0
                    if event.key == pygame.K_RIGHT:
                        x_change = 1
                        y_change = 0
                    if event.key == pygame.K_UP:
                        x_change = 0
                        y_change = -1
                    if event.key == pygame.K_DOWN:
                        x_change = 0
                        y_change = 1
                    if event.key == pygame.K_a:
                        x_change = -1
                        y_change = 0
                    if event.key == pygame.K_d:
                        x_change = 1
                        y_change = 0 
                    if event.key == pygame.K_w:
                        x_change = 0
                        y_change = -1
                    if event.key == pygame.K_s:
                        x_change = 0
                        y_change = 1
                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT:
                        x_change = 0
                        y_change = 0
                    if event.key == pygame.K_RIGHT:
                        x_change = 0
                        y_change = 0
                    if event.key == pygame.K_UP:
                        x_change = 0
                        y_change = 0
                    if event.key == pygame.K_DOWN:
                        x_change = 0
                        y_change = 0
                    if event.key == pygame.K_a:
                        x_change = 0
                        y_change = 0
                    if event.key == pygame.K_d:
                        x_change = 0
                        y_change = 0
                    if event.key == pygame.K_w:
                        x_change = 0
                        y_change = 0
                    if event.key == pygame.K_s:
                        x_change = 0
                        y_change = 0
                if x_change >= 800:
                    x_change < 800


        if x >= width or x < 0 or y >= height or y < 0:
            print("Barrier")
            message("Barrier",red)
            pygame.display.update()
        
        x += x_change
        y += y_change


        #makes var music be repeated thru game
        #pygame.mixer.music.play(-1)
        screen.fill((135, 206, 235))

            

        
        
        #pygame.draw.circle(screen, (0, 0, 150), (x, y), r)

        #SPRITES AND CONTROL TEXT IN GAME
        controls("P/Esc = pause",green)
        
        attacksprite = screen.blit(attack, (randx, randy))
        sprite = screen.blit(image, (x, y))
        topRight("Dodged: ", black)


        pygame.display.flip()
        clock.tick(60)
        pygame.display.update()

Error message I get -

game()

NameError: name 'game' is not defined. Did you mean: 'pygame'?

Thank you for your time

Tipsy Dev
  • 13
  • 3

1 Answers1

1

Order of function definition does not matter in python. Your error is not related to it. This simple test demonstrates that.

def test1():
    return test2()
def test2():
    return 1
test1()

What you are looking for is state machines. In your case, you can do something like this:

currentState = OPTIONS
OPTIONS, PLAYING, PAUSE = range(3)

def options(events):
    for event in events:
        if event.key == pygame.K_c:
            playing = True
            currentState = PLAYING

def game(events):
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    playing = not playing
                #maybe add on options to set the state to OPTIONS

        currentState = PLAYING if playing else PAUSE
            
        #logic and stuff
        #clear display
        #drawing code
        #update display



while True:
    events = pygame.event.get()
     if currentState == OPTIONS:
         options(events)
     if currentState == PLAYING:
         game(events)
     if currentState == PAUSED:
         #and so on
     

         

Also, only call pygame.event.get() once like the code above. Only call either pygame.display.update() or pygame.display.flip() once in your entire application.