Why is my blitted characted not moving in pygame?
Question: I am making an RPG in Python using Pygame. My first step is to create my main
character and let it move. But it isn't. This is my code:

    
    
    import pygame,random
    from pygame.locals import *
    
    pygame.init()
    
    black = (0,0,0)
    white = (255,255,255)
    red = (255,0,0)
    blue = (0,255,0)
    green = (0,0,255)
    
    global screen, size, winWidth, winHeight, gameExit, pressed, mainChar, x, y
    size = winWidth,winHeight = (1350,668)
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("RPG")
    gameExit = False
    pressed = pygame.key.get_pressed()
    mainChar = pygame.image.load("Main Character.png")
    x,y = 655,500
    
    def surroundings():
        stoneTile = pygame.image.load("Stone Tile.png")
        stoneTileSize = stoneTile.get_rect()
    
    def move():
        if pressed[K_LEFT]: x -= 1
        if pressed[K_RIGHT]: x += 1
        if pressed[K_UP]: y -= 1
        if pressed[K_DOWN]: y += 1
    
    def player():
        move()
    
        screen.fill(black)
        screen.blit(mainChar,(x,y))
    
    while not gameExit:
        for event in pygame.event.get():
            if event.type == QUIT:
                gameExit = True
    
        surroundings()
        move()
        player()
    
        pygame.display.update()
    
    pygame.quit()
    quit()
    

Please help me and explain why it isn't working, too. Thanks.


Answer: You will have to update your pressed variable in each run

    
    
    while not gameExit:
        for event in pygame.event.get():
            if event.type == QUIT:
                gameExit = True
    
        pressed = pygame.key.get_pressed()
    
        surroundings()
        move()
        player()
    
        pygame.display.update()
    

The values x and y that you have used within that move function are being
treated as a local variable you will have to tell the interpreter that they
are global variables

    
    
    def move():
        global x,y
        if pressed[K_LEFT]: x -= 1
        if pressed[K_RIGHT]: x += 1
        if pressed[K_UP]: y -= 1
        if pressed[K_DOWN]: y += 1
    

