'How to simulate more train objects running simultaneously in Python?
Good evening,
I am trying to make my first simulation, modelling metro network. I discovered I was probably very naive. Can someone point me in the right direction?
I wanted to have 6 trains on 3 metro lines (in opposite directions) running silently in the background in endless loops back and forth in each line. Then I thought the user could ask a question where is a particular train "right now" and would get its position for instance.
Here is the code (the problematic part):
from classes import Train
from objects import lineA, lineB, lineC
train1 = Train(1464, lineA[0])
train2 = Train(1464, lineA[-1])
train3 = Train(1464, lineB[0])
train4 = Train(1464, lineB[-1])
train5 = Train(1464, lineC[0])
train6 = Train(1464, lineC[-1])
train1.move(lineA)
train2.move(list(reversed(lineA)))
train3.move(lineB)
train4.move(list(reversed(lineB)))
train5.move(lineC)
train6.move(list(reversed(lineA)))
where moving is defined like:
class Train:
def __init__(self, places, position):
self.places = places
self.position = position
def move(self, line):
forth = True
while True:
for i in range(len(line)):
temp = line if forth else list(reversed(line))
position = temp[i]
time.sleep(2)
forth = not forth
I suppose the problem is that the program executes only the first function train1.move(lineA) which is an endless loop so nothing after that gets executed. I am not sure how to proceed...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
