Cannot get button to toggle a change in pygame
Question: I am wondering why it is that my "GO" button will not toggle the

    
    
    def game_start()
    

permanently, it toggles it while holding the button, but when you let go of
the button its goes back to the main menu?

I am also curious to if there is a way of making the text and buttons vanish
when you press the go button as the game has start?

I am quite new to python so an explanation would be great with any code I have
made a mistake on and or need to add/change.

    
    
    import sys
    import pygame
    from pygame.locals import *
    pygame.init()
    
    size = width, height = 720, 480
    speed = [2, 2]
    
    #Colours
    black = (0,0,0)
    blue = (0,0,255)
    green = (0,200,0)
    red = (200,0,0)
    green_bright = (0,255,0)
    red_bright = (255,0,0)
    
    screen = pygame.display.set_mode(size)
    
    #Pictures
    road = pygame.image.load(r"C:\Users\John\Desktop\Michael\V'Room External\1.png")
    BackgroundPNG = pygame.image.load(r"C:\Users\John\Desktop\Michael\V'Room External\BackgroundPNG.png")
    carImg = pygame.image.load(r"C:\Users\John\Desktop\Michael\V'Room External\Sp1.png").convert_alpha()
    
    
    pygame.display.set_caption("Broom! || BETA::00.0.3")
    
    clock = pygame.time.Clock()
    
    def text_objects(text, font):
        textSurface = font.render(text, True, black)
        return textSurface, textSurface.get_rect()
    
    def game_intro():
        intro = True
    
        while intro:
            for event in pygame.event.get():
                print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
    
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
    
            print(mouse)
            print(click)
    
            screen.fill(blue)
            screen.blit(BackgroundPNG,(0,0))
    
            largeText = pygame.font.Font('freesansbold.ttf',115)
            TextSurf, TextRect = text_objects("V'Room!", largeText)
            TextRect.center = ((width/2),(height/2))
            screen.blit(TextSurf, TextRect)
    
            #Button
            if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, green_bright,(75,400,100,50))
    
                if click != None and click[0] == 1:
                    print("GO == 1 ! == None")
                    x = 350
                    y = 370
                    game_start()
            else:
                pygame.draw.rect(screen, green,(75,400,100,50))
    
            smallText = pygame.font.Font("freesansbold.ttf",20)
            TextSurf, TextRect = text_objects("GO", smallText)
            TextRect.center = ((75+(100/2)),(400+(50/2)))
            screen.blit(TextSurf, TextRect)
    
            if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, red_bright,(550,400,100,50))
    
                if click != None and click[0] == 1:
                    pygame.quit()
                    quit()
            else:
                pygame.draw.rect(screen, red,(550,400,100,50))
    
            TextSurf, TextRect = text_objects("Exit", smallText)
            TextRect.center = ((550+(100/2)),(400+(50/2)))
            screen.blit(TextSurf, TextRect)
    
            pygame.display.flip()
            pygame.display.update()
            clock.tick(15)
    
    def game_start():
       print("Car Loaded Sucessfully")
       screen.blit(road, (0,0))
       screen.blit(carImg, (350,370))
    
    game_intro()
    


Answer: One of solution is to use `game_started` variable instead of function
`game_start()` and use it to decide what to draw - title or car and road, GO
or STOP button, etc.

I use rectangles in place of bitmaps to make full working example.

    
    
    import pygame
    
    # --- constants ----
    
    size = width, height = 720, 480
    speed = [2, 2]
    
    #Colours
    
    black = (0,0,0)
    blue = (0,0,255)
    green = (0,200,0)
    red = (200,0,0)
    green_bright = (0,255,0)
    red_bright = (255,0,0)
    
    # --- functions ---
    
    def text_objects(text, font):
        textSurface = font.render(text, True, black)
        return textSurface, textSurface.get_rect()
    
    
    def game_intro():
    
        largeText = pygame.font.Font('freesansbold.ttf',115)
        smallText = pygame.font.Font("freesansbold.ttf",20)
    
        text_vroom, text_vroom_rect = text_objects("V'Room!", largeText)
        text_vroom_rect.center = ((width/2),(height/2))
    
        text_go, text_go_rect = text_objects("GO", smallText)
        text_go_rect.center = ((75+(100/2)),(400+(50/2)))
    
        text_stop, text_stop_rect = text_objects("STOP", smallText)
        text_stop_rect.center = ((75+(100/2)),(400+(50/2)))
    
        text_exit, text_exit_rect = text_objects("Exit", smallText)
        text_exit_rect.center = ((550+(100/2)),(400+(50/2)))
    
        game_started = False 
    
        intro = True
    
        while intro:
    
            for event in pygame.event.get():
                print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
    
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
    
            screen.fill(blue)
            #screen.blit(BackgroundPNG,(0,0))
    
    
            # road and car - or title
    
            if game_started:
               screen.blit(road, (0,0))
               screen.blit(carImg, (350,370))
            else:
                screen.blit(text_vroom, text_vroom_rect)
    
            # Button GO/STOP
    
            if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, green_bright,(75,400,100,50))
    
                if click != None and click[0] == 1:
                    # toggle True/False
                    game_started = not game_started
            else:
                pygame.draw.rect(screen, green,(75,400,100,50))
    
            # draw GO or STOP
            if not game_started:
                screen.blit(text_go, text_go_rect)
            else:
                screen.blit(text_stop, text_stop_rect)
    
            # Button EXIT
    
            if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, red_bright,(550,400,100,50))
    
                if click != None and click[0] == 1:
                    pygame.quit()
                    quit()
            else:
                pygame.draw.rect(screen, red,(550,400,100,50))
    
            screen.blit(text_exit, text_exit_rect)
    
            pygame.display.flip()
    
            clock.tick(15)
    
    # --- main ---
    
    pygame.init()
    
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Broom! || BETA::00.0.3")
    
    #Pictures
    road = pygame.surface.Surface( size )
    road.fill(black)
    
    carImg =  pygame.surface.Surface( (10,10) )
    road.fill(green)
    
    clock = pygame.time.Clock()
    
    game_intro()
    

**EDIT:** second solution is to create function (`game_running`) with own
`while` loop, own buttons, etc.

I had to use `pygame.time.wait()` because `pygame.mouse.get_pressed()` is not
good function for single click on button. Computer (and `while` loop) is too
fast (for human click) and `pygame.mouse.get_pressed()` toggle button many
times. Better to use `pygame.event.get()` for single click.

    
    
    import pygame
    
    # --- constants ----
    
    size = width, height = 720, 480
    speed = [2, 2]
    
    #Colours
    
    black = (0,0,0)
    blue = (0,0,255)
    green = (0,200,0)
    red = (200,0,0)
    green_bright = (0,255,0)
    red_bright = (255,0,0)
    
    # --- functions ---
    
    def text_objects(text, font):
        textSurface = font.render(text, True, black)
        return textSurface, textSurface.get_rect()
    
    
    def game_intro():
    
        text_vroom, text_vroom_rect = text_objects("V'Room!", largeText)
        text_vroom_rect.center = ((width/2),(height/2))
    
        text_go, text_go_rect = text_objects("GO", smallText)
        text_go_rect.center = ((75+(100/2)),(400+(50/2)))
    
        text_exit, text_exit_rect = text_objects("Exit", smallText)
        text_exit_rect.center = ((550+(100/2)),(400+(50/2)))
    
        running = True
    
        while running:
    
            for event in pygame.event.get():
                print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
    
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
    
            screen.fill(blue)
            screen.blit(text_vroom, text_vroom_rect)
    
            # Button GO
    
            if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, green_bright,(75,400,100,50))
    
                if click != None and click[0] == 1:
                    # wait because `pygame.mouse.get_pressed()` is too fast for human clik
                    pygame.time.wait(100)
                    # run game
                    game_running()
            else:
                pygame.draw.rect(screen, green,(75,400,100,50))
    
            screen.blit(text_go, text_go_rect)
    
            # Button EXIT
    
            if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, red_bright,(550,400,100,50))
    
                if click != None and click[0] == 1:
                    pygame.quit()
                    quit()
            else:
                pygame.draw.rect(screen, red,(550,400,100,50))
    
            screen.blit(text_exit, text_exit_rect)
    
            pygame.display.flip()
    
            clock.tick(15)
    
    def game_running():
    
        text_stop, text_stop_rect = text_objects("STOP", smallText)
        text_stop_rect.center = ((75+(100/2)),(400+(50/2)))
    
        text_exit, text_exit_rect = text_objects("Exit", smallText)
        text_exit_rect.center = ((550+(100/2)),(400+(50/2)))
    
        running = True
    
        while running:
    
            for event in pygame.event.get():
                print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
    
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
    
            screen.fill(blue)
            #screen.blit(BackgroundPNG,(0,0))
    
    
            # road and car - or title
    
            screen.blit(road, (0,0))
            screen.blit(carImg, (350,370))
    
            # Button STOP
    
            if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, green_bright,(75,400,100,50))
    
                if click != None and click[0] == 1:
                    # return to menu
                    return
            else:
                pygame.draw.rect(screen, green,(75,400,100,50))
    
            # draw STOP
            screen.blit(text_stop, text_stop_rect)
    
            # Button EXIT
    
            if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
                pygame.draw.rect(screen, red_bright,(550,400,100,50))
    
                if click != None and click[0] == 1:
                    pygame.quit()
                    quit()
            else:
                pygame.draw.rect(screen, red,(550,400,100,50))
    
            screen.blit(text_exit, text_exit_rect)
    
            pygame.display.flip()
    
            clock.tick(15)
    
    
    # --- main ---
    
    pygame.init()
    
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Broom! || BETA::00.0.3")
    
    # pictures
    road = pygame.surface.Surface( size )
    road.fill(black)
    
    carImg =  pygame.surface.Surface( (10,10) )
    road.fill(green)
    
    # fonts
    largeText = pygame.font.Font('freesansbold.ttf',115)
    smallText = pygame.font.Font("freesansbold.ttf",20)
    
    # others
    
    clock = pygame.time.Clock()
    
    game_intro()
    

