Precision kicking help!


#21

Okay, I almost have it, but I can’t seem to get it to do the right thing. Can I have some help @Mr-Borges?

Here is my code:

# Push the ball to knock over all the blue skeletons without hitting any red ones.
# The blue skeletons can be found as enemies.
loop:
    ball = hero.findNearest(hero.findByType("ball"))
    ballPos = ball.pos
    enemy = hero.findNearestEnemy()
    friends = hero.findFriends()
    friend = hero.findNearest(friends)
    heroPos = hero.pos
    nubPos = enemy.pos
    newVector = Vector.subtract(nubPos, ballPos)
    direction = Vector.normalize(newVector)
    dm = Vector.multiply(direction, -3)
    finalVector = Vector.add(ballPos, dm)
    flag = hero.findFlag("black")
    if flag:
        point1 = {"x": 40, "y": 25}
        hero.command(friend, "move", point1)
        if friend.distanceTo(finalVector) < 2:
            hero.command(friend, "move", finalVector)
        else:
            point2 = {"x": 34, "y": 35}
            hero.command(friend, "move", point2)
            if friend.distanceTo(finalVector) < 2:
                hero.command(friend, "move", finalVector)
            else:
                point3 = {"x": 40, "y": 45}
                hero.command(friend, "move", point3)
                if friend.distanceTo(finalVector) < 2:
                    hero.command(friend, "move", finalVector)
                else:
                    point4 = {"x": 48, "y": 37}
                    hero.command(friend, "move", point4)
                    if friend.distanceTo(finalVector) < 2:
                        hero.command(friend, "move", finalVector)

#22

hmm, I think I get what you are trying to do, but the peasant still might bump the ball on accident when she moves to point1.

Also, she doesn’t have enough time to get to point1 before she gets commanded to move to point2. Since move doesn’t wok like moveXY she will never get to point1

Have you done the level Broken Circles? I used the idea from that level to move around the ball in a circle and decide when to move towards the center.


#23

She only goes to point1 then stops. Why is this happening?


#24

I am confused. The peasant still only moves to point1 and stops. Some help please?

Here is my code:

Thanks!
(Click on image to magnify)

@Mr-Borges please respond


#25

Try putting the vector stuff and ball info inside the loop.


#26

Does it work? @kaiduck


#27

Every single thing is already inside the loop. Seriously, can I have some real help like @Mr-Borges gave to me?


#28

Sorry I haven’t been on in a long time. I’m a school teacher and I was on summer vacation :slight_smile:

Now I’m back at school and my students are all doing code combat. :slight_smile:

@Neel_Sharma can you please cut and paste your code and format it with the three tick marks?

I’ll actually run your code and see what it is doing.


#29

He means format your code according to the FAQ. Also you need to give more information about what is happening, if there are any error messages etc.


#30

Yes @Luke10 thank you.


#31

I already know how to format, thank you.
Here is my code (In the post that I made above there is the same code, and it shows the error too.):

loop:
    ball = hero.findNearest(hero.findByType("ball"))
    ballPos = ball.pos
    enemy = hero.findNearestEnemy()
    friends = hero.findFriends()
    friend = hero.findNearest(friends)
    heroPos = hero.pos
    nubPos = enemy.pos
    newVector = Vector.subtract(nubPos, ballPos)
    direction = Vector.normalize(newVector)
    dm = Vector.multiply(direction, -3)
    finalVector = Vector.add(ballPos, dm)
    flag = hero.findFlag("black")
    if flag:
        point1 = {"x": 40, "y": 25}
        hero.command(friend, "move", point1)
        if friend.distanceTo(finalVector) < 2:
            hero.command(friend, "move", finalVector)
        else:
            point2 = {"x": 34, "y": 35}
            hero.command(friend, "move", point2)
            if friend.distanceTo(finalVector) < 2:
                hero.command(friend, "move", finalVector)
            else:
                point3 = {"x": 40, "y": 45}
                hero.command(friend, "move", point3)
                if friend.distanceTo(finalVector) < 2:
                    hero.command(friend, "move", finalVector)
                else:
                    point4 = {"x": 48, "y": 37}
                    hero.command(friend, "move", point4)
                    if friend.distanceTo(finalVector) < 2:
                        hero.command(friend, "move", finalVector)
                        hero.command(friend, "move", ballPos)

Thanks


#32

It is a little hard for me to figure out what you are trying to do. I think you are wanting to time your peasant movement with a black flag. Have her move in a diamond shape around the ball and then move into kicking position when she gets close.

hero.command(friend, "move", point1)

This command does not change the peasant’s pos to be point1, it only makes them take one step towards point1.

What I mean is, you need to be able to just get your peasant to move around the ball at all before you’ll be able to kick in the right direction. Maybe just work on that one piece first.

if friend.distanceTo(finalVector) < 2:
    hero.command(friend, "move", finalVector)

2 is a very small distance, the 4 points you are using are much more than 2 units apart

hero.command(friend, "move", finalVector)
hero.command(friend, "move", ballPos)

I think these two should always go together. Once she is at finalVector she should always do a kick by moving to ballPos. Problem is again, these two commands will not make the peasant be positioned at finalVector and then be positioned at ballPos. These commands will only make her move one time towards those positions.


#33

Finished level thanks!


#34

So do you have nalfar yet


#35

We can buy Nalfar without completing the levels.


#36

so do you have nalfar?


#37

Of course! I had him before finishing this lvl.


#38

me too. (20 characters)


#39

Nice! :wink: (20 characters)


#40

yay my bad forgot you can just buy it