My character won't move in python
Question: This is the code I used.

    
    
    import pygame, sys
    
    from pygame.locals import *
    
    pygame.init()
    
    def game():
    
    width, height = 1000, 600
    screen = pygame.display.set_mode((width,height))
    pygame.display.set_caption('My game far now :P') #This command allows you make a title.
    background=pygame.image.load('AE.jpg')
    background = pygame.transform.scale(background, (width,height))
    screen.blit(background, (0,0))
    
    #Load target image and player
    
    
    
    player = pygame.image.load('little.png')
    player = pygame.transform.scale(player, (40,40))
    px,py = width/2,height/2
    screen.blit(player, (px,py))
    
    movex = movey = 0
    
    #Running of the game loop
    
    while True:
        screen.blit(background, (0,0))
        #screen.blit(target,targetpos)
        screen.blit(player, (px,py))
        pygame.display.update()
    
    
        #keyboard an/or mouse movements
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
    
    
            elif event.type == pygame.KEYDOWN:
                if event.key == K_RIGHT:
                    movex = 2
                if event.key == K_LEFT:
                    movex = -2
                if event.key == K_UP:
                    movey = -2
                if event.key == K_DOWN:
                    movey = 2
    
            elif event.type == pygame.KEYUP:
                if event.key == K_RIGHT:
                    movex = 0
                if event.key == K_LEFT:
                    movex = 0
                if event.key == K_UP:
                    movey = 0
                if event.key == K_DOWN:
                    movey = 0
    
                    px = px + movex
                    py = py + movey
    
    
    
    
    #Python 's way of running the main routine
    
    if __name__=='__main__':
       game()
    

When I run the program it all starts right, the screen opens with background
and player spawning in the middle of the screen, but when I try to move
nothing happends, no errors nothing.

Would apriciate any help I can get :)

Thx for taking time to help me.


Answer: You seem to have code indentation problem in last two lines which may be
causing the bug.

Your curent code is equivalent to this if code block:

    
    
        elif event.type == pygame.KEYUP:
            if event.key == K_RIGHT:
                movex = 0
            if event.key == K_LEFT:
                movex = 0
            if event.key == K_UP:
                movey = 0
            if event.key == K_DOWN # IF BLOCK STARTS
                movey = 0
    
                px = px + movex  # THIS FALLS IN THE PREVIOUS IF BLOCK
                py = py + movey
    

Correct code would be :

    
    
      elif event.type == pygame.KEYUP:
            if event.key == K_RIGHT:
                movex = 0
            if event.key == K_LEFT:
                movex = 0
            if event.key == K_UP:
                movey = 0
            if event.key == K_DOWN # IF BLOCK STARTS
                movey = 0          #IF BLOCK ENDS
    
      px = px + movex  # NOW THIS IS OUT OF THE IF BLOCK
      py = py + movey
    

