简体   繁体   English

每次到达新航点时,让我的自动驾驶汽车的行为发生变化的最有效方法是什么? (Python)

[英]What would be the most effective way to have my automated vehicle's behavior change every time it reached a new waypoint? (Python)

I would like to have my automated vehicle change its behavior every time it reaches a new waypoint.我想让我的自动驾驶汽车在每次到达一个新的航路点时改变它的行为。 I planned to nest "if" statements for however many iterations I end up choosing, but the loop always gets stuck in the first if statement, keeping the vehicle in "aggressive" every time it reaches a new waypoint.我计划为最终选择的多次迭代嵌套“if”语句,但循环总是卡在第一个 if 语句中,每次到达新航点时都保持车辆“积极”。 What would be the best way to ensure the behavior changes everytime it reaches a new waypoint?确保每次到达新航点时行为都会发生变化的最佳方法是什么?

if agent.done():
    agent = BehaviorAgent(world.player, behavior='aggressive') 
    agent.set_destination(random.choice(spawn_points).location)
    world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
    print("The target has been reached, searching for another target")

    if agent.done():
        agent = BehaviorAgent(world.player, behavior='cautious') 
        agent.set_destination(random.choice(spawn_points).location)
        world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
        print("The target has been reached, searching for another target")

Please let me know if any additional code is needed.如果需要任何其他代码,请告诉我。

It is possible that I completely misunderstood the question, but if I understood right, you want to change it every time a destination from aggressive to cautious and back to aggressive.我可能完全误解了这个问题,但如果我理解正确,您希望每次将目的地从激进变为谨慎,然后再回到激进。 I would do it with a global variable like this:我会用这样的全局变量来做到这一点:

current_behavior = "cautious"

if agent.done():

    if current_behavior == "cautious":
        current_behavior = "aggressive"
        agent = BehaviorAgent(world.player, behavior='aggressive') 
        agent.set_destination(random.choice(spawn_points).location)
        world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
        print("The target has been reached, searching for another target")

    elif current_behavior == "aggressive":
        current_behavior = "cautious"
        agent = BehaviorAgent(world.player, behavior='cautious') 
        agent.set_destination(random.choice(spawn_points).location)
        world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
        print("The target has been reached, searching for another target")

And I would also clean up the unnecessary code like this:我也会像这样清理不必要的代码:

current_behavior = "cautious"

if agent.done():

    if current_behavior == "cautious":
        agent = BehaviorAgent(world.player, behavior=current_behavior) 
        current_behavior = "aggressive"
    
    elif current_behavior == "aggressive":
        agent = BehaviorAgent(world.player, behavior=current_behavior)
        current_behavior = "cautious" 

    agent.set_destination(random.choice(spawn_points).location)
    world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
    print("The target has been reached, searching for another target")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在操作列表或字符串时,最有效的迭代方法是什么? - What's the most effective way to Iterate, while manipulating a list or string? 将Python词典/列表插入SQL数据库的最有效方法是什么? - What is the most effective way of Inserting Python dictionaries / lists into SQL database? 在Python中增加大量值的最有效方法是什么? - What is the most effective way to incremente a large number of values in Python? 在 pygame 中做陷阱的有效方法是什么? - What would be an effective way to do traps in pygame? Python:过滤列表的最有效方法 - Python: most effective way of filtering a list 在Python中对元素计数器进行分组的最有效方法 - Most effective way to group element counter in Python 最有效的方法是在 Python 中使用海龟绘制像素? - What would the most efficient way be to draw a pixel using turtle in Python? 什么是最有效的Python睡眠方式? - What's the most efficient way to sleep in Python? 随机翻转numpy数组中每个图像的最有效方法 - The most effective way to random flip every image in a numpy array 在两个非常大的文件中使用 python 比较字符串的最有效方法是什么? - What is the most effective way to compare strings using python in two very large files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM