Noob help - Closing the Distance - python - what am I doing wrong?


#1

I did the level “Closing the Distance” and not happy with how I “passed” the level. I mean, I passed, but it’s VERY messy. This is what I did.

loop:
self.moveRight()

You should recognize this from the last level.

enemy = self.getNearestEnemy()

Now attack the variable.

self.attack(enemy)

self.moveDown()
self.moveRight()

Pretty much - I inched my way across the battle field. What’s a more proper way of writing this code?


#2

It might be better to not use a loop here, because the movements needed aren’t obviously the same in the two loop iterations. That said, nice thinking for using one!

Soon I’ll be adjusting it such that when you attack an enemy, you return to your starting position before moving on, so it should be less messy.


#3

Thanks Nick! I finally finished all the beginner courses! I look forward to beginner-intermediate courses! ^^ This is really inspiring me to go back to college and get my degree in programming. Once I got a hold of the “loop” and "self.moveXY(.__), really bloomed! :slight_smile:

Thanks again!


#4

Awesome–stay tuned for a bunch more levels!


#5

I have been having a lot of difficulty with this level. I am able to kill the first ogre, but no matter how I code to kill the second ogre my avatar just stands there and lets it kill him. Any suggestions would really help! My code looks something like this:

self.moveRight();
target= self.findNearestEnemy();
self.attack(target);
self.attack(target);
self.moveDown();
self.moveRight();
self.moveRight();
self.moveUp();
self.findNearestEnemy();
self.attack(target);
self.attack(target);


#6

Hey @Josiah, sorry for the difficulty. We’re going to try to make this part easier soon. The problem is that you need to assign the result of self.findNearestEnemy() to the target variable. Otherwise, you still have the old target and are trying to attack that instead of trying to attack the new nearest enemy.

To assign target again, look at how you assigned it the first time:

target = self.findNearestEnemy()

You’ll want to do that again before you attack the second ogre, once you can see her.


#7

I can not get my guy to make the first right on this.


#8

Could you give us some code so we can help, @Chi96?


#9

I am working in Java. My browser is Firefox. I used all the codes I have learn so far and nothing happens.

this.moveRight();

// You should recognize this from the last level.
enemy = this.findNearestEnemy();
// Now attack the variable.

// And there's another room to clear!

Give code: Line 1, time 0.0: tmp2[tmp3] is not a function

I’ve tried
this.moveXY(11, 34);
I’ve tried
this.say("Hey there!");
I’ve tried every code I know and the guy won’t move or say anything.


#10

Do you have the simple boots and crude glasses equipped? (You will also need the longsword).


#11

Thanks. It was the boots.


#12

Also, while this is probably considered a minor detail, you are using JavaScript, not Java. JavaScript is a Scripting language (duh :wink: ) used in websites along with html and css. Java is full programming language, which is origanally designed to be used on any computer, and is actually quite different, (and, in my opinion, harder to learn,) than JavaScript.


#13

Hi,
I manage to complete this level but my hero keeps doing a loop and returning to kill the second ogre, then says But he’s already dead!

I’ve used
hero.moveRight()
enemy1 = hero.findNearestEnemy()
hero.attack(enemy1)
hero.attack(enemy1)
hero.moveRight()
enemy2 = hero.findNearestEnemy()
hero.attack(enemy2)
hero.attack(enemy2)
hero.moveRight()

The hero attacks and kills the second ogre, and then it should just go right, but it doesn’t. Any idea why?