My peasant is NOT Ronaldo (Precision Kick)

So, the idea of the code is to go around the ball in a circle and then when it lines up with a skeleton it kicks the ball. However, halfway through it just keeps kicking the ball to the right even though the skeleton is already dead! Is my peasant just drunk or something?

Screenshot

Screenshot 2024-03-13 10.47.36 AM

Code:

// Push the ball to knock over all the blue skeletons without hitting any red ones.
// The blue skeletons can be found as enemies.
function almostEqual(a, b){
    return Math.abs(a - b) < 0.02
}
var peasant = hero.findByType("peasant")[0];
var ball = hero.findByType("ball")[0];
var enemies = hero.findEnemies();
function killSkeletons(i){
    var enemy = enemies[i]
    var angle = 0;
    hero.command(peasant, "move", {'x': 35, 'y': 35})
    hero.wait(1.5);
    if (i > -1 && enemy){
        var ballToSkeleton = Vector.subtract(enemy.pos, ball.pos);
        var peasantToBall = Vector.subtract(ball.pos, peasant.pos);
        if(ballToSkeleton.heading() != peasantToBall.heading()){
            if (ball.velocity.magnitude() < 1) {
                while (true){
                    ballToSkeleton = Vector.subtract(enemy.pos, ball.pos);
                    peasantToBall = Vector.subtract(ball.pos, peasant.pos);
                    let newX = ball.pos.x + (5 * Math.cos(angle));
                    let newY = ball.pos.y + (5 * Math.sin(angle));
                    hero.command(peasant, "move", {'x': newX, 'y': newY});
                    angle += 0.01
                    if (almostEqual(ballToSkeleton.heading(), peasantToBall.heading())){
                        break;
                    }
                }
            }
            else {
                
            }
        }
        hero.command(peasant, "move", {'x': 40, 'y': 35});
        hero.wait(1);
        killSkeletons(i - 1);
    }
}

killSkeletons(enemies.length - 1);

The code is a little old so if you ask what something does it might take a while for me to figure it out because I kinda forgot lol.

ya last night all the peasents went to a big party just wait till he’s sober again :rofl: :rofl: :rofl:

4 Likes

I can’t really help u bc I have no clue how JS works. Srry. :frowning_face:

You’re good. :+1:

1 Like

Since the condition for the while loop is true, it will keep rotating the ball even after the skeleton is dead if it doesn’t align perfectly.

while (true) {
    ballToSkeleton = Vector.subtract(enemy.pos, ball.pos);
    peasantToBall = Vector.subtract(ball.pos, peasant.pos);
    let newX = ball.pos.x + (5 * Math.cos(angle));
    let newY = ball.pos.y + (5 * Math.sin(angle));
    hero.command(peasant, "move", { 'x': newX, 'y': newY });
    angle += 0.01;
    if (almostEqual(ballToSkeleton.heading(), peasantToBall.heading())) {
        break;
    }
    if (angle >= Math.PI * 2) {
        break; # Break the loop if a full circle is completed
    }
}

This addition of if (angle >= Math.PI * 2) will ensure that the while loop breaks if a full circle is completed without aligning the ball and the skeleton. This way, the peasant won’t keep kicking the ball unnecessarily.

Also, there are some errors with your angle calculation, which makes the ball hit into red skeletons.

P.S how do you make the format into Javascript?

to format to javascript just write javascript right after the three ticks like this

```javascript
code goes here

```

Anyways, I figured it out. The peasant always moves to the left after kicking and I set the angle wrong. Since it’s on the left the initial angle should be pi not 0. So, I just need to figure out how to make the peasant go faster since the peasant is too slow.

Edit: I beat the level

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.