Stillness in Motion Python

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        self.distanceTo(enemy) < 5
        self.attack(enemy)
    if enemy:
        self.distanceTo(enemy) > 5
        self.shield()
    else:
        self.moveXY(40, 34)

Hi.
This is my code for Stillness in motion. I have no clue what is wrong but it’s not working. Sorry if it’s not formatted right but I’m short on time and am in ICT class at school. If someone could lend a hand that’d be great.

The discourse FAQ specifically says “Read Before Posting”. If you didn’t read the FAQ, then you shouldn’t be posting. I will do it for you this time, but don’t post again until you have read the FAQ.

I’ll go through your code line by line:

Line 4: self..distanceTo(enemy) < something returns a boolean that states whether it is true or not. This should be an if-statement.

Line 7: See line 4.

2 Likes

What do you mean by it should be an if-statement. I have if enemy: as my if, but I end up walking over to the head hunters instead.

@ChronistGilver means instead of:

if enemy:
self.distanceTo(enemy) < 5:

It would turn into:

if enemy and self.distanceTo(enemy) < 5:

I finally got it. Thank you so very much.

1 Like

I still don’t under stand i’m stuck in the whole thing my guy keeps leaving his area

NM Chronist made sence and helped me thank

1 Like

Have you fixed it? If not please explain the situation more clearly, take your code from the game and post it into the discourse (formatted as stated in the FAQ (search FAQ on the discourse search bar))
and maybe also a screenshot.
Thanks :lion: :lion: :lion:

I think he did, it was good he Solved it

1 Like

k, (20 charactersAhheyy)

hey id like to ask how to get to the FAQ

FAQButton01

2 Likes

Still having problems with this in JavaScript, having the cursed Expected ‘)’ to match {b} from line 13 and instead saw “less”.

while(true) {
    var enemy = hero.findNearestEnemy();
    // If there is an enemy, then...
    if(enemy) {
        // Create a distance variable with distanceTo.
        var distance = hero.distanceTo(enemy);
        // If the distance is less than 5 meters, then attack.
         if distance is less than 5
      hero.attack(enemy);
            
        }
        // Else (the enemy is far away), then shield.
            hero.shield();
        }
    // Else (there is no enemy)...
 hero.moveXY(40, 34);
        // ... then move back to the X.
        hero.moveXY(40, 34);


Can someone tell me what’s wrong with my code? Thanks.

Hi hotpockets!
While indentation isn’t “necessary” in javascript, it usually helps alot when trying to figure out what you might want to change/fix and where!
This time, i’ve quickly “beautified” your code, so it’s a bit easier to read.

Beautified Code - Click to show
while(true) {
	var enemy = hero.findNearestEnemy();
	// If there is an enemy, then...
	if(enemy) {
		// Create a distance variable with distanceTo.
		var distance = hero.distanceTo(enemy);
		// If the distance is less than 5 meters, then attack.
		if distance is less than 5
			hero.attack(enemy);
	}
	// Else (the enemy is far away), then shield.
	hero.shield();
} // <----------!!! This is the end of the while(true) loop !!!
// Else (there is no enemy)...
hero.moveXY(40, 34);
// ... then move back to the X.
hero.moveXY(40, 34);

So regarding your error message first: the problem is most likely the line 8 with
if distance is less than 5
In javascript you have ( and ) around the condition of an if statement, so we’d first change it this way:
if(distance is less than 5)
But, sadly, that won’t be enough to make it work.
In order to check if something is less than, greater than, or equal to or similar, see the Info below

condition help - click to expand

Less than something
if( a < b) this checks if “a” is smaller than “b”
Less than, or equal to something
if( a <= b) this checks if “a” is smaller than “b” or has exactly the same value (the above one won’t make code run if a is 5 and b is 5!)
Greater than something
if( a > b) this checks if “a” is bigger than “b”
Greater than, or equal to something
if( a >= b) this checks if “a” is bigger than “b” or has exactly the same value
Equal to something
if(a == b) this checks if “a” is equal to (the same as) “B”


Additionally, some information regarding if statements aswell, that you might want to consider:

Click to expand information

While it is possible to have an if statement without curly brackets { and },
it is ONLY used, when you run a single line of code afterwards.
To keep it “beginner” friendly, you might want to consider using these brackets to start and end whatever you want to do, in case the condition is true!


Now, that i’ve also “beautified” your code a bit, do you notice how the 2 hero.moveXY commands are outside of the while(true) loop?
They’ll never execute, as you’ll be in the loop forever! So you’d need to implement them into the while(true) loop as well!

I hope this helps out, even though it might contain more information than necessary and sorry for the length of this answer!

java?sripct becausgjj

Hi, welcome to the COCO discourse. I’m a bit confused, please could you clarify your comment.
Thanks
Danny

please i need help really bad this is my code

var enemy = hero.findNearestEnemy();
// If there is an enemy, then…
if(enemy) {
// Create a distance variable with distanceTo.
var distance = hero.distanceTo(enemy);
// If the distance is less than 5 meters, then attack.
if(distance is less than 5)

     }
  hero.attack(enemy);
        
    }
    // Else (the enemy is far away), then shield.
        hero.shield();
        
    }
// Else (there is no enemy)...

hero.moveXY(40, 34);
// … then move back to the X.
hero.moveXY(40, 34);

im really stuck and need help

Please post your code properly formatted. We need this so we can ensure indentation/syntax are correct. You can learn how here: [Essentials] How To Post/Format Your Code Correctly