title: Pygame. Mysterious error. Event referenced before assignment error
body: <p>I am trying to make a python space shooter game with pygame.</p>
  
  <p><strong>The error I am getting is: local variable 'event' referenced before assignment</strong>.</p>
  
  <p>It is mysterious, because <strong>sometimes it appears</strong> and breaks my program, <strong>and sometimes it doesn't</strong>. There is no pattern to the error occurrences, it just happens randomly out of nowhere.</p>
  
  <p>I could run the program 2 times, and it would work fine, but then I run it again, and it gives this error.</p>
  
  <p>The code that I am showing is a very simplified version of the real program, but it still produces the same error.</p>
  
  <pre><code>import pygame
  
  WIDTH = 640
  HEIGHT = 660
  FPS = 30
  
  WHITE = (255,255,255)
  BLACK = (0,0,0)
  RED = (255,0,0)
  BLUE = (0,0,255)
  GREEN = (0,255,0)
  
  pygame.init()
  pygame.mixer.init()
  gamedisplay = pygame.display.set_mode((WIDTH, HEIGHT))
  pygame.display.set_caption("Game!")
  
  clock = pygame.time.Clock()
  
  all_sprites = pygame.sprite.Group()
  
  def quit_game():
      pygame.quit()
  
  def message_to_screen(msg, colour, x, y):#This function is used to display messages to the game display.
      font = pygame.font.SysFont(None, 25)
      text = font.render(msg, True, colour)
      gamedisplay.blit(text, (x,y))
  
  
  def start_screen():    
      global intro_running
      intro_running = True    
  
      while intro_running:
          clock.tick(FPS)
  
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  running = False
                  quit_game()
              if event.type == pygame.KEYDOWN:
                  if event.key == pygame.K_ESCAPE:
                      quit_game()
                  if event.key == pygame.K_a:
                      main_game_loop()
  
          gamedisplay.fill(WHITE)
  
          message_to_screen("start screen: press \"a\" to start", RED, 200, 200)
  
          pygame.display.update()#Updating the display.
  
  
  
  def main_game_loop():
      intro_running = False#I make intro_running equal False here so that when this subprogram starts, the start_screen subprogram will end
  
      running = True
  
      while running:
          clock.tick(FPS)
  
          pygame.mouse.set_visible(0)#Makes the mouse invisible.
  
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  running = False
                  quit_game()
              if event.type == pygame.KEYDOWN:
                  if event.key == pygame.K_ESCAPE:
                      quit_game()
  
          gamedisplay.fill(GREEN)
  
          message_to_screen("Main game will be here", RED, 200, 200)
  
          all_sprites.draw(gamedisplay)
  
          all_sprites.update(event)#The event parameter needs to be passed, because some sprites in the group need it to check for events.
  
          pygame.display.update() 
  
  
  start_screen()
  </code></pre>
  
  <p><strong>The full error is:</strong></p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/Home/Documents/Python/pygame/test game/re-creation.py", line 106, in &lt;module&gt;
      start_screen()
    File "C:/Users/Home/Documents/Python/pygame/test game/re-creation.py", line 64, in &lt;module&gt;
      main_game_loop()
    File "C:/Users/Home/Documents/Python/pygame/test game/re-creation.py", line 98, in &lt;module&gt;
      all_sprites.update(event)#All sprites will be updated, each frame.
  builtins.UnboundLocalError: local variable 'event' referenced before assignment
  </code></pre>
  
  <p>All help will be greatly appreciated.</p>
  
  <p>Thank you.</p>