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.