'an error in pygame :D (line 86, in main_menu screen.fill(Black) UnboundLocalError: local variable 'screen' referenced before assignment) [duplicate]

import pygame, sys from BUT import Button from Data import * pygame.init() Clock = pygame.time.Clock()

pygame.display.set_caption("Menu") monitor_size = [pygame.display.Info().current_w, pygame.display.Info().current_h] BG = pygame.image.load("Background.png") screen = pygame.display.set_mode((1280, 720),pygame.RESIZABLE)

def play(): while True: PLAY_MOUSE_POS = pygame.mouse.get_pos()

    screen.fill("black")

    PLAY_TEXT = get_font(45).render("This is the PLAY screen.", True, "White")
    PLAY_RECT = PLAY_TEXT.get_rect(center=(640, 260))
    SCREEN.blit(PLAY_TEXT, PLAY_RECT)

    PLAY_BACK = Button(image=None, pos=(640, 460), 
                        text_input="BACK", font=get_font(75), base_color="White", hovering_color="Green")

    PLAY_BACK.changeColor(PLAY_MOUSE_POS)
    PLAY_BACK.update(screen)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if PLAY_BACK.checkForInput(PLAY_MOUSE_POS):
                main_menu()
        if event.type == KEYDOWN:
            if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode(monitor_size, pygame.FULLSCREEN)
                else:
                    screen = pygame.display.set_mode((screen.get_width(), screen.get_height()), pygame.RESIZABLE)

    pygame.display.update()
    Clock.tick(FPS)

def options(): while True: OPTIONS_MOUSE_POS = pygame.mouse.get_pos()

    screen.fill("white")

    OPTIONS_TEXT = get_font(45).render("This is the OPTIONS screen.", True, "Black")
    OPTIONS_RECT = OPTIONS_TEXT.get_rect(center=(640, 260))
    SCREEN.blit(OPTIONS_TEXT, OPTIONS_RECT)

    OPTIONS_BACK = Button(image=None, pos=(640, 460), 
                        text_input="BACK", font=get_font(75), base_color="Black", hovering_color="Green")

    OPTIONS_BACK.changeColor(OPTIONS_MOUSE_POS)
    OPTIONS_BACK.update(screen)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if OPTIONS_BACK.checkForInput(OPTIONS_MOUSE_POS):
                main_menu()
        if event.type == KEYDOWN:
            if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode(monitor_size, pygame.FULLSCREEN)
                else:
                    screen = pygame.display.set_mode((screen.get_width(), screen.get_height()), pygame.RESIZABLE)


    pygame.display.update()
    Clock.tick(FPS)

def main_menu(): while True:

    MENU_MOUSE_POS = pygame.mouse.get_pos()
    screen.fill(Black)
    MENU_TEXT = get_font(100).render("MAIN MENU", True, "#b68f40")
    MENU_RECT = MENU_TEXT.get_rect(center=(640, 100))

    PLAY_BUTTON = Button(image=pygame.image.load("Play Rect.png"), pos=(640, 250), 
                        text_input="PLAY", font=get_font(75), base_color="#d7fcd4", hovering_color="White")
    OPTIONS_BUTTON = Button(image=pygame.image.load("Options Rect.png"), pos=(640, 400), 
                        text_input="OPTIONS", font=get_font(75), base_color="#d7fcd4", hovering_color="White")
    QUIT_BUTTON = Button(image=pygame.image.load("Quit Rect.png"), pos=(640, 550), 
                        text_input="QUIT", font=get_font(75), base_color="#d7fcd4", hovering_color="White")

    screen.blit(MENU_TEXT, MENU_RECT)

    for button in [PLAY_BUTTON, OPTIONS_BUTTON, QUIT_BUTTON]:
        button.changeColor(MENU_MOUSE_POS)
        button.update(screen)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if PLAY_BUTTON.checkForInput(MENU_MOUSE_POS):
                play()
            if OPTIONS_BUTTON.checkForInput(MENU_MOUSE_POS):
                options()
            if QUIT_BUTTON.checkForInput(MENU_MOUSE_POS):
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_F11:
                    fullscreen = not fullscreen
                    if fullscreen:
                        screen = pygame.display.set_mode(monitor_size, pygame.FULLSCREEN)
                    else:
                        screen = pygame.display.set_mode((screen.get_width(), screen.get_height()), pygame.RESIZABLE)


    pygame.display.update()
    Clock.tick(FPS)

main_menu()



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source