This is (I assume) a basic question, but I can't seem to figure it out.
Given the following code:
from src.Globals import *
import pygame
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# This is a list of 'sprites.'
block_list = pygame.sprite.Group()
def update_screen():
# Loop until the user clicks the close button.
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Clear the screen
screen.fill(WHITE)
for i in blocks:
block_list.add(block)
block_list.draw(screen)
# Limit to 20 frames per second
clock.tick(20)
# Update the screen with what we've drawn.
pygame.display.flip()
pygame.quit()
Everything works fine. I can call the function update_screen
in a thread and have it work correctly. However, if I move done = False
above the function declaration, then I get the error: UnboundLocalError: local variable 'done' referenced before assignment.
My question is: why is it that I can safely have clock
, and block_list
outside of the function, but not done
?