如何在 Python 中同时 运行 两个 while 循环?

how to run two while loops at the same time in Python?

我是 Python 的新手,使用 Zelle 的图形创建游戏。我需要下面的两个 while 循环同时到 运行,但是我 运行 遇到了困难。我尝试嵌套 while 循环,但只有单击鼠标时马和平民才会移动,这是我不想要的。我想要的是马匹和平民一直在移动,而公主只有在点击鼠标时才移动,当她拯救了 10 个平民时 "game over" 停止游戏。

    # animation loop.
while True==True:

    for horse in horseList:
        if horse.leg.getX() > -187:
            horse.move( -1, 20 )
        else:
            horse.move( 5, 28 )

    for civilian in civiliansList:

        if civilian.getX() < 800:
            civilian.move( 20, 0 )
        else:
            civilian.move( -100, 0 )

while civiliansSaved != 10:

    mouse = win.getMouse()

    princess.move( mouse, civilianCounter)

    civilianCounter = princess.move( mouse, civilianCounter)
    # move is a method that will return an updated civilianCounter ( it is initially 0 and defined outside of the while loop ), depending on whether princess runs into civilians

else:
    print( "Game over" )
    win.getMouse()
    win.close()

只需在动画循环中使用 checkMouse() 而不是 getMouse()

我想就这么简单。

while civiliansSaved < 11:

    for horse in horseList:
        if horse.leg.getX() > -187      
            horse.move( -1, 20 )
        else:
            horse.move( 5, 28 )

    for civilian in civiliansList:
        if civilian.getX() < 800:
            civilian.move( 20, 0 )
        else:
            civilian.move( -100, 0 )

    mouse = win.checkMouse()

    if mouse:
        princess.move( mouse, civilianCounter)
        civilianCounter = princess.move( mouse, civilianCounter)

print( "Game over" )
win.getMouse()
win.close()

多可:

checkMouse() Similar to getMouse, but does not pause for a user click. Returns the latest point where the mouse was clicked or None if the window as not been clicked since the previous call to checkMouse or getMouse. This is particularly useful for controlling simple animation loops.

这里是一个示例,无需并行处理(这在 python 中很棘手)即可完成您想要的操作:

while True:  # you don't need to write True==True
    for horse in horseList:
        if horse.leg.getX() > -187:
            horse.move( -1, 20 )
        else:
            horse.move( 5, 28 )

    for civilian in civiliansList:  
        if civilian.getX() < 800:
            civilian.move( 20, 0 )
        else:
            civilian.move( -100, 0 )

    mouse = win.getMouse()
    princess.move( mouse, civilianCounter)
    civilianCounter = princess.move( mouse, civilianCounter)

    if civiliansSaved >= 10: # check to see if 10 or more have been saved
        break
        print( "Game over" )
        win.getMouse()
        win.close()

你想做的是保持游戏 运行 直到 civilizationSaved 计数器至少为 10。但是你不能在主游戏循环之外的单独循环中执行此操作,因此它会产生更多感觉 在计数至少为 10 之前不要停止 游戏。此 if 语句可以包含在您的主游戏循环中。

Zelle 的图形不是事件驱动的图形工具,我认为这正是您要找的。也许考虑切换到 tkinter,它是 Python 标准库的一部分。您可以使用回调函数来处理鼠标事件,以便您的应用在等待处理鼠标事件时可以自由地做其他事情。

tkinter 有一个 after() 功能,允许您在指定的时间段后调用函数。您可以为 civilian movementhorse movement 创建单独的函数,并在 指定的时间段(比如 50 毫秒)后 调用这些函数。这有效地模拟了 运行 并行的功能,并允许马匹和平民同时移动,至少从用户的角度来看是这样。您需要在移动函数中再次调用 after(),以便连续调用它们。

self.root.after(50, self.process_horse_movement)
self.root.after(50, self.process_civilian_movement)

然后在每个移动函数结束时:

def process_horse_movement():
    ...
    self.root.after(50, self.process_horse_movement)

def process_civilian_movement():
    ...
    self.root.after(50, self.process_civilian_movement)

还有一个小问题。使用 while True: 而不是 while True == True: