Recently I began to study python, I have an idea to make an extremely primitive game. To accumulate a resource, gold is followed by a gradual increase, using a function with an infinite loop.

import time current_gold = 100 def gold(current_gold): while True: print(current_gold) time.sleep(1) current_gold += 10 gold(current_gold) current_gold -= 50 # test current_gold -= 50 # test 

How do I change current_gold in real time? Here you need to drive everything into the class or you need another function that will refer to the existing gold(current_gold) only with the argument gold(current_gold - 100) or how best to implement?

    1 answer 1

    It is better to implement without a cycle, because These are additional costs. You most likely have a time to start accumulating a resource:

     import time user_start = time.time() # Время unixtime когда пользователь начал накапливать ресурс 

    Now, when accessing the user object, you count in real time:

     new_balance = (time.time() - user_start) * 10 

    And return the number of the resource for the elapsed time, here is the class that it implements:

     import time class GoldInGame(object): def __init__(self): self.start_time = int(time.time()) self.my_gold = 0 self.gold_in_sec = 1 # количество золота в секунду def now_my_gold(self): now = int(time.time()) self.my_gold += (now - self.start_time) * self.gold_in_sec self.start_time = now return self.my_gold def upgrade_user(self, point): self.gold_in_sec += point print('now my gold in sec', self.gold_in_sec) self.start_time = int(time.time()) return True user_gold = GoldInGame() time.sleep(2) now = user_gold.now_my_gold() print(now) user_gold.upgrade_user(1) time.sleep(2) now = user_gold.now_my_gold() print(now) user_gold.upgrade_user(1) time.sleep(2) now = user_gold.now_my_gold() print(now) time.sleep(10) now = user_gold.now_my_gold() print(now) time.sleep(2) now = user_gold.now_my_gold() print(now) 
    • as it turns out, if I need to change a variable every second, then in any case I’ll have to drive this code into a loop. - Paul
    • There is no need for a cycle, why change the variable every second? - Igor Lavrynenko
    • The closest analogy - I need a counter (stopwatch), to which you can add seconds and take at any time, as well as reduce and increase the interval of "ticks." As far as I understand the method with time.time is not quite suitable for monitoring the value of a variable. For example, I have to accumulate 2 gold every second. I build a farm that costs 50 gold, after which 3 gold / sec is mined. - Pavel
    • To take away the gold, just take it away from self.my_gold - Igor Lavrynenko
    • 2
      time.monotonic() : it is useful to enter "game time", not tied to the computer clock ( time.monotonic() ). For games, you can use the asynchronous organization of the program (as in network services, GUI) - there is one central cycle in which events are processed and which provides an interface to connect your handlers (for example, almost all popular GUI, network libraries). The central loop might look like: while not game_over: paint(); get_input(); handle_schedule_events(); compute_new_game_state() while not game_over: paint(); get_input(); handle_schedule_events(); compute_new_game_state() while not game_over: paint(); get_input(); handle_schedule_events(); compute_new_game_state() (the loop can be hidden inside libraries, for example, tkinter, asyncio) - jfs