Python pygame中上下移动矩形的问题

Python pygame中上下移动矩形的问题,python,pygame,Python,Pygame,我试图用箭头键在Pygame中移动一个矩形。我可以左右移动,但不能上下移动。如果我按下向下键,它将沿y方向增长,而不是移动。 这是密码 import pygame , sys from pygame.locals import * catx = 10 caty = 10 screen =0 def myquit(): pygame.quit() sys.exit() def event_inpu

我试图用箭头键在Pygame中移动一个矩形。我可以左右移动,但不能上下移动。如果我按下向下键,它将沿y方向增长,而不是移动。 这是密码

    import pygame , sys
    from pygame.locals import *

    catx = 10
    caty = 10
    screen =0

    def myquit():
        pygame.quit()
        sys.exit()
    
    def event_input(events):
        global catx,caty, screen
    
        for event in events:
            if event.type == QUIT:
                pygame.quit()
            else:
                if event.type== KEYDOWN:
                    if event.key== K_ESCAPE:
                        myquit()
                    elif event.key== K_RIGHT:
                        catx+=5
                    elif event.key==K_LEFT:
                        catx-=5
                    else:
                        pass
                elif event.type== KEYUP:
                    if event.key== K_DOWN:
                        caty+=5
                    elif event.key== K_UP:
                        caty-=5
                screen.fill((0,0,0))
                pygame.draw.rect(screen,(255,255,255),(catx,50,50,caty))
                pygame.display.update()
    
    
    def main():
        global screen
        pygame.init()
        screen_width=640
        screen_height=500
        pygame.display.set_mode((screen_width,screen_height))
        pygame.display.set_caption("Move Rectangle")
        screen = pygame.display.get_surface()
        pygame.display.update()
    
        while True:
            event_input(pygame.event.get())
    
    main()

您正在更改矩形的高度,而不是位置。pygame.rect的参数是
(x,y,width,height)
,但是您的参数被传递时就像参数是
(x,width,height,y)
,因此它会更改矩形的高度而不是y。换行

pygame.draw.rect(屏幕,(255255255),(catx,50,50,caty))


pygame.draw.rect(屏幕,(255255255),(catx,caty,50,50))

释放按键时,程序似乎正在检查是否按下了上/下键:
elif event.type==KEYUP
。如果在
if event.type==KEYDOWN
子句中检查它会发生什么?谢谢,我知道了。我想我犯了一些新手错误