-2

I am currently making a 2D game where there are different guns available to the player, each of which have different firing rates. When I tried to implement the different firing rates for each gun, I got this error:

if currentTime - bulletGap >= 2 and bulletType == "pistol": # 2 seconds between bullets
UnboundLocalError: local variable 'bulletGap' referenced before assignment

I have defined "bulletGap = 0" and "gunFire = [True, True, True]", both above the function they are being used in, however I am still getting the error. The rest of the (not working) code is below.

def __init__(self, x, y, velx, vely, direction, bulletType):

    currentTime = pygame.time.Clock()

    if currentTime - bulletGap >= 2 and bulletType == "pistol": # 2 seconds between bullets
        bulletGap = pygame.time.Clock()
        gunFire[1] = True
        return

    elif currentTime - bulletGap >= 4 and bulletType == "shotgun": # 4 seconds between bullets
        bulletGap = pygame.time.Clock()
        gunFire[2] = True
        return

    elif currentTime - bulletGap >= 0.5 and bulletType == "automatic": # 0.5 seconds between bullets
        bulletGap = pygame.time.Clock()
        gunFire[3] = True
        return

    self.type = bulletType
    self.direction = direction
    self.velx, self.vely = velx, vely

    for n in range(gunFire):

        if gunFire[n] == True:

            if direction == "north":

                south = pygame.transform.rotate(Bullet.bulletImage[bulletType], 90)
                self.image = pygame.transform.flip(south, False, True)

            elif direction == "east":

                self.image = pygame.transform.flip(Bullet.bulletImage[bulletType], True, False)

            elif direction == "south":

                self.image = pygame.transform.rotate(Bullet.bulletImage[bulletType], 90)

            elif direction == "west":

                self.image = Bullet.bulletImage[bulletType]

            pygame.Rect.__init__(self, x, y, Bullet.width, Bullet.height)

            Bullet.bulletList.append(self)
            break

Also (as a side note), do I need still need "return" after the if statements? They were there from some previous code I wrote, and I tried removing them but I still got the same error.

  • 1
    possible duplicate of [Python 3: UnboundLocalError: local variable referenced before assignment](http://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment) – vaultah Aug 04 '14 at 16:54
  • Please reduce your code to the shortest, **complete** program that demonstrates the problem. Copy-paste that short program into your question. See http://stackoverflow.com/help/mcve for more info. – Robᵩ Aug 04 '14 at 16:56
  • Specifically, we cannot know from your example whether `__init__` is a module-level function or a class-level method, nor can we know whether the variable "bulletGap" was defined at module or class level. – Robᵩ Aug 04 '14 at 16:59
  • I edited my question so that it includes the rest of the code. It is slightly longer now, as I have previously tried not to copy and paste too much code. Is that all that was needed, or is the whole class needed? – Monkeymad358 Aug 04 '14 at 17:00
  • Your example is simultaneously too long and too short. It is too long, in that contains code that is completely unrelated to your question. It is too short in that it is incomplete -- essential bits are missing. Please modify your program to be the **shortest** possible **complete** program. Both http://stackoverflow.com/help/mcve and http://SSCCE.org have great descriptions of how to create helpful example programs. – Robᵩ Aug 04 '14 at 17:02

1 Answers1

0

Assuming that your code looks (roughly) like this:

class C:
  bulletGap=0
  def method(self):
    print(bulletGap)  # or any sort of reference
    bulletGap = 1     # or any sort of assignment

c=C()
c.method()

Then you'll need to use self. to correctly reference bulletGap:

class C:
  bulletGap=0
  def method(self):
    print(self.bulletGap)
    self.bulletGap = 1  # If bulletGap is instance-specific
    C.builletGap = 1    # If bulletGap is class-specific

c=C()
c.method()
Robᵩ
  • 163,533
  • 20
  • 239
  • 308