如何在 python 中保持 运行 的嵌套 While

How to keep running a nested While in python

我有一个pygame。里面有几个嵌套的while循环,运行他们一次完成一个回合,我如何让回合一直进行,永不停止?

重复{我有一个pygame。其中有几个嵌套的 while 循环,运行 所有这些都一次完成一轮,我如何让轮次继续进行,永不停止?我有一个pygame。其中有几个嵌套的 while 循环,运行 所有这些都一次完成一轮,我如何让轮次继续进行,永不停止?我有一个pygame。其中有几个嵌套的 while 循环,运行 所有这些都一次完成一轮,我如何让轮次继续进行,永不停止?我有一个pygame。其中有几个嵌套的 while 循环,运行 所有这些都一次完成一轮,我如何让轮次继续进行,永不停止? }重复

def running_game():
    import pygame
    pygame.init()
    window = pygame.display.set_mode((640, 480))
    robot = pygame.image.load("robot.png")


    x = 0
    y = 0
    velocity = 5
    clock = pygame.time.Clock()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        window.fill((0, 0, 0))
        window.blit(robot, (x, y))
        pygame.display.flip()
        print(x)
        x += velocity
        if x+robot.get_width() >= 640:
            #break
            #print(x)
            while True:
                window.fill((0, 0, 0))
                window.blit(robot, (x, y))
                pygame.display.flip()
                y += velocity
                #clock.tick(60)
                if y+robot.get_height() >= 480:
                    #break
                    velocity = -velocity
                    while True:
                        window.fill((0, 0, 0))
                        window.blit(robot, (x, y))
                        pygame.display.flip()
                        x += velocity
                        if x <= 0:
                            #break
                            while True:
                                window.fill((0, 0, 0))
                                window.blit(robot, (x, y))
                                pygame.display.flip()
                                y += velocity
                                if y <= 0:
                                    velocity = -velocity
                                    break               

        clock.tick(120)
if __name__ == "__main__":
    while True: 
        running_game()

你那里嵌套太多了。因为所有内部 while 循环永远不会终止,程序也永远不会返回到 pygame.event.get() 调用,这就是为什么您还会发现程序不响应输入(包括关闭 window).还有很多重复的代码。

最好将机器人的当前方向保存在一个变量中,我们称之为direction,并在到达边缘时更改其值。为了让代码更容易理解,可以在顶部定义一些常量:

UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3

def running_game():
    import pygame
    pygame.init()
    window = pygame.display.set_mode((640, 480))
    robot = pygame.image.load("robot.png")

    x = 0
    y = 0
    velocity = 5
    direction = RIGHT
    clock = pygame.time.Clock()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

        window.fill((0, 0, 0))
        window.blit(robot, (x, y))
        pygame.display.flip()

        if direction == UP:
            y -= velocity
            if y <= 0:
                direction = RIGHT
        elif direction == RIGHT:
            x += velocity
            if x+robot.get_width() >= 640:
                direction = DOWN
        elif direction == DOWN:
            y += velocity
            if y+robot.get_height() >= 480:
                direction = LEFT
        elif direction == LEFT:
            x -= velocity
            if x <= 0:
                direction = UP

        clock.tick(120)

if __name__ == "__main__":
    while True: 
        running_game()