重置后世界崩溃

时间:2020-02-19 11:57:27

标签: python pygame box2d collision reset

我具有此PyBox2D功能,并且我希望所有车身都被破坏,然后在汽车撞到建筑物时重置。 碰撞检测效果很好,破坏世界也是如此,当我尝试重置世界时会出现问题。 世界可能会崩溃,或者汽车无法控制地行驶,或者根本不行驶。

def _reset():
    if len(box2world.bodies) == 0:
        for building in skyscrapers:
            building.destroy_flag = False


        for wheel in cars[0].tires:
            wheel.destroy_flag = False

        cars[0].destroy_flag = False

        create_buildings()      
        create_car()
        cars[0].control()

box2world = world(contactListener=myContactListener(), gravity=(0.0, 0.0), doSleep=True)

1 个答案:

答案 0 :(得分:0)

您似乎控制的唯一汽车是cars [0],是列表中的第一辆汽车。 当您撞到建筑物和_step()时,会将cars [0]的destroy_flag设置为True,然后将其销毁,然后在_reset中将其重新设置为false。 同样,当您创建汽车时,您将附加到汽车上。您需要将汽车重置为空列表:创建新汽车时,也不会更新汽车[0]的位置,而只是在列表中更新新汽车。除了不清空摩天大楼列表之外,同一位置还有汽车[0]在同一位置。 这将导致永久性的毁坏/重置场景,进而无限期地创建汽车和摩天大楼,从而使您的世界崩溃。

def _reset():
    if len(box2world.bodies) == 0:
        for building in skyscrapers:
            building.destroy_flag = False


        for wheel in cars[0].tires:
            wheel.destroy_flag = False

        cars[0].destroy_flag = False

        skyscrapers=[]
        cars = []
        #or you could keep your old car and just increase the index
        # to do this, instead of calling car[0], your may want to call car[carnum]
        #before creating your first car you could set the carnum to 0
        #just before creating the new car during reset you would do carnum += 1
        #another way would be instead of appending your car to a list you could do cars=[Car()]

        create_buildings()
        create_car()
        cars[0].control()