Hi, I’ve tried to complete this level but I’m having trouble with it as my code is not working. I already read the above solutions but nothing seems to be working so a little help would be greatly appreciated.
This is my code:
Move your pet to the left or right button as needed.
def onHear(event):
# Find the guards to listen to.
archer = pet.findNearestByType(“archer”)
soldier = pet.findNearestByType(“soldier”)
# If event.speaker is the archer:
if event.speaker == ‘archer’:
# Move to the left button.
pet.moveXY(32, 30)
# If event.speaker is the soldier:
if event.speaker == ‘soldier’:
# Move to the right button.
pet.moveXY(48, 30)
pet.on(“hear”, onHear)
You don’t have to change the code below.
Your hero should protect the bottom right passage.
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
First, 2 things, please refrain from posting in an already solved thread. Second, welcome to the CodeCombat Discourse! This is a cozy place where you can ask for help, get hints, and maybe just have fun! Before you proceed, please review this topic.
Hi @K_P, welcome to the Discourse!
After following the link in @Yisrael_Hefter’s reply, please learn how to format your code correctly, then reformat your code so that it’s easier for us to help you and post it in a new reply (or edit your previous response and tag me @MarmiteOnToast to get my attention).
Hi @MarmiteOnToast
I just learnt how to format the code properly. Here it is. I have the vision glasses on that help the player see through walls as thats what people have said. However, it still doesn’t work and help would be greatly appreciated.
# Move your pet to the left or right button as needed.
def onHear(event):
# Find guards to listen them.
archer = pet.findNearestByType("archer")
soldier = pet.findNearestByType("soldier")
# If the speaker is the archer:
if event.speaker == archer:
# Move to the left button.
pet.moveXY(32, 30)
# If the speaker is the soldier:
if event.speaker == soldier:
# Move to the right button.
pet.moveXY(48, 30)
pet.on("hear", onHear)
# You don't have to change the code below.
# Your hero should protect the bottom right passage.
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
Here is my code for the pet engineer level on CS3 (python).
def onHear(event):
# Find the guards to listen to.
archer = pet.findNearestByType(“archer”)
soldier = pet.findNearestByType(“soldier”)
# If event.speaker is the archer:
if event.speaker == archer:
# Move to the left button.
pet.moveXY(32, 30)
# If event.speaker is the soldier:
if event.speaker == soldier:
# Move to the right button.
pet.moveXY(48, 30)
pet.on(“hear”, onHear)
You don’t have to change the code below.
Your hero should protect the bottom right passage.
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
Hi @Priyanka_Paul, welcome to the Discourse! This is a friendly place to chat and ask for help on CodeCombat levels (remember to format your code correctly). Before you continue, don’t forget to read the guidelines here if you haven’t already.
We hope you enjoy your time here!
(@)TheCodingCrusader22, the ‘don’t post unless’ message for solved topics does say that it’s ok to reply if the solution doesn’t work for someone
def onHear(event):
# Find the guards to listen to.
archer = pet.findNearestByType(“archer”)
soldier = pet.findNearestByType(“soldier”)
# If event.speaker is the `archer`:
if event.speaker == "archer":
# Move to the left button.
pet.moveXY(32, 30)
# If event.speaker is the `soldier`:
if event.speaker == "soldier":
# Move to the right button.
pet.moveXY(48, 30)
pet.on(“hear”, onHear)
while True:
enemy = hero.findNearestEnemy()
if enemy and hero.distanceTo(enemy) < 5:
hero.attack(enemy)
my code is not working what seems to be the problem??