Monday, 2 December 2019

How to set positive or negative integer but only in the first iteration of a loop

I have a Box2D world with circles representing pedestrians and squares representing buildings

def pedestrian_walk(pedestrian, buildings):
    ## Find the building nearest to the pedestrian
    list_of_distances = []
    for building in buildings:
        list_of_distances.append(np.sqrt((pedestrian.body.position[0] - building.footprint.position[0])**2 + (pedestrian.body.position[1] - building.footprint.position[1])**2))
    pedestrian.nearest_building= list_of_distances.index(min(list_of_distances))
    #print(f"Nearest builing index is: {pedestrian.nearest_building}")


    building = buildings[pedestrian.nearest_building]

    #Pedestrian walks around the left side of the nearest building
    if pedestrian.body.position[0] <= building.position[0] and  building.position[1] - building.shape[1] <= pedestrian.position[1] <= building.position[1] + 1.05*building.shape[1]:
        pedestrian.body.__SetLinearVelocity(b2Vec2(0,pedestrian.ped_velocity))
        print("1st if")
    #Pedestrian walks around the right side of the nearest building
    elif pedestrian.body.position[0] > building.position[0] and building.position[1] - building.shape[1] <= pedestrian.position[1]<= building.position[1] + 1.05*building.shape[1]:
        pedestrian.body.__SetLinearVelocity(b2Vec2(0,-pedestrian.ped_velocity))
        print("2nd if")

    #Pedestrian walks around the upper side of the nearest building
    elif pedestrian.body.position[1] > building.position[1] and building.position[0] - building.shape[0] <= pedestrian.position[0] <= building.position[0] + 1.05*building.shape[0]:
        pedestrian.body.__SetLinearVelocity(b2Vec2(pedestrian.ped_velocity,0))
        print("3rd if")
    #Pedestrian walks around the lower side of the nearest building
    elif pedestrian.body.position[1] <= building.position[1] and building.position[0] - building.shape[0] <= pedestrian.position[0] <= building.position[0] + 1.05*building.shape[0]:
        pedestrian.body.__SetLinearVelocity(b2Vec2(-pedestrian.ped_velocity,0))
        print("4th if")

I call the function in pygame main game loop and it works OK:

Skyscrapers = create four buildings
while running:

...

    while k <= MAX_AMOUNT_PEDESTRIANS:
        random_skyscraper = random.choice(skyscrapers) #Randomly choose a skyscraper next to which a pedestrian is generated
        skyscraper_to_walk_around.append(random_skyscraper)
        new_walker = Pedestrian(box2world, position = (random_skyscraper.position[0] -random.randint(-random_skyscraper.shape[0],random_skyscraper.shape[0]),\
                            random_skyscraper.position[1] -random.randint(-random_skyscraper.shape[1],random_skyscraper.shape[1])))
        walkers.append(new_walker)
        positive_negative.append([-1,1][random.randrange(2)])
        k = k+1

    for ped in range(MAX_AMOUNT_PEDESTRIANS):
        pedestrian_walk(walkers[ped],skyscrapers)

And if possible I would have another request, is there any more "pythonic" way how to make the code simpler or cleaner (but this is a secondary problem, I need to solve the turning first)? Thank you very much

EDIT: The main idea behind my code is this: I have classes Pedestrian and Building, Pedestrians are dynamic bodies and buildings are static bodies. I need pedestrians to move around its nearest building and cross "streets" (I tried to find working algorithm in my previous question "[Rolling a circle around a square][1]" where you can find my definition of both classes. I was able to kind of solve the rolling problem, it is not working perfectly but satisfactorily

FINAL EDIT My problem was solved but since I have started bounty on this question I have a new one concerning this code, please see my own answer to this question. Thank You



from How to set positive or negative integer but only in the first iteration of a loop

No comments:

Post a Comment