0

I am making a simple game in pygame, with a structure something like this:

def mainmenu():
    ...

def gameloop()
    ...

[initialising Pygame, making the window, etc]

mainmenu()

gameloop()

This is the problem. Now in the function gameloop() there is a variable 'MOUSE_X' which is set to the X position of the mouse.

When I only run just the mainmenu() function or just the gameloop() function, the program works fine.

However, when I run the two functions in succession, I get the error:

**UnboundLocalError: local variable 'MOUSE_X' referenced before assignment**

The local variable 'MOUSE_X' only exists in the gameloop() function and is not referenced outside of that function. I've gone through the program and confirmed this multiple times.

Why am I getting this error?

skrx
  • 19,980
  • 5
  • 34
  • 48
  • 1
    Can you please format your code? – Christian Dean Mar 07 '18 at 02:34
  • 2
    Please add in a small reproducible code example. It's very difficult to debug without seeing even parts of your `mainmenu` and `gameloop` functions. – sam Mar 07 '18 at 02:34
  • Don't worry, no one is going to steal your code. Share it and there might be a chance that you'll get some help, otherwise it's impossible. – ChatterOne Mar 07 '18 at 08:59
  • I fixed the issue anyway: I was defining the variable inside an 'if' loop and referenced it ouside the loop. Silly me! – KingSpikey Mar 08 '18 at 09:09

0 Answers0