Hello I’m still a beginner in Code Combat so maybe one of you more experienced users could point me in the right direction. As you will see in this level you need to patrol moving from X to X. My problem is that for some reason ( that might be obvious ) I can’t make my character move to the bottom X. I have tried to change the order and re-writing my code but I still have the same problem.
Please let me know if you can’t go to the level or if you do not understand something this is my first post so there could be an error. Thanks!
…
def checkAndAttack(target):
# The ‘target’ parameter is just a variable!
# It contains the argument when the function was called.
if target:
hero.attack(target)
hero.moveXY(43, 34)
while True:
hero.moveXY(58, 52)
topEnemy = hero.findNearestEnemy()
# Using the checkAndAttack function with the topEnemy variable.
checkAndAttack(topEnemy)
while True:
# Move to the bottom X mark.
hero.moveXY(58, 16)
# Create a variable named bottomEnemy and find the nearest enemy.
bottomEnemy = hero.findNearestEnemy()
# Use the checkAndAttack function, and include the bottomEnemy variable.
checkAndAttack(topEnemy)
…
def checkAndAttack(target):
# The 'target' parameter is just a variable!
# It contains the argument when the function was called.
if target:
hero.attack(target)
hero.moveXY(43, 34)
while True:
hero.moveXY(58, 52)
topEnemy = hero.findNearestEnemy()
# Using the checkAndAttack function with the topEnemy variable.
checkAndAttack(topEnemy)
while True:
# Move to the bottom X mark.
hero.moveXY(58, 16)
# Create a variable named bottomEnemy and find the nearest enemy.
bottomEnemy = hero.findNearestEnemy()
# Use the checkAndAttack function, and include the bottomEnemy variable.
checkAndAttack(topEnemy)
You don’t need to define a variable when it’s inside a function (if the variable is contained as an argument within the function’s brackets). He is defining target here:
The reason your hero isn’t moving to the bottom X is because you’ve used two while true loops. Unless you use something to stop a while true loop, e.g. break (which is covered in later levels), then the code in the second while true loop won’t play.
@Deadpool198 thank you for taking the time to help me with my problem. Here in my country Chile I don’t know anyone who knew how to do this, so thank you again
# This shows how to define a function called cleaveWhenClose
# The function defines a parameter called `target`
def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
while True:
enemies = hero.findEnemies()
if enemy:
hero.isReady("cleave")
hero.cleave(enemies)
# else, just attack `target`!
hero.attack(enemy)
hero.attack(enemy)
# This code is not part of the function.
while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
cleaveWhenClose(enemy)
You don’t need the while True in the function because the function is already in the loop. Also your hero.isReady('cleave') is not in a if statement.
Here’s what I would do
if target:
if hero.isReady('cleave'):
hero.cleave(target)
else:
hero.attack(target)