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

标签 python parallel-processing python-multithreading

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

    # 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 - 如何在Python中同时运行两个while循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30092997/

相关文章:

python - 比较多个也包含数字的单词,并将它们设置为升序

python动态类名

matlab - Simulink-Simulation with parfor(并行计算)

go - 保存并行化 goroutine 的结果

python - 限制 Python 上每秒的 HTTP 请求数

python - 如何通过 input() 控制线程?

Python请求库——获取SSL证书信息

Python 2D 数组——如何插入 x 并检索 y 值?

java - 在嵌套的 Java 8 并行流操作中使用信号量可能会死锁。这是一个错误吗?

python - 模块对象没有属性 'thread'