1

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!

5
  • 1
    To answer your question - you will need to look into threading. However there is easier way, using simple calculations - you can calculate the timedelta in seconds since start of the game. Commented Mar 15, 2020 at 16:38
  • This is a fairly broad question. Essentially, you need (at least) two threads; one that increments a time variable, and one that runs your game while using read-only access to the time variable. This turns into a problem of synchronizing access to the variable, so that only one thread tries to read from or write to it at a time, which can be solved using various locking mechanisms, or by writing asynchrouns coroutines that trade off between updating the variable and running your game in a single thread. Commented Mar 15, 2020 at 16:38
  • Note: you haven't actually included what you have so far :) Commented Mar 15, 2020 at 16:38
  • (Or the third option of computing game time from real time on demand, rather than explicitly tracking game time, as suggested by @buran.) Commented Mar 15, 2020 at 16:39
  • Multithreading won't help much with OP's code assumedly waiting on a synchronous input() call... Commented Mar 15, 2020 at 16:42

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.