如何在pygame中冻结特定实例而又不影响其他实例>>

问题描述 投票:1回答:1

[我正在使用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是包含所有火车实例的列表,并且一次由一列火车填充。

[我正在使用pygame开发火车模拟器(仅使用一个rect来代表火车),我有一班火车,并且该班有停止功能,可以在每个车站停止火车(由x定义...

] >

我强烈建议您不要使用多线程/处理这个问题。尽管多线程/处理可能能够解决您的问题,但是引入多线程/处理可能会使其将来难以添加或改进代码,并且很可能导致更多错误。只需调整代码的设计即可解决此问题。

所有列车立即停止的原因是由于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

在您的move函数中,有一个if条件来检查该标志是否为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()的调用处于某个主事件循环中,在该循环中不断对其进行调用。

python pygame python-multiprocessing python-multithreading pygame-clock
1个回答
2
投票

我强烈建议您不要使用多线程/处理这个问题。尽管多线程/处理可能能够解决您的问题,但是引入多线程/处理可能会使其将来难以添加或改进代码,并且很可能导致更多错误。只需调整代码的设计即可解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.