# Precision kicking help!

**URL:** <https://discourse.codecombat.com/t/precision-kicking-help/10605>\
**Category:** Level Help\
**Created:** [February 18, 2017, 8:04am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605 "2017-02-18T08:04:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [February 18, 2017, 8:04am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/1 "2017-02-18T08:04:48Z")

</div>

I need help on how I can move the peasant to kick the ball so it won’t hit the red skeleton.  
But my code I use flags. Can you tell me how I can make my peasant move without flag to hit blue skeleton?

```python
def commandPeasant():
    flag = self.findFlag()
    friends = self.findFriends()
    for friend in friends:
        enemy = self.findNearestEnemy()
        
        if friend.type == "skeleton":
            continue
            
            
        if flag:
            self.command(friend, "move", flag.pos)
            pass       
        
while True:
    commandPeasant()

```

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 18, 2017, 1:53pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/2 "2017-02-18T13:53:59Z")

</div>

> [@Chaboi\_3000](#):
>
> can I have code for precision kicking?

Sorry Chaboi\_300, We cannot give out the code. It makes it easier for you to just get the code, then beat the level. But, if you post your code, we can help you along and give you little tips and clues to assist you. Remember to post your code according to the FAQ.  
Here’s the link:  
[https://discourse.codecombat.com/faq](https://discourse.codecombat.com/faq)

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [February 20, 2017, 8:26am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/3 "2017-02-20T08:26:19Z")

</div>

I need help on how I can move the peasant to kick the ball so it won’t hit the red skeleton.  
But my code I use flags. Can you tell me how I can make my peasant move without flag to hit blue skeleton?

```python
def commandPeasant():
    flag = self.findFlag()
    friends = self.findFriends()
    for friend in friends:
        enemy = self.findNearestEnemy()
        
        if friend.type == "skeleton":
            continue
            
            
        if flag:
            self.command(friend, "move", flag.pos)
            pass       
        
while True:
    commandPeasant()

```

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [February 20, 2017, 8:44am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/4 "2017-02-20T08:44:28Z")

</div>

It’s better to use vectors. Make a vector from the skeleton to the ball, then extend it little further. That is your (peasant) point. Then send the peasant from there to the ball. repeat.

---

<div class="post-metadata">

**Author:** ![Neel\_Sharma](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@Neel\_Sharma](https://discourse.codecombat.com/u/Neel_Sharma)\
**Post date:** [June 29, 2017, 1:42pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/5 "2017-06-29T13:42:53Z")

</div>

Is it also possible to find the angle of the vector, move the peasant behind the ball in that line and repeat that?

---

<div class="post-metadata">

**Author:** ![Minh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/minh/32/6856_2.png) [@Minh](https://discourse.codecombat.com/u/Minh)\
**Post date:** [June 30, 2017, 9:04am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/6 "2017-06-30T09:04:39Z")

</div>

Yes if you are smart enough.🙂

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [July 1, 2017, 10:25pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/7 "2017-07-01T22:25:30Z")

</div>

This code won’t move the peasant to pos and only goes to ballPos:

```python
# The blue skeletons can be found as enemies.
ball = hero.findNearest(hero.findByType("ball"))
    
ballPos = ball.pos
enemy = hero.findNearestEnemy()
friends = hero.findFriends()
friend = hero.findNearest(friends)
Vector = enemy.pos
distance = Vector.subtract(Vector, ballPos)

pos=Vector.add(distance,5)
while True:
    
    
    
    hero.command(friend, "move", pos)
    
    hero.command(friend, "move", ballPos)
    

```

---

<div class="post-metadata">

**Author:** ![Neel\_Sharma](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@Neel\_Sharma](https://discourse.codecombat.com/u/Neel_Sharma)\
**Post date:** [July 4, 2017, 3:38pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/8 "2017-07-04T15:38:40Z")

</div>

@Chaboi_3000 Can you give us a screenshot of where Alianor moves?

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [July 4, 2017, 5:25pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/9 "2017-07-04T17:25:44Z")

</div>

I told you only on the ball and no where else. @Bryukh is this a bug?

---

<div class="post-metadata">

**Author:** ![Minh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/minh/32/6856_2.png) [@Minh](https://discourse.codecombat.com/u/Minh)\
**Post date:** [July 5, 2017, 5:54am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/10 "2017-07-05T05:54:42Z")

</div>

I don’t think it a bug but I could be wrong.

---

<div class="post-metadata">

**Author:** ![Minh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/minh/32/6856_2.png) [@Minh](https://discourse.codecombat.com/u/Minh)\
**Post date:** [July 5, 2017, 6:08am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/11 "2017-07-05T06:08:21Z")

</div>

I think these two part of the code make the peasant move to the ball - `ballPos = ball.pos` `hero.command(friend, "move", ballPos)` I’m not aways accurate.

---

<div class="post-metadata">

**Author:** ![Mr-Borges](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mr-borges/32/6499_2.png) [@Mr-Borges](https://discourse.codecombat.com/u/Mr-Borges)\
**Post date:** [July 5, 2017, 8:01pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/12 "2017-07-05T20:01:46Z")

</div>

Hi @Chaboi_3000,

I tried your code and saw the problem. I think “Vector” is a reserved word so trying to use it as a variable name causes problems. (unfortunately without any error or warning)

When I changed your code to use some other variable name it got un-stuck and then correctly gave a warning for the line

```python
pos=Vector.add(distance,5)

```

The add function adds two vectors like this Vector.add(vector1,vector2), so you can add two vectors, but you can not add a scalar to a vector.

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [July 5, 2017, 9:58pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/13 "2017-07-05T21:58:28Z")

</div>

So can you tell me how to fix that?

---

<div class="post-metadata">

**Author:** ![Mr-Borges](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mr-borges/32/6499_2.png) [@Mr-Borges](https://discourse.codecombat.com/u/Mr-Borges)\
**Post date:** [July 6, 2017, 3:07am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/14 "2017-07-06T03:07:16Z")

</div>

It was a pretty challenging level for me, I planned to just help you find your problem, but I’ll try to give you the idea I used.

When you subtract the ball position vector from the enemy position vector you are getting a direction not a distance . In your code I’m talking about this part.

```python
distance = Vector.subtract(Vector, ballPos)

```

(The variable name Vector is a problem, use some other name.)  
Your distance vector is the right direction you want to send your peasant (from the ball) but not the right distance. It is too long.

I think you picked 5 because you want to send your peasant 5 units away from the ball (in the correct direction, which you already have). To do that you need to make your vector have a length of 5 with out changing its direction.

To change a vector to a specific length without changing its direction you can Vector.normalize() it to give it a length of one, then Vector.multiply() it by 5 or whatever you like to give it that length.

Once you have done all of that, the direction and length will be correct, but the starting place is not. (You will still have to Vector.add() your vector to the ball position vector to get the spot to send your peasant.

Once I did all of that I still had the problem of the peasant sometimes walking into the ball on its way to that spot. That is another part of the problem to solve.

---

<div class="post-metadata">

**Author:** ![Neel\_Sharma](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@Neel\_Sharma](https://discourse.codecombat.com/u/Neel_Sharma)\
**Post date:** [July 7, 2017, 4:56pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/16 "2017-07-07T16:56:59Z")

</div>

I need help on this level. I am extremely confused and I need help. Alianor (the peasant) moves in directions I don’t want her to.  
Thanks!

```python
# 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
    Vector = enemy.pos
    newVector = Vector.add(Vector, ballPos)
    newVector1 = Vector.limit(newVector, 1)
    #pVector = Vector.copy(newVector)
    #pVector = Vector.rotate(pVector, Math.PI)
    #hero.debug(x)
    #hero.debug(m)
    #hero.debug(x1)
    #hero.debug(m1)
    #hero.debug(newVector)
    #hero.debug(newVector1)
    hero.command(friend, "move", newVector)
    flag = hero.findFlag(flag)
    if flag:
        target = {"x": ballPos.x, "y": ballPos.y}
        hero.command(friend, "move", target)

```

Later:

I edited my code using @Mr-Borges suggestion as well since I did almost the same thing as @Chaboi_3000

Here it is:

```python
# 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
    Vector = enemy.pos
    newVector = Vector.subtract(Vector, ballPos)
    direction = Vector.normalize(newVector)
    dm = Vector.multiply(direction, 3)
    finalVector = Vector.add(ballPos, dm)
    hero.command(friend, "move", finalVector)
    flag = hero.findFlag(flag)
    if flag:
        target = {"x": ballPos.x, "y": ballPos.y}
        hero.command(friend, "move", target)

```

It still doesn’t work. Can I have some other help?

---

<div class="post-metadata">

**Author:** ![Neel\_Sharma](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@Neel\_Sharma](https://discourse.codecombat.com/u/Neel_Sharma)\
**Post date:** [July 7, 2017, 5:24pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/17 "2017-07-07T17:24:20Z")

</div>

I did practically the same thing as Chaboi\_3000 except my thing works kinda odd. I think that the problem is with the normalizing.

---

<div class="post-metadata">

**Author:** ![Mr-Borges](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mr-borges/32/6499_2.png) [@Mr-Borges](https://discourse.codecombat.com/u/Mr-Borges)\
**Post date:** [July 8, 2017, 5:24am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/18 "2017-07-08T05:24:24Z")

</div>

Hi @Neel_Sharma, I tried your program and it is pretty close to working.

Your peasant is moving a distance of 3 units from the ball towards the skeleton. Instead of +3 I think you want to move the _opposite_ direction. (hint hint)

I think you are just using the flag just for timing, so the peasant finishes moving to finalVector before she moves towards the ball. You can do it that way, but you’ll need to pick up the flag after you use it and then have another way to control the timing of her moving away from the ball.

---

<div class="post-metadata">

**Author:** ![Neel\_Sharma](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@Neel\_Sharma](https://discourse.codecombat.com/u/Neel_Sharma)\
**Post date:** [July 9, 2017, 5:24pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/19 "2017-07-09T17:24:15Z")

</div>

@Mr-Borges I tried it. It almost worked, but I believe that it is trying to hit a friendly skeleton. 😄

Here is my code:

```python
# 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
    Vector = enemy.pos
    newVector = Vector.subtract(Vector, ballPos)
    direction = Vector.normalize(newVector)
    dm = Vector.multiply(direction, -3)
    finalVector = Vector.add(ballPos, dm)
    hero.command(friend, "move", finalVector)
    flag = hero.findFlag("black")
    if flag:
        target = {"x": ballPos.x, "y": ballPos.y}
        hero.command(friend, "move", target)
        if friend.pos == flag.pos:
            hero.removeFlag(flag)

```

---

<div class="post-metadata">

**Author:** ![Mr-Borges](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mr-borges/32/6499_2.png) [@Mr-Borges](https://discourse.codecombat.com/u/Mr-Borges)\
**Post date:** [July 11, 2017, 2:39am UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/20 "2017-07-11T02:39:54Z")

</div>

I’ll point you to something I said about this earlier.

> [@Mr-Borges](#):
>
> I think “Vector” is a reserved word so trying to use it as a variable name causes problems. (unfortunately without any error or warning)

The name “Vector” is already in use, as a class I think. The problem lines are these.

**Vector** = enemy.pos

and

newVector = Vector.subtract( **Vector** , ballPos)

These lines are not doing what you would think because the name “Vector” already belongs to a special thing that you can’t mess with. I used hero.say(newVector) to see what was actually getting saved here and newVector was always 0,0

Then I changed the variable name “Vector” to some other name and then newVector wasn’t zero anymore and the program started to work.

---

<div class="post-metadata">

**Author:** ![Neel\_Sharma](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@Neel\_Sharma](https://discourse.codecombat.com/u/Neel_Sharma)\
**Post date:** [July 12, 2017, 5:17pm UTC](https://discourse.codecombat.com/t/precision-kicking-help/10605/21 "2017-07-12T17:17:07Z")

</div>

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:

```python
# 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)

```

[Next page](https://discourse.codecombat.com/t/precision-kicking-help/10605.md?page=2)
