Metal Detector(金属探测器)
# The artillery uses coins as a target.
# You'll be the rangefinder for the artillery.
# Write the function.
def coinDistance():
# Find the nearest coin,
coin = hero.findNearestItem()
# If there is a coin, return the distance to it.
if coin:
distance = hero.distanceTo(coin)
return distance
# Else, return 0 (zero).
return 0
pass
while True:
distance = coinDistance()
if distance > 0:
# Say the distance.
hero.say(distance)
pass
流星雨
本章我们开始学习分类的内容。
# Pick up coins only if they are closer than 20m.
# Pick up all gems.
while True:
item = hero.findNearestItem()
distance = hero.distanceTo(item)
# If the item's type is "gem"
# OR the distance to the item less than 20 meters:
if item.type == 'gem' or distance < 20:
# Move to item's position.
hero.moveXY(item.pos.x, item.pos.y)
春雷
# Certain coins and gems attract lightning.
# 这个英雄应只收集银币和蓝宝石
while True:
item = hero.findNearestItem()
# A silver coin has a value of 2.
# Collect if item.type is equal to "coin"
# AND item.value 相等于2
if item.type == "coin" and item.value == 2:
hero.moveXY(item.pos.x, item.pos.y)
# 一个蓝宝石价值10
# Collect if item.type is equal to "gem"
# AND item.value is equal to 10.
if item.type == "gem" and item.value == 10:
hero.moveXY(item.pos.x, item.pos.y)
Forest Shadow(森林阴影)
# Big ogres can't see you in the forest.
# Attack only the small ogres in the forest.
# Collect coins and gems only.
# Don't leave the forest and don't eat/drink anything.
while True:
# Find the nearest enemy.
enemy = hero.findNearestEnemy()
# Attack it only if its type is "thrower" or "munchkin".
if enemy.type == "thrower" or enemy.type == "munchkin":
hero.attack(enemy)
# Find the nearest item.
item = hero.findNearestItem()
# Collect it only if its type is "gem" or "coin".
if item.type == "gem" or item.type == "coin":
hero.moveXY(item.pos.x, item.pos.y)
pass
Teleport Lasso(传送套锁)
# Our wizards teleport ogres from their camp here.
# They appear for a short period and they are stunned.
# Attack only weak and near ogres.
while True:
enemy = hero.findNearestEnemy()
distance = hero.distanceTo(enemy)
# If enemy.type is "munchkin"
# AND the distance to it is less than 20m
if enemy.type == "munchkin" and distance < 20:
# Then attack it.
hero.attack(enemy)
平常的一天
# 打败食人魔,收集金币。一切都那么平常。
# 使用 与(AND) 在同一行检查存在性和类型。
while True:
enemy = hero.findNearestEnemy()
# 有了与(AND),只在敌人存在时检查类型
if enemy and enemy.type == "munchkin":
hero.attack(enemy)
# 寻找最近的物品
item = hero.findNearestItem()
# 如果有名为 “coin” (金币)的物品存在,那就快去收集它!
if item and item.type == "coin":
hero.moveXY(item.pos.x, item.pos.y)
穿越
# Don't insult this tribe of peaceful ogres.
while True:
item = hero.findNearestItem()
if item:
# If item.type IS NOT EQUAL TO "gem"
if item.type != "gem":
# 然后跟随你的宠物。
hero.moveXY(pet.pos.x, pet.pos.y)
# 否则:
else:
# 移动到宝石的坐标。
hero.moveXY(item.pos.x, item.pos.y)
Brawler Hunt(狩猎斗士)
# Don't worry about small and medium-sized ogres.
# Your targets are type "brawler".
# When a "brawler" is closer than 50m, fire artillery.
while True:
# Find the nearest enemy and the distance to it.
enemy = hero.findNearestEnemy()
distance = hero.distanceTo(enemy)
# If the enemy's type is "brawler"
# AND the distance to it is less than 50 meters,
# Then say "Fire!" to signal the artillery.
if enemy.type == "brawler" and distance < 50:
hero.say("Fire!")
pass