I am trying to understand why I get UnboundLocalError in my pygame application, Table Wars. Here is a brief description of what is happening:
The variables REDGOLD
, REDCOMMAND
, BLUEGOLD
and BLUECOMMAND
initialized as global variables:
#Red Stat Section REDGOLD = 50 REDCOMMAND = 100
This works when units are unrestored in the main cycle, subtracting the means for the appearance of units.
I'm currently trying to set up the system so that when the unit dies, the killer returns the COMMAND
victim and earns GOLD
based on the fact that he killed:
class Red_Infantry(pygame.sprite.Sprite): def __init__(self, screen): [...] self.reward = 15 self.cmdback = 5 [...] def attack(self): if self.target is None: return if self.target.health <= 0: REDGOLD += self.target.reward
This works until the device dies. Then this happens:
Traceback (most recent call last): File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 606, in <module> main() File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 123, in main RedTeam.update() File "C:\Python27\lib\site-packages\pygame\sprite.py", line 399, in update for s in self.sprites(): s.update(*args) File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 304, in update self.attack() File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 320, in attack REDGOLD += self.target.reward UnboundLocalError: local variable 'REDGOLD' referenced before assignment
How to get global variables mentioned above using attack
block? If this helps, I am using Pygame 2.7.x, so nonlocal
will not work: /