Этом уровень покажет, как определять собственные функции.
Код внутри функции не выполняется сразу. Он откладывается напото.
Эта функция заставляет вашего героя поднять ближайшую монету.
def pickUpNearestCoin():
items = self.findItems()
nearestCoin = self.findNearest(items)
if nearestCoin and self.distanceTo(nearestCoin)<30:
if self.isReady(“jump”):
self.jumpTo(nearestCoin.pos)
else:
self.move(nearestCoin.pos)
С помощью этой функции ваш герой призывает солдата.
def summonSoldier():
# Заполни код здесь, что призвать солдата, если у тебя достаточно золота.
if self.gold > self.costOf(“soldier”):
self.summon(“soldier”)
Эта функция приказывает вашим солдатам атаковать ближайшего враг.
def commandSoldiers():
for soldier in self.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
self.command(soldier, “attack”, enemy)
def attack(target):
if target:
if(self.isReady(“jump”) and self.distanceTo>10):
self.jumpTo(target.pos)
elif(self.isReady(“bash”)):
self.bash(target)
hero.attack(enemy)
else:
self.attack(target)
def tacktick():
enemies = self.findEnemies()
nearest = self.findNearest(enemies)
friends = self.findFriends()
if nearest and (self.distanceTo(nearest)<10 or self.now()>25):
attack(nearest)
elif nearest and len(friends)/3<len(enemies):
attack(nearest)
else:
pickUpNearestCoin()
while True:
# В своем цикле ты можешь "вызывать" функции, определенные выше.
# Эта строка вызывает выполнение кода внутри функции "pickUpNearestCoin" .
tacktick()
summonSoldier()
commandSoldiers()
But it wont work can anybody help please
kind regards
Sarah B :
Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort. Please read this topic and format your code again correctly
Этом уровень покажет, как определять собственные функции.
Код внутри функции не выполняется сразу. Он откладывается напото.
Эта функция заставляет вашего героя поднять ближайшую монету.
def pickUpNearestCoin():
items = self.findItems()
nearestCoin = self.findNearest(items)
if nearestCoin and self.distanceTo(nearestCoin)<30:
if self.isReady(“jump”):
self.jumpTo(nearestCoin.pos)
else:
self.move(nearestCoin.pos)
С помощью этой функции ваш герой призывает солдата.
def summonSoldier():
# Заполни код здесь, что призвать солдата, если у тебя достаточно золота.
if self.gold > self.costOf(“soldier”):
self.summon(“soldier”)
Эта функция приказывает вашим солдатам атаковать ближайшего враг.
def commandSoldiers():
for soldier in self.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
self.command(soldier, “attack”, enemy)
def attack(target):
if target:
if(self.isReady(“jump”) and self.distanceTo>10):
self.jumpTo(target.pos)
elif(self.isReady(“bash”)):
self.bash(target)
hero.attack(enemy)
else:
self.attack(target)
def tacktick():
enemies = self.findEnemies()
nearest = self.findNearest(enemies)
friends = self.findFriends()
if nearest and (self.distanceTo(nearest)<10 or self.now()>25):
attack(nearest)
elif nearest and len(friends)/3<len(enemies):
attack(nearest)
else:
pickUpNearestCoin()
while True:
# В своем цикле ты можешь "вызывать" функции, определенные выше.
# Эта строка вызывает выполнение кода внутри функции "pickUpNearestCoin" .
tacktick()
summonSoldier()
commandSoldiers()```
You have to copy the code from the game, not from here. That’s why there’s no indents in your formatted code. Proper indents are critical. Yes, you have used the triple back ticks and that’s great, but you need to copy the code from the game.
I straightened out your code and used it to pass - only made it 30 seconds and collected lots of gold but it did pass. There were many things I had to change. I’m going to PM the code to you. Go line by line to compare the changes in mine with what you have to understand the difference. Also, you need a faster sword. The one you’re using packs a big punch, but you need to swing faster against that many enemies. It doesn’t take much to kill them so you don’t need as much damage per swing, you just need to swing it faster.
def pickUpNearestCoin():
items = self.findItems() # This looks for an array. You can't move to array because it doesn't have a specific position.
nearestCoin = self.findNearest(items) # same as above. Try a simple, item = self.findNearestItem().
if nearestCoin and self.distanceTo(nearestCoin)<30: # Alter the remaining code to accommodate the variable changes above.
if self.isReady(“jump”):
self.jumpTo(nearestCoin.pos)
else:
self.move(nearestCoin.pos)
def attack(target): # (The target argument is not necessary if you define target in the function)
# target = self.findNearestEnemy()
if target:
if(self.isReady(“jump”) and self.distanceTo>10):
self.jumpTo(target.pos)
elif(self.isReady(“bash”)):
self.bash(target)
hero.attack(enemy) # You have not defined enemy. Did you mean target?
else:
self.attack(target)
def tacktick(): # I eliminated this function. It's not necessary. Just call the four other functions in a while True loop.
enemies = self.findEnemies()
nearest = self.findNearest(enemies)
friends = self.findFriends()
if nearest and (self.distanceTo(nearest)<10 or self.now()>25):
attack(nearest)
elif nearest and len(friends)/3<len(enemies):
attack(nearest)
else:
pickUpNearestCoin()
I only have the Simple Sword,Sharpened Sword,Long Sword and Kithsteel Blade but if you recommend one i dont have i can just save up and buy it.
kind regards @MunkeyShynes
You probably shoudn’t use the sword you have equipped in your screenshot. To my understanding and correct me if I am wrong, that sword requires more code than just the hero.attack code that you would use. There are a few more lines of code you need to be able to use the full power that the sword contains