我正在使用pygame开发火车模拟器(仅使用rect来代表火车),我有一个班级火车,这个班级具有停止功能,可以在每个站上停止火车(由x坐标定义):
def stop(self):
current_time = pg.time.get_ticks()
while pg.time.get_ticks() - current_time < self.stopping_Time:
continue
pass
这种实现方式只适用于一个火车实例,但是我的问题是,当添加更多火车时,如果一个实例停在其车站,则所有其他火车实例也会停止,即使它们不在该车站也是如此!
我也尝试过该实现,但没有成功:
def stop(self):
while self.stopping_Time> 0:
self.stopping_Time -= 1
pass
这个答案也对我不起作用:https : //stackoverflow.com/a/46801334/11334093
是多线程问题吗?我需要为每个火车实例创建一个线程,以便他们可以独立执行停止功能吗?或者如何为该功能使用多处理技巧?
这是我的整个火车课:
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
我还有另一个函数,我称之为:
for train in op_trains:
train.move()
op_train是一个包含所有火车实例的列表,并且一次填充一列火车。
我强烈建议您不要使用多线程/处理这个问题。尽管多线程/处理可能能够解决您的问题,但引入多线程/处理可能会使其将来很难添加或改进代码,并且很可能会导致更多错误。只需调整代码设计即可解决此问题。
所有列车立即停止的原因是由于stop功能中的while循环。
def stop(self):
current_time = pg.time.get_ticks()
while pg.time.get_ticks() - current_time < self.stopping_Time:
continue
pass
这个while循环会阻塞主线程,从而阻止pygame处理其他火车。在火车课程中,从移动功能调用停止功能。现在的目标是用一种跟踪火车保持多长时间而不会阻塞主线程的方式来替换while循环。
您需要做的是在火车班级中设置一个状态,以表示火车是否已停止。
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()
# new attributes
self.stopped = False
self.stopped_time = None
要停止火车,请切换状态并记下停止开始的时间。
def stop(self):
self.stopped_time = pg.time.get_ticks()
self.stopped = True
在您的移动功能中,使用if条件检查此标志是否为True,如果为True,则将其停止多长时间。如果停止时间足够长,请将标志设置为False并继续。我已经进行了重新排序,并清理了条件。
def move(self):
if self.stopped and pg.time.get_ticks() - self.stopped_time < self.stopping_Time:
return
self.stopped = False
if self.right:
self.x += self.vel
else:
self.x -= self.vel
if self.x == 235 or self.x == 510 or self.x == 1295:
self.stop()
if self.x == 235:
self.right = True
self.left = False
elif self.x == 1295:
self.right = False
self.left = True
请注意,没有循环。
此代码假定您的调用train.move()
处于某个主事件循环中,在该循环中不断对其进行调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句