Closed Crossroad (python)

I can’t figure out how to make my hero build fences and firetraps in the right place. Here’s what I have:

# Only build if you see an enemy.

# The function defines THREE parameters.
def maybeBuildSomething(buildType, x, y):
    # Move to the position specified by x and y parameters.
    hero.moveXY(x, y)
    # Find the nearest enemy.
    enemy = hero.findNearestEnemy()
    # If there is an enemy
    if enemy:
        # then use buildXY with buildType, x, and y
        hero.buildXY("fire-trap", 40, 22)
        hero.buildXY("fence", 26, 35)
        hero.buildXY("fire-trap", 40, 50)
        hero.buildXY("fence", 54, 35)
    pass
    
while True:
    # Call maybeBuildSomething with "fire-trap" and the coordinates of the bottom X.
    maybeBuildSomething("fire-trap", 40, 20)
    # Call maybeBuildSomething, with "fence" at the left X!
    maybeBuildSomething("fence", 26, 34)
    # Call maybeBuildSomething with "fire-trap" at the top X!
    maybeBuildSomething("fire-trap", 40, 50)
    # Call maybeBuildSomething with "fence" at the right X!
    maybeBuildSomething("fence", 54, 34)

Any suggestions?

2 Likes

Can you properly format your code? You can use the triple backticks to format your code.
Click on

</>
2 Likes

Also I see the problem. You are building in all locations at once in your function. Try picking them out one by one. In the function put hero.buildXY(buildType, x, y). When you call this function in your loop, fill in the parameters buildType, x, and y with the required buildType, x coordinate, and y coordinate.

2 Likes

Make sure you write

hero.buildXY(buildType, x, y)

You have already passed the parmeters for what to build and where

1 Like

Yes please do what @Hellenar suggested , an easier way would be to place triple backticks around you code. Like this

enemy = self.findNearestEnemy()
if enemy;
self.attack(enemy)

becomes this:

enemy = self.findNearestEnemy()
if enemy;
self.attack(enemy) 

Please :heart: this post if it is useful

Cheers :beers:

2 Likes

It’s an instruction. So you need just one statement instead four. This level is a demonstration of the using functions with multiple parameters.

3 Likes