I am using pygame to develop a train simulator (using simply a rect to represent a train) I have a class train and this class has a stop function to stop trains at each station (defined by an x coordinate):
def stop(self):
current_time = pg.time.get_ticks()
while pg.time.get_ticks() - current_time < self.stopping_Time:
continue
pass
This implementation works with one train instance but my problem is that when adding more trains, if an instance stop at its station all the other train instances stop even if they are not in there station!
I have tried this implementaion also and it didn't work:
def stop(self):
while self.stopping_Time> 0:
self.stopping_Time -= 1
pass
This answer didn't work for me also: https://stackoverflow.com/a/46801334/11334093
Is it a multithreading problem? do I need to create a thread for each train instance, so they can execute the stop function independently? Or how can I use a multiprocessing trick for this function?
Here is my whole train class:
class train(object):
"""docstring for train"""
def __init__(self, x, y, width, height, vel):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = vel
self.right = True
self.left = False
self.headway = 20
self.stopping_Time = 2500
self.capacity = 200
self.start_time = pg.time.get_ticks()
def draw(self, win):
#trains
pg.draw.rect(win, (255,0,0), (self.x, self.y, self.width, self.height))
def stop(self):
self.current_time = pg.time.get_ticks()
while pg.time.get_ticks() - self.current_time < self.stopping_Time:
continue
pass
def move(self):
if self.right:
if self.x == 235 or self.x == 510 or self.x == 1295:
self.stop()
if self.x == 1295:
self.right = False
self.left = True
self.x -= self.vel
else:
self.x += self.vel
else:
self.x += self.vel
else:
if self.x == 235 or self.x == 510:
#train.stop = 3 * 100
self.stop()
if self.x == 235:
self.right = True
self.left = False
self.x += self.vel
else:
self.x -= self.vel
else:
self.x -= self.vel
and I have another function which in it I call:
for train in op_trains:
train.move()
op_train is a list containing all train instances and it is filled by one train at a time.