简体   繁体   中英

How to run an infinite loop while continuing the rest of the script in Python

I am a beginner Python developer and I am making a text-based adventure game. I am at the point where I am setting up a time system for the game. I want to add one minute to the game for every second that passes in real-time. So essentially, I want to run a loop indefinitely while the rest of the code is also executed.

This is what I have so far. It seems that it is looping and adding time to stor_time, but it does not continue everything else below it. How can I fix this? Thank you!

I suggest you use multithreading concept. By using multithreading what you can do is have this infinite loop running in your separate thread and the rest of the code will keep running without interruption.

Also if you want to share some variables in this thread and your script, don't forget to use global for variable.

Some reference link : https://www.geeksforgeeks.org/multithreading-python-set-1/

Welcome to Stack Exchange!

Try:

Mins = 0 ##Assign the variable Mins

def Update_Timer():
    Mins = Mins + 1 ##Add 1 to the Mins.

clock.schedule_interval(Update_Timer, 1.0) ##Every 1.0 Seconds, run Update_Timer()

To update the variable Mins every 1 second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM