Okay so I'm writing a code for a game and the way its currently programmed is the main menu is contained within a large function that references another function, and so on. Not too complicated. But (as you'll see with the code below) I have all the essential variables set before the functions even happen, at the very beginning of the code.
import pygame, sys, time, math
##This is where the variables are set
pygame.init()
DISPLAY = pygame.display.set_mode((1023, 647))
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
clock = pygame.time.Clock()
active = True
started = False
mainScreen = True
coordsX = 0
coordsY = 0
coordsZ = 0
coordsW = 0
def backMain(fps):
##Loads main menu
mainscreendisplayimg = pygame.image.load('mainScreen.png')
def mainscreen(x, y):
DISPLAY.blit(mainscreendisplayimg, (x, y))
mainscreen(1, 1)
while active:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
if started == False:
if mainScreen == True:
started = True
mainScreen = False
if started == True:
forwardMain(15)
started = False
pygame.display.update()
clock.tick(fps)
def ultCoords():
def c0_0_0_0():
pygame.draw.rect(DISPLAY, GREEN, (507, 333, 8, 8), 0)
def forwardMain(fps):
ultCoords()
c0_0_0_0()
backmain(15)
This should display a rectangle on the screen and so on so forth, but instead it gives me an error saying that the variable 'started' is referenced before assignment
This means (I think) that the variable is being used before it's assigned, but it's clearly assigned up at the top, well before any of the functions use it.
Any help is appreciated, thank you!