Search code examples
pythonpython-3.xpygamenameerror

Found this code online of creating a game with pygame - can't find error.(Version of python is older in the guide I'm using, if that helps)


specific line of error:

if event.type==pygame.MOUSEBUTTONDOWN:
NameError: name 'event' is not defined

the block:

position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26))
playerrot = pygame.transform.rotate(player, 360-angle*57.29)
playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
screen.blit(playerrot,playerpos1)
if event.type==pygame.MOUSEBUTTONDOWN:
        position=pygame.mouse.get_pos()
        acc[1]+=1
        arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])

I'm a newbie, would appreciate all the help I can get :) Thank you!


Solution

  • I think you are missing the line where 'event' is defined.

    Usually in pygame this line (any line where you use 'event', really),

    if event.type==pygame.MOUSEBUTTONDOWN:
    

    is run inside a for loop where 'event' is defined like so:

    # get all the events
    for event in pygame.event.get():
        # do something with an event
        if event.type==pygame.MOUSEBUTTONDOWN: