简体   繁体   English

如何在后台运行while循环以便其他操作可以继续(Python)

[英]How can I run a while loop in the background so that the other operations can continue (Python)

I am making a simple game, and have created a loop to continuously check if the enemy is dead:我正在制作一个简单的游戏,并创建了一个循环来不断检查敌人是否已经死亡:

while player_health > 0:
    if current_enemy_dic['current_enemy']['Health'] <= 0:
        slayed_enemy_count = slayed_enemy_count + 1
        enemy_encounter(slayed_enemy_count)

The problem is that the program will not allow the other code to run, and will continually be checking for the if statement to be true.问题是程序不允许其他代码运行,并且会不断检查 if 语句是否为真。 To fix this, I was wondering if there was a way for it to run in the background, and run concurrently with other operations.为了解决这个问题,我想知道是否有办法让它在后台运行,并与其他操作同时运行。

Thank you谢谢

Don't do it concurrently: Create a main loop, register various tasks with the loop.不要同时进行:创建一个主循环,在循环中注册各种任务。 Every time around the loop the tasks will run.每次循环时,任务都会运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 中运行其他代码时如何在后台运行循环 - How to run a loop in the background while running other code in python 如何停止无限while循环并继续执行其余代码? - python How can I stop an infinite while loop and continue with rest of code? 如何使用for循环在一组数据帧上运行操作? - How can I run operations on a set of dataframes with a for loop? 我可以在另一个循环的后台运行一个循环吗? 蟒蛇 - Can I run a loop in the background of another loop? Python 如何故意降低python脚本的速度,以便在我工作时可以在后台永久运行它? - How to intentionally slow down a python script so I can have it running forever in the background while I work? Selenium,使用Python,如何简化脚本,以便可以从其他python脚本运行它们? - Selenium, with Python, how to simplify scripts so that I can run them from other python scripts? 如何通过交替数学运算在 python 上使用 for 循环? - How can I use a for loop on python with alternating mathematical operations? python多线程,我如何一次运行多个操作 - python multithreading, how can i run multiple operations at once 如何同时通过2个列表运行循环,以便循环中的每个项目都匹配? [蟒蛇] - How can I run a loop through 2 lists simultaneously so each item in the loops match up? [Python] 如何重置 python 中的 while 循环,以便所有条件都与开始时相同? - How can I reset a while loop in python so that all conditions are the same as when it started?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM