Python 我试图在Pygame中使用坐标移动一个矩形,但没有成功';行不通

Python 我试图在Pygame中使用坐标移动一个矩形,但没有成功';行不通,python,pygame,rect,Python,Pygame,Rect,这是我的密码 import pygame from pygame.locals import * import sys pygame.init() pygame.display.set_caption("*no current mission*") size = (1280, 750) screen = pygame.display.set_mode(size) clock = pygame.time.Clock() bg

这是我的密码

    import pygame
    from pygame.locals import *
    import sys
    pygame.init()
    pygame.display.set_caption("*no current mission*")
    size = (1280, 750)
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()
    bg = pygame.image.load("bg1.png")
    guy = pygame.image.load("hero_stand.png")
    rect = guy.get_rect()
    x = 10
    y = 10
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == KEYDOWN:
                    _if event.key == K_RIGHT:
                        x += 5
                        rect.move(x,y)_
        rect.move(x,y) 
        screen.blit(bg,(0,0))
        screen.blit(guy, rect)
        pygame.display.flip()

这只是一个简单的测试,看看我是否可以移动一个矩形。除了我用斜体字写的代码外,一切似乎都正常

我对PyGame一无所知,但它看起来像是
while
循环永远不会退出,缩进是这样的,从第二个
rect.move(x,y)
开始的所有内容都在循环之外,因此无法访问。从那里开始缩进所有内容,使其位于while循环中,这样做就可以了。

问题在于.flip()方法。您必须在每次blit和更新时调用它一次,这样它就会在屏幕上显示更改。将它们放在While循环中,它应该可以工作。 基本pygame循环应如下所示:

#capture user input and call functions responsible
delta = timer.tick()
#this takes into account the speed of the machine, so it moves the sprites indepented of speed
updateSprites(delta) #moves the sprites
drawSprites()#blits the sprites at their positions
screen.diplay.flip() #flips the screen to show changes.

欢迎来到StackOverflow?你能解释一下它是怎么不工作的吗?我认为代码上写着‘rect.move(x,y)’的两个地方是它失败的地方。我将x和y更改为100,并运行了程序,但没有任何更改。代码块支持斜体吗?请在进一步解释问题时编辑您的问题。如果你只是留下评论,人们可能会错过它是的,那部分代码是无法访问的,但这不是问题所在。我无意中在网站上以这种方式键入了它,但在我的实际程序中没有。您的帖子底部应该有一个“编辑”链接,可以让您修复此问题。:-)