重置后世界崩溃

2024-06-16 12:57:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个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)

Tags: in功能falseforcreate世界cars汽车
1条回答
网友
1楼 · 发布于 2024-06-16 12:57:13

看起来您控制的唯一汽车是汽车[0],它是列表中的第一辆汽车。 当你撞到一栋建筑时,_step()将汽车[0]的销毁标志设置为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()

相关问题 更多 >