I'm making a mini-game in Python. The idea is that, when the object is near the player within a fixed distance, the thread is started and repeated after an amount of times.
For example:
class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
self.start()
def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)
def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
def ObjectHit(player):
player.power = player.power + 5
if((Object_A_pos_x == player_pos_x + distance or Object_A_pos_x == player_pos_x - distance) and
(Object_A_pos_y == player_pos_y + distance or Object_A_pos_y == player_pos_y - distance)):
tA = RepeatedTimer(10, ObjectHit, player)
% Thread t starts after every 10 seconds
else:
t.stop()
I want that many object affect to the player separately in the same way. For example, object A is near the player for 23 seconds and then object B comes. So now there are two thread which are tA and tB. After that, object A leaves so the thread tA stops but the tB keeps running until the objectB leaves. And these apply similarly to objectC, objectD, ....
I have read some documentation about reusing and spawning thread such as ThreadPool
and concurrent.futures
. However, i'm new to multithreading so i don't have very clear idea about these.
Thanks alot for helping me.