title: Do global variables become local variables in a Pyglet window.event function
body: <p>Recently while making a games in Pyglet I am facing a problem repeatedly. The game is basically a lane changing game, where you need to change lanes in order to prevent colliding with the chaser, which leads you to loosing the game and the end screen appears which allows you to restart the game.</p>
  <p>I have set a variable <code>loose</code> which stores a boolean data. <code>loose</code> will be <code>True</code> if we have collided with the chaser and <code>False</code>, if we haven't. This variable is initially set to <code>False</code>, but in my collision detection algorithm, (which lies in my update function) it is set to <code>True</code>.</p>
  <pre><code>global loose
  loose = False 
  ...
  def update(dt):
      ...
      if collision:
          loose = True
      ...
  </code></pre>
  <p>Later when I am using the mouse event which detects clicking of the restart button,</p>
  <pre><code>@window.event
  def on_mouse_press(x, y, button, modifiers):
      global loose
      if mouse_clicked:
          if loose:
             ...
             loose = False
             ...
  </code></pre>
  <p>There I am getting the error in the line <code>if loose:</code>, of</p>
  <blockquote>
  <p>UnboundLocalError: local variable 'loose' referenced before assignment</p>
  </blockquote>
  <p>I am not understanding why this is happening and is there anything I can do to fix the error.</p>
  <p><strong>Note:</strong> The code is simplified to keep the question short. If I comment out the instances I refer to <code>loose</code>, the code works perfectly.</p>