0
import pygame
pygame.init()

win = pygame.display.set_mode((1280,800))
pygame.display.set_caption("Fight")

appwallpaper = pygame.image.load('/Users/arnav/Downloads/AppWallpaper.jpeg') #setting the apps wallpaper storing it in a variable

pygame.display.set_icon(appwallpaper) #setting the apps wallpaper as the poster

walkLeft = [pygame.image.load('/Users/arnav/Downloads/Jokerattack1.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack2.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack3.png')]
walkRight = [pygame.image.load('/Users/arnav/Downloads/Jokerattack1Right.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack2Right.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack3Right.png')]

walkLeft2 = [pygame.image.load('/Users/arnav/Downloads/Jokerattack1.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack2.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack3.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack4.png')]
walkRight2 = [pygame.image.load('/Users/arnav/Downloads/Jokerattack1Right.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack2Right.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack3Right.png'),     pygame.image.load('/Users/arnav/Downloads/Jokerattack4Right.png')] 

Joker1 = pygame.image.load('/Users/arnav/Downloads/Joker.png')
char = pygame.transform.scale(Joker1, (100, 165)) 
bg1 = pygame.image.load('/Users/arnav/Downloads/fighterbackground.png')
bg = pygame.transform.scale(bg1, (1280, 800))
x = 50
y = 550
width = 40
height = 60
vel = 5

clock = pygame.time.Clock()

isJump = False
jumpCount = 10

left = False
right = False
space = False
walkCount = 0

currentWalk = walkLeft

def redrawGameWindow():
    global walkCount

    win.blit(bg, (0,0))  
    if currentWalk == walkLeft or walkRight:
            walkCount + 1 >= 12
            walkCount = 0
    
    if left:  
        currentWalk = walkLeft
        win.blit(currentWalk[walkCount//4], (x,y))
        walkCount += 1   
    elif right:
        currentWalk = walkRight
        win.blit(currentWalk[walkCount//4], (x,y))
        walkCount += 1
    elif space:
        win.blit(bg, (0,0))  
        if walkCount + 1 >= 16:
            walkCount = 0       
        
        currentWalk = walkLeft2
        win.blit(currentWalk[walkCount//4], (x,y))
        walkCount += 1

    else:
        win.blit(char, (x, y))
        walkCount = 0
    
    pygame.display.update() 



run = True

while run:
    clock.tick(27)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] or keys[ord('a')] and x > vel: 
        x -= vel
        left = True
        right = False
        space = False
        currentWalk = walkLeft

    elif keys[pygame.K_RIGHT] or keys[ord('d')] and x < 1000 - vel - width:  
        x += vel
        left = False
        right = True
        space = False
        currentwalk = walkRight
    
    else: 
        left = False
        right = False
        space = False
        walkCount = 0
    
    if not(isJump):
        if keys[pygame.K_UP] or keys[ord('w')]:
            isJump = True
            left = False
            right = False
            space = False
            walkCount = 0
    else:
        if jumpCount >= -10:
            y -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else: 
            jumpCount = 10
            isJump = False

    if keys[ord('n')]:   
        x -= vel
        left = False
        right = False 
        space = True
        currentWalk = walkLeft2    
    
    if keys[ord('z')]:   
        x -= vel
        left = False
        right = False 
        space = True
        currentWalk = walkLeft2   
        
    if x <= 0:
        x = 0
    elif x >= 1100:
        x = 1100

    

    redrawGameWindow() 
  

  
pygame.quit()

builtins.UnboundLocalError: local variable 'currentWalk' referenced before assignment. This is what happens when I run the code. I am trying to animate walkLeft2 and walkRight2 but it is not working, walkLeft and walkRight are animating. This is because the walkcount was originally eliminated if it was above 12 but I changed it to an if statement and now it does not run.

  • 1
    You need `global currentWalk`. Also see https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values – Barmar Dec 08 '21 at 01:43
  • You're missing `if` before `walkCount + 1 >= 12` – Barmar Dec 08 '21 at 01:43

0 Answers0