1
\$\begingroup\$

I've been testing out movement for a fighting game that I'm creating in Python.

The problem is that when I try to move the sprite, it moves correctly to the left, but only ever works in that direction. I think the problem is with my event handling loop, but I don't have much experience so I have no idea how to solve the problem. Googling and spending a while doing trial and error was no help.

My main loop looks like this:

pressedLeft = None
pressedRight = None
pressedUp = None
pressedDown = None

while True:

    #event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pressedLeft = True
            elif event.key == pygame.K_d:
                PressedRight = True
            elif event.key == pygame.K_w:
                PressedUp = True
            elif event.key == pygame.K_s:
                PressedDown = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                pressedLeft = False
            elif event.key == pygame.K_d:
                PressedRight = False
            elif event.key == pygame.K_w:
                PressedUp = False
            elif event.key == pygame.K_s:
                PressedDown = False

    # movement
    if pressedLeft:
        profx -= 5
    elif pressedRight:
        profx += 5
    elif pressedUp:
        profy -= 5
    elif pressedDown:
        profy += 5
    DisplaySurf.fill(white)
    DisplaySurf.blit(profImg, (profx, profy))
    pygame.display.update()
    fpsClock.tick(FPS)
\$\endgroup\$

closed as off-topic by Josh Oct 6 '15 at 15:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ You have an issue with the capitalization of your variable names. You use a lowercase first letter for pressedLeft, but you're not careful with the other names (PressedRight vs pressedRight, same for up and down). \$\endgroup\$ – Vaillancourt Oct 5 '15 at 13:53
  • \$\begingroup\$ @AlexandreVaillancourt wanna make an answer out of that? I don't wanna steal yours :) \$\endgroup\$ – Elva Oct 5 '15 at 13:58
  • \$\begingroup\$ @KevinvanderVelden This is almost too trivial for an answer, but oh well! \$\endgroup\$ – Vaillancourt Oct 5 '15 at 14:01
  • 2
    \$\begingroup\$ @AlexandreVaillancourt Agreed, but it keeps the unanswered list down :) \$\endgroup\$ – Elva Oct 5 '15 at 14:06
5
\$\begingroup\$

You have an issue with the capitalization of your variable names.

You use a lowercase first letter for pressedLeft, but you're not careful with the other names (PressedRight vs pressedRight, same for up and down).

This results in the creation of never used values in your if blocs.

The syntax colouring helped my spot it right away.

This should do the trick:

pressedLeft = None
pressedRight = None
pressedUp = None
pressedDown = None

while True:

    #event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pressedLeft = True
            elif event.key == pygame.K_d:
                pressedRight = True
            elif event.key == pygame.K_w:
                pressedUp = True
            elif event.key == pygame.K_s:
                pressedDown = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                pressedLeft = False
            elif event.key == pygame.K_d:
                pressedRight = False
            elif event.key == pygame.K_w:
                pressedUp = False
            elif event.key == pygame.K_s:
                pressedDown = False

    # movement
    if pressedLeft:
        profx -= 5
    elif pressedRight:
        profx += 5
    elif pressedUp:
        profy -= 5
    elif pressedDown:
        profy += 5
    DisplaySurf.fill(white)
    DisplaySurf.blit(profImg, (profx, profy))
    pygame.display.update()
    fpsClock.tick(FPS)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.