H
H
human1002021-03-31 17:46:08
Python
human100, 2021-03-31 17:46:08

How to solve NameError: name 'animCount' is not defined?

In the process of creating a simple game, I encountered an error.
Thanks in advance!

def play():
    import pygame
    pygame.init()

    walkup = [pygame.image.load('ee.goup.png'), pygame.image.load('ee.goup1.png'),
    pygame.image.load('ee.goup2.png'), pygame.image.load('ee.goup3.png')]

    walkdown = [pygame.image.load('ee.godown.png'), pygame.image.load('ee.godown1.png'),
    pygame.image.load('ee.godown2.png'), pygame.image.load('ee.godown3.png')]

    walkright = [pygame.image.load('ee.goright.png'), pygame.image.load('ee.goright1.png'),
    pygame.image.load('ee.goright2.png')]

    walkleft = [pygame.image.load('ee.goleft.png'), pygame.image.load('ee.goleft1.png'),
    pygame.image.load('ee.goleft2.png')]

    mag = pygame.image.load("ee.png")

    colors = (2, 200, 70)
    height = 900
    width = 900

    window = pygame.display.set_mode((height, width))
    screen = pygame.Surface((height, width))

    start = True 

    mag_width = 118
    mag_height = 182

    x = 450
    y = 450 

    speed = 1

    clock = pygame.time.Clock()

    right = False
    left = False
    up = False
    down = False

    animCount = 0

    def drawwidnow():
        global animCount

        if animCount + 1 >= 30:
            animCount = 0
        if left:
            window.blit(walkleft[animCount // 5] (x, y))
            animCount += 1
        elif right:
            window.blit(walkright[animCount // 5] (x, y))
            animCount += 1
        elif up:
            window.blit(walkup[animCount // 5] (x, y))
            animCount += 1
        elif down:
            window.blit(walkdown[animCount // 5] (x, y))
            animCount += 1
        else:
            screen.blit(mag, (x, y))

        window.blit(screen, (0,0))
        pygame.display.update()
        screen.fill(colors)

    while start == True:
        clock.tick(30)

        for i in pygame.event.get():
            if i.type == pygame.QUIT:
                start = False 
        keys = pygame.key.get_pressed()
        if keys [pygame.K_LEFT] and x > 0:
            x -= speed
            right = False
            left = True
            up = False
            down = False
        elif keys [pygame.K_RIGHT] and x < width - mag_width:
            x += speed
            right = True
            left = False
            up = False
            down = False
        elif keys [pygame.K_UP] and y > 0:
            y -= speed
            right = False
            left = False
            up = True
            down = False
        elif keys [pygame.K_DOWN] and y < height - mag_height:
            y += speed
            right = False
            left = False
            up = False
            down = True
        else:
            right = False
            left = False
            up = False
            down = False
            animCount = 0
        drawwidnow()
play()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-03-31
@jerwright

If you want to use global in this situation, then try moving global animCountlike this:

global animCount
animCount = 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question