首页:https://cn.codecombat.com/play
语言:Python

第二界面:Sarven沙漠(43关)
时间:4-11小时
内容:算术运算,计数器,while循环,break(跳出循环),数组,字符串比较,寻找最小最大值。
网页:https://cn.codecombat.com/play/desert

闯关:
第1关:强壮的沙牦牛
子网页:https://cn.codecombat.com/play/level/the-mighty-sand-yak?

  1. # 当牦牛靠近时向右移动10米来躲避
  2. # 躲避4头牦牛完成此关
  3.  
  4. while True:
  5. # 使用你的灵石获取你当前的 x 和 y 位置。
  6. x = hero.pos.x
  7. y = hero.pos.y
  8.  
  9. # 找到最近的耗牛。
  10. yak = hero.findNearestEnemy()
  11.  
  12. # 使用 if 仅仅当牦牛少于10米距离的时候。
  13. if hero.distanceTo(yak) < 10:
  14. # 向右移动,添加10到你的x位置。
  15. x = x + 10
  16. # 使用 moveXY 来移动!
  17. hero.moveXY(x, y)
  18. pass

第2关:绿洲
子网页:https://cn.codecombat.com/play/level/oasis?

  1. # 向绿洲移动
  2. # Move left to avoid nearby yaks.
  3.  
  4. while True:
  5. x = hero.pos.x
  6. y = hero.pos.y
  7. enemy = hero.findNearestEnemy()
  8. if enemy and hero.distanceTo(enemy) < 10:
  9. # 通过在你的X坐标上减去10来移动到左边
  10. x = x - 10
  11. # Use moveXY to move to the new x, y position.
  12. hero.moveXY(x, y)
  13. pass
  14. else:
  15. # 通过在你的X坐标上加上10来移动到右边
  16. x = x + 10
  17. # Use moveXY to move to the new x, y position.
  18. hero.moveXY(x, y)
  19. pass

第3关:盆地的践踏
子网页:https://cn.codecombat.com/play/level/basin-stampede?

  1. # Keep moving right, but adjust up and down as you go.
  2.  
  3. while True:
  4. enemy = hero.findNearestEnemy()
  5. xPos = hero.pos.x + 5
  6. yPos = 17
  7. if enemy:
  8. # Adjust y up or down to get away from yaks.
  9. if enemy.pos.y > hero.pos.y:
  10. # If the Yak is above you, subtract 3 from yPos.
  11. yPos = yPos - 3
  12. pass
  13. elif enemy.pos.y < hero.pos.y:
  14. # If the Yak is below you, add 3 to yPos.
  15. yPos = yPos + 3
  16. pass
  17. hero.moveXY(xPos, yPos)

第4关:萨文路
子网页:https://cn.codecombat.com/play/level/sarven-road?

第一次:

  1. # 到达绿洲。小心新的敌人:食人魔侦察兵!
  2. # 通过添加你当前的X位置和Y位置以向上向右走
  3.  
  4. while True:
  5. # If there's an enemy, attack.
  6. enemy = hero.findNearestEnemy()
  7. xPos = hero.pos.x
  8. yPos = hero.pos.y
  9. if enemy:
  10. hero.attack(enemy)
  11. # Else, keep moving up and to the right.
  12. else:
  13. xPos = xPos + 5
  14. yPos = yPos + 5
  15. hero.moveXY(xPos, yPos)
  16. pass

第5关:十字路口
子网页:https://cn.codecombat.com/play/level/crossroads?

  1. # 使用 fire-trap 打败进攻的食人魔
  2.  
  3. while True:
  4. enemy = hero.findNearestEnemy()
  5. if enemy:
  6. # If the enemy is to the left of the hero:
  7. if enemy.pos.x < hero.pos.x:
  8. # 如果敌人从左边进攻,在左边建一个fire-trap(火焰陷阱)
  9. hero.buildXY("fire-trap", 25, 34)
  10. pass
  11. # If the enemy is to the right of the hero:
  12. elif enemy.pos.x > hero.pos.x:
  13. # 如果敌人从右边进攻,在右边建一个fire-trap(火焰陷阱)
  14. hero.buildXY("fire-trap", 55, 34)
  15. pass
  16. # If the enemy is below the hero:
  17. elif enemy.pos.y < hero.pos.y:
  18. # 如果敌人从下边进攻,在下边建一个fire-trap(火焰陷阱)
  19. hero.buildXY("fire-trap", 40, 19)
  20. pass
  21. # If the enemy is above the hero:
  22. elif enemy.pos.y > hero.pos.y:
  23. # 如果敌人从上边进攻,在上边建一个fire-trap(火焰陷阱)
  24. hero.buildXY("fire-trap", 40, 49)
  25. pass
  26. # Move back to the center.
  27. hero.moveXY(40, 34)

第6关:雷蹄
子网页:https://cn.codecombat.com/play/level/thunderhooves?

  1. # 到达绿洲,
  2. # 用栅栏引导砂牦牛到你去的地方
  3.  
  4. while True:
  5. yak = hero.findNearestEnemy()
  6. if yak:
  7. # 如果它的 y 值大于你的,那么耗牛在你前面
  8. if yak.pos.y > hero.pos.y:
  9. # 如果耗牛在你前面,在它后面10米建立一个栅栏
  10. hero.buildXY("fence", yak.pos.x, yak.pos.y - 10)
  11. else:
  12. if yak.pos.y < hero.pos.y:
  13. # 如果耗牛在你后面,在它前面10m 建立一个栅栏
  14. hero.buildXY("fence", yak.pos.x, yak.pos.y + 10)
  15. pass
  16. else:
  17. # 向右移动10走向绿洲
  18. hero.moveXY(hero.pos.x + 10, hero.pos.y)
  19. pass

第7关:截擎
子网页:https://cn.codecombat.com/play/level/interception?

  1. # Stand between the peasant and the tower.
  2.  
  3. while True:
  4. enemy = hero.findNearestEnemy()
  5. friend = hero.findNearestFriend()
  6. # Calculate x by adding friend.pos.x to enemy.pos.x
  7. # 然后除以2
  8. # Check the guide if you need more help!
  9. x = (friend.pos.x + enemy.pos.x) / 2
  10. # Now do the same for y
  11. y = (friend.pos.y + enemy.pos.y) / 2
  12. # 移动到您计算的X和Y坐标。
  13. hero.moveXY(x, y)

第8关:猎杀行动
子网页:https://cn.codecombat.com/play/level/operation-killdeer?

  1. # 引诱这些食人魔进入陷阱,这些食人魔非常小心
  2. # 只有英雄受伤了他们才会跟着英雄
  3.  
  4. # 这个函数会检查英雄的生命值
  5. # 并返回一个布尔型(Boolean)的值。
  6.  
  7. def shouldRun():
  8. if hero.health < hero.maxHealth / 2:
  9. return True
  10. else:
  11. return False
  12.  
  13. while True:
  14. enemy = hero.findNearestEnemy()
  15. # 当shouldRun()返回True时 移动到X点 True
  16. if shouldRun():
  17. hero.moveXY(75, 37)
  18. # 否则,攻击!
  19. else:
  20. hero.attack(enemy)

第9关:医疗注意
子网页:https://cn.codecombat.com/play/level/medical-attention?

  1. # 当你生命值少于一半时,请求医疗人员的治疗。
  2.  
  3. while True:
  4. currentHealth = hero.health
  5. healingThreshold = hero.maxHealth / 2
  6. enemy = hero.findNearestEnemy()
  7.  
  8. # 如果你当前的健康值少于下限,
  9. # 移动到治疗点说『heal me』
  10. # 否则的话,攻击。你需要战斗的更狠点!
  11. if currentHealth < healingThreshold:
  12. hero.moveXY(65, 46)
  13. hero.say("heal me")
  14. elif enemy:
  15. hero.attack(enemy)

第10关:跟上时间
子网页:https://cn.codecombat.com/play/level/keeping-time?

  1. # 使用你的新技能来选择你要做什么 hero.now()
  2.  
  3. while True:
  4. # 如果是头十秒,进攻。
  5. if hero.now() < 10:
  6. enemy = hero.findNearestEnemy()
  7. if enemy:
  8. hero.attack(enemy)
  9. pass
  10. # 反之,如果是前35秒,收集金币。
  11. elif hero.now() < 35:
  12. coin = hero.findNearestItem()
  13. if coin:
  14. hero.moveXY(coin.pos.x, coin.pos.y)
  15. pass
  16. # 后35秒,加入救助。
  17. else:
  18. enemy = hero.findNearestEnemy()
  19. if enemy:
  20. hero.attack(enemy)
  21. pass

第11关:Sarven哨兵
子网页:https://cn.codecombat.com/play/level/sarven-sentry?

  1. # 使用不同的颜色旗子来执行不同的任务。
  2.  
  3. while True:
  4. flagGreen = hero.findFlag("green")
  5. flagBlack = hero.findFlag("black")
  6. # 如果是绿色旗子,就建立一个栅栏。
  7.  
  8. if flagGreen:
  9. # Build a "fence" at flagGreen's position.
  10. hero.buildXY("fence", flagGreen.pos.x, flagGreen.pos.y)
  11. # 记住要捡起旗子,在你都完成之后!
  12. hero.pickUpFlag(flagGreen)
  13.  
  14. # 如果是黑色旗子,就建立一个火焰陷阱
  15. if flagBlack:
  16. # Build a "fire-trap" at flagBlack's position.
  17. hero.buildXY("fire-trap", flagBlack.pos.x, flagBlack.pos.y)
  18. # 记住要捡起旗子,在你都完成之后!
  19. hero.pickUpFlag(flagBlack)
  20.  
  21. # 回到中间。
  22. hero.moveXY(43, 31)

第12关:囤积黄金
子网页:https://cn.codecombat.com/play/level/hoarding-gold?

  1. # 收集25金币,然后告诉 Naria 总数
  2. # 当金币总数大于25,使用 break 来停止收集金币。
  3.  
  4. totalGold = 0
  5. while True:
  6. coin = hero.findNearestItem()
  7. if coin:
  8. # 捡起金币
  9. hero.moveXY(coin.pos.x, coin.pos.y)
  10. # 将金币的价值加进 totalGold.(查看帮助了解更多.)
  11. # 使用以下方法得到它的价值:: coin.value
  12. totalGold = totalGold + coin.value
  13. pass
  14. if totalGold >= 25:
  15. # 这会中断循环并且执行循环下面的语句
  16. # The loop ends, code after the loop will run.
  17. break
  18. # 完成收集金币!
  19. hero.moveXY(58, 33)
  20. # 去告诉 Naria 你收集了多少金币。
  21. hero.say(totalGold)

第13关:诱饵钻
子网页:https://cn.codecombat.com/play/level/decoy-drill?

  1. # 我们在测试一个新的战斗单位:诱饵。
  2. # 创建4个诱饵,然后汇报给 Naria
  3.  
  4. decoysBuilt = 0
  5. while True:
  6. coin = hero.findNearestItem()
  7. if coin:
  8. # 掠夺金币!
  9. hero.moveXY(coin.pos.x, coin.pos.y)
  10. pass
  11. # 每个诱饵消费25个金币。
  12. # 让它知道当你有超过25个金币的时候
  13. if hero.gold >= 25:
  14. # buildXY a "decoy"
  15. hero.buildXY("decoy", hero.pos.x, hero.pos.y)
  16. # 当你一直走的时候,保持统计你创建的诱饵的数量。
  17. decoysBuilt += 1
  18. if decoysBuilt == 4:
  19. # 当你创建了4个诱饵时跳出循环
  20. break
  21. pass
  22.  
  23. hero.say("完成创建诱饵!")
  24. hero.moveXY(14, 36)
  25. # 去找 Naria 并告诉她你创建了多少个诱饵。
  26. hero.say("I duilded "+decoysBuilt+" decoys !")

第14关:守书人
子网页:https://cn.codecombat.com/play/level/bookkeeper?

  1. # 奋战沙场15秒。
  2. # Keep count whenever an enemy is defeated.
  3.  
  4. defeated = 0
  5. while True:
  6. enemy = hero.findNearestEnemy()
  7. if enemy:
  8. hero.attack(enemy)
  9. if enemy.health <= 0:
  10. defeated = defeated + 1
  11. if hero.now() > 15:
  12. break
  13. # Tell Naria how many enemies you defeated.
  14. hero.moveXY(59, 33)
  15. hero.say("I defeated "+defeated+" enemies !")
  16.  
  17. # 收集金币,直到时间达到30秒
  18. while True:
  19. coin = hero.findNearestItem()
  20. if coin:
  21. hero.moveXY(coin.pos.x, coin.pos.y)
  22. if hero.now() > 30:
  23. break
  24. # Tell Naria how much gold you collected.
  25. hero.moveXY(59, 33)
  26. hero.say("I got "+hero.gold+" golds !")
  27.  
  28. # 攻击敌人,直到时间达到45秒
  29. # 记得重置击败的敌人数。
  30. defeated = 0
  31. while True:
  32. enemy = hero.findNearestEnemy()
  33. if enemy:
  34. hero.attack(enemy)
  35. if enemy.health <= 0:
  36. defeated = defeated +1
  37. if hero.now() > 45:
  38. break
  39. # Tell Naria how many enemies you defeated.
  40. hero.moveXY(59, 33)
  41. hero.say("I defeated "+defeated+" enemies !")

第15关:连续的炼金术
子网页:https://cn.codecombat.com/play/level/continuous-alchemy?

  1. # Race munchkins to the water distilled by Omarn Brewstone!
  2. # 使用 "continue" 验证丛林中的条件。
  3.  
  4. while True:
  5. enemy = hero.findNearestEnemy()
  6. item = hero.findNearestItem()
  7.  
  8. # 如果没有敌人,跳出循环继续。
  9. if not enemy:
  10. continue
  11.  
  12. # 如果有敌人却没物品,要一瓶药,跳到下次循环。
  13. if not item:
  14. hero.say("把喝的拿来!")
  15. continue
  16.  
  17. # 使用 if 语句检查物品的类型。如果类型是 "poison",跳出循环继续运行。
  18. if item.type =="poison":
  19. continue
  20. # 如果不是,那瓶子里装的是水,所以走向它,返回出发点!
  21. # so moveXY to the potion, then back to the start!
  22. if item.type !="poison":
  23. hero.moveXY(44, 35)
  24. hero.moveXY(34, 47)

第16关:复查
子网页:https://cn.codecombat.com/play/level/double-cheek?

  1. # 第一点,打败6位ogres~
  2. # Then collect coins until you have 30 gold.
  3.  
  4. # 变量用来对ogres计数
  5. defeatedOgres = 0
  6.  
  7. # 没打败6位ogres,就继续打
  8. while defeatedOgres < 6:
  9. enemy = hero.findNearestEnemy()
  10. if enemy:
  11. hero.attack(enemy)
  12. defeatedOgres += 1
  13. else:
  14. hero.say("Ogres!")
  15.  
  16. # Move to the right side of the map.
  17. hero.moveXY(49, 36)
  18.  
  19. # 钱没攒够30块,就继续捡
  20. while hero.gold < 30:
  21. # 寻找并收集金币
  22. coin = hero.findNearestItem()
  23. if coin:
  24. hero.moveXY(coin.pos.x, coin.pos.y)
  25. # 去掉这行 say()。
  26. #hero.say("我应该收集金币!")
  27.  
  28. # 移动到出口。
  29. hero.moveXY(76, 32)

第17关:沙漠战役
子网页:https://cn.codecombat.com/play/level/desert-combat?

  1. # while循环重复直到条件为否。
  2.  
  3. ordersGiven = 0
  4. y = 50
  5. while ordersGiven < 5:
  6. # 在站场上移动和排列你的盟友。 (如果你是直接在他们面前,他们只能听到你的。)
  7. hero.moveXY(10, y)
  8. # 用hero.say的方法说“Attack”来命令你的盟友
  9. # 只有当你的英雄在 “X” 位置的时候 他们才能听到你的说什么
  10. hero.say("Attack!")
  11. y = y - 10
  12. # 确保ordersGiven增加
  13. ordersGiven += 1
  14.  
  15. while True:
  16. enemy = hero.findNearestEnemy()
  17. # 当你下达完命令,立即加入战斗!
  18. if enemy:
  19. hero.attack(enemy)

第18关:尘埃
子网页:https://cn.codecombat.com/play/level/dust?

  1. # 使用循环直到你有足够的击杀10个芒奇金人
  2.  
  3. attacks = 0
  4. while attacks < 10:
  5. # 攻击最近的敌人!
  6. enemy = hero.findNearestEnemy()
  7. if enemy:
  8. hero.attack(enemy)
  9. # Incrementing means to increase by 1.
  10. # 增加你的攻击统计量。
  11. attacks += 1
  12.  
  13. # 当你完成后,撤退到伏击点。
  14. hero.say("I should retreat!") #∆ Don't just stand there blabbering!
  15. hero.moveXY(79, 33)

第19关:别冲过去,安静点
子网页:https://cn.codecombat.com/play/level/dont-rush-be-quiet?

  1. # Dodge the cannons and collect 8 gems.
  2. # Watch out, cannons are ready to fire!
  3. # 以一个特殊的方式缓慢移动去迷惑敌人
  4.  
  5. # This function returns a value from 0 to 30:
  6. def mod30(n):
  7. if n >= 30:
  8. return n - 30
  9. else:
  10. return n
  11.  
  12. # 这一功能将会返回一个从0到40的值
  13. def mod40(n):
  14. # 使用一个 “if” 语句去返回正确的值
  15. if n >= 40:
  16. return n - 40
  17. else:
  18. return n
  19.  
  20. # You don't need to change the following code:
  21. while True:
  22. time = hero.now()
  23. x = mod30(time) + 25
  24. y = mod40(time) + 10
  25. hero.moveXY(x, y)

第20关:Zig Zag and Zoom
子网页:https://cn.codecombat.com/play/level/zig-zag-and-zoom?

  1. # 从死亡峡谷逃出!
  2. # 使用真正的求余函数走出Z字形路线。
  3.  
  4. # This function returns a value from 0 to 15:
  5. def mod15(n):
  6. while n >= 15:
  7. n -= 15
  8. return n
  9.  
  10. # 这个函数应该会反馈一个从0到9的值
  11. def mod9(n):
  12. # 在返回前使用 while 循环修改参数。
  13. while n>= 9:
  14. n -= 9
  15. return n
  16.  
  17. # Don't change the following code:
  18. while True:
  19. time = self.now()
  20. if time < 30:
  21. y = 10 + 3 * mod15(time)
  22. else:
  23. y = 20 + 3 * mod9(time)
  24. x = 10 + time
  25. self.moveXY(x, y)

第21关:沙漠三角洲
子网页:https://cn.codecombat.com/play/level/desert-delta?

  1. # 只攻击在敌军名称(enemyNames)数组中的敌人
  2. # Be sure to attack in order! 0 -> 1 -> 2 -> 3
  3. enemyNames = ["Kog", "Godel", "Vorobun", "Rexxar"]
  4.  
  5. hero.attack(enemyNames[0])
  6. hero.attack(enemyNames[1])
  7. # 攻击 enemyNames[2]
  8. hero.attack(enemyNames[2])
  9. # 攻击最后一个元素。
  10. hero.attack(enemyNames[3])

第22关:立方雷区
子网页:https://cn.codecombat.com/play/level/cubic-minefield?

  1. # 穿过雷区
  2.  
  3. # 这个函数返回乘以次数的数字。
  4. def mult(number, times):
  5. total = 0
  6. while times > 0:
  7. total += number
  8. times -= 1
  9. return total
  10.  
  11. # 这个函数返回乘方的数字。
  12. def power(number, exponent):
  13. total = 1
  14. # 补全函数。
  15. while exponent > 0:
  16. total *= number
  17. exponent -= 1
  18. return total
  19.  
  20. # 别修改这些代码
  21. # You can find coefficients for the equation on the tower
  22. tower = hero.findFriends()[0]
  23. a = tower.a
  24. b = tower.b
  25. c = tower.c
  26. d = tower.d
  27. x = hero.pos.x
  28.  
  29. while True:
  30. # To find the path use a cubic equation
  31. y = a * power(x, 3) + b * power(x, 2) + c * power(x, 1) + d * power(x, 0)
  32. hero.moveXY(x, y)
  33. x = x + 5

第23关:Sarven救世主
子网页:https://cn.codecombat.com/play/level/sarven-savior?

  1. # 一个数组(Array)就是物品的数列。
  2. # 这个数组是一个朋友名字的数列。
  3.  
  4. friendNames = ['Joan', 'Ronan', 'Nikita', 'Augustus']
  5.  
  6. # 数组从零开始计数,不是1!
  7. friendIndex = 0
  8.  
  9. # 循环该数组中的每一个名字
  10. # 使用 len()方法来得到列表的长度。
  11. while friendIndex < len(friendNames):
  12. # 使用方括号来获得数组中的名字。
  13. friendName = friendNames[friendIndex]
  14. # 告诉你的朋友回家。
  15. # 使用+来连接两个字符串。
  16. hero.say(friendName + ', go home!')
  17. # 增加索引来获取数组中的下一个名字
  18. friendIndex += 1
  19.  
  20. # 回去建造栅栏让食人魔远离。
  21. hero.moveXY(29, 30)
  22. hero.buildXY("fence", 30, 30)

第24关:Bank Raid
子网页:https://cn.codecombat.com/play/level/bank-raid?

  1. # 等待食人魔出现,然后打败它们,并收集金币。
  2.  
  3. while True:
  4. enemies = hero.findEnemies()
  5.  
  6. # enemyIndex 用于迭代数组。
  7. enemyIndex = 0
  8.  
  9. # While enemyIndex is less than len(enemies)
  10. while enemyIndex < len(enemies):
  11. # Attack the enemy at enemyIndex
  12. enemy = enemies[enemyIndex]
  13. hero.attack(enemy)
  14. # 给 enemyIndex 加上 1。
  15. enemyIndex += 1
  16.  
  17. coins = hero.findItems()
  18. # coinIndex is used to iterate the coins array.
  19. coinIndex = 0
  20.  
  21. while coinIndex < len(coins):
  22. # 用 coinIndex 从 coins 数组得到一个金币。
  23. coin = coins[coinIndex]
  24. # 收集那个金币。
  25. hero.moveXY(coin.pos.x, coin.pos.y)
  26. # 给 coinIndex 的值增加 1。
  27. coinIndex += 1

第25关:游魂
子网页:https://cn.codecombat.com/play/level/wandering-souls?

  1. # 攻击骷髅捡走宝石
  2.  
  3. while True:
  4. enemies = hero.findEnemies()
  5. enemyIndex = 0
  6. while enemyIndex < len(enemies):
  7. enemy = enemies[enemyIndex]
  8. # Attack while enemy has health.
  9. while enemy.health > 0:
  10. hero.attack(enemy)
  11. enemyIndex += 1
  12.  
  13. items = hero.findItems()
  14. itemIndex = 0
  15. # 遍历所有的物品。
  16. while itemIndex < len(items):
  17. item = items[itemIndex]
  18. # While the distance greater than 2:
  19. while hero.distanceTo(item) > 2:
  20. # 试着拿到物品
  21. hero.moveXY(item.pos.x, item.pos.y)
  22. # 别忘了让itemIndex变大 (itemIndex+=1)或(itemIndex=itemIndex+1)
  23. itemIndex += 1

第26关:团队合作
子网页:https://cn.codecombat.com/play/level/team-work?

  1. # Gems will disappear soon. You'll need help!
  2.  
  3. # findItems() returns an array of items.
  4. items = hero.findItems()
  5.  
  6. # Get the first gem from the array.
  7. # Don't forget that the first index is 0.
  8. gem0 = items[0]
  9.  
  10. # # 告诉 Bruno 拿到 gem0
  11. hero.say("Bruno " + gem0)
  12.  
  13. # You can reference the gem without a variable.
  14. hero.say("Matilda " + items[1])
  15.  
  16. # Create a variable for the last gem, items[2]:
  17. gem2 =items[2]
  18. # Move to that gem's position using moveXY()
  19. hero.moveXY(gem2.pos.x, gem2.pos.y)

第27关:潜伏
子网页:https://cn.codecombat.com/play/level/lurkers?

  1. # 用findEnemies把敌人存在数组enemies中
  2. # 只攻击萨满巫师,不要攻击牦牛!
  3.  
  4. enemies = hero.findEnemies()
  5. enemyIndex = 0
  6.  
  7. # 把这段代码用一个while loop 功能循环遍历所有的敌人
  8. # 当 enemyIndex 小于 enemies 的长度时:
  9. while enemyIndex < len(enemies):
  10. enemy = enemies[enemyIndex]
  11. if enemy.type == 'shaman':
  12. while enemy.health > 0:
  13. hero.attack(enemy)
  14. # Remember to increment enemyIndex
  15. enemyIndex += 1

第28关:捡闪亮东西的人
子网页:https://cn.codecombat.com/play/level/shine-getter?

  1. # 很快的获取最多的金币
  2.  
  3. while True:
  4. coins = hero.findItems()
  5. coinIndex = 0
  6.  
  7. # 把这个封装进循环里枚举所有的硬币
  8. while coinIndex < len(coins):
  9. coin = coins[coinIndex]
  10. # 金币价值3点。
  11. if coin.value == 3:
  12. # 只捡金币。
  13. hero.moveXY(coin.pos.x, coin.pos.y)
  14. coinIndex += 1
  15. pass

第29关:Sarven牧羊人
子网页:https://cn.codecombat.com/play/level/sarven-shepherd?

  1. # 使用 while 循环来对付食人魔。
  2.  
  3. while True:
  4. enemies = hero.findEnemies()
  5. enemyIndex = 0
  6. # 将攻击逻辑放到 while 循环里来攻击所有的敌人。
  7. # Find the array's length with: len(enemies)
  8. while enemyIndex < len(enemies):
  9. enemy = enemies[enemyIndex]
  10. # "!=" 意思是 "不等于"
  11. if enemy.type != "sand-yak":
  12. # 当敌人的健康值大于0,攻击它!
  13. while enemy.health > 0:
  14. hero.attack(enemy)
  15. enemyIndex += 1
  16. pass
  17. # 在两波敌人之间,移动回中央。
  18. hero.moveXY(40, 30)

第30关:掠夺者
子网页:https://cn.codecombat.com/play/level/marauder?

  1. # 打几下泡泡人捡走掉出的币
  2.  
  3. while True:
  4. coin = hero.findNearestItem()
  5. # 当存在金币时:
  6. if coin:
  7. # 移动到金币处。
  8. hero.moveXY(coin.pos.x, coin.pos.y)
  9. # ‘coin’应该是最近的那枚 捡到手以后要另找一枚最近的
  10. coin += 1
  11.  
  12. enemy = hero.findNearestEnemy()
  13. if enemy:
  14. # 如果敌人还会动
  15. while enemy.health > 0:
  16. # 就打它
  17. hero.attack(enemy)
  18. pass

第31关:疯狂的Maxer
子网页:https://cn.codecombat.com/play/level/mad-maxer?

  1. # 优先杀掉最远的敌人。
  2.  
  3. while True:
  4. farthest = None
  5. maxDistance = 0
  6. enemyIndex = 0
  7. enemies = hero.findEnemies()
  8.  
  9. # 查看全部敌人,找出最远的那个。
  10. while enemyIndex < len(enemies):
  11. target = enemies[enemyIndex]
  12. enemyIndex += 1
  13.  
  14. # 是不是存在远得看不到的敌人?
  15. distance = hero.distanceTo(target)
  16. if distance > maxDistance:
  17. maxDistance = distance
  18. farthest = target
  19.  
  20. if farthest:
  21. # 干掉最远的敌人!
  22. # 如果敌人血量大于0就保持攻击。
  23. while farthest.health > 0:
  24. if hero.isReady("cleave"):
  25. hero.cleave(farthest)
  26. elif hero.isReady("bash"):
  27. hero.bash(farthest)
  28. else:
  29. hero.attack(farthest)
  30. pass

第32关:沙蛇
子网页:https://cn.codecombat.com/play/level/sand-snakes?

  1. # 这片区域布满了火焰陷阱。幸好我们之前派出了侦察员,他沿路在地上留下了宝石作为暗号,我们只需要顺着最近的宝石走就能躲过这些陷阱。
  2.  
  3. # 沙漠峡谷似乎会干扰你使用眼镜的findNearest技能!
  4. # 你需要自己找到离你最近的宝石。
  5.  
  6. while True:
  7. coins = hero.findItems()
  8. coinIndex = 0
  9. nearest = None
  10. nearestDistance = 9999
  11.  
  12. # 搜索所有的宝石,找到离你最近的那一颗。
  13. while coinIndex < len(coins):
  14. coin = coins[coinIndex]
  15. coinIndex += 1
  16. distance = hero.distanceTo(coin)
  17. # 如果宝石与你的距离小于“最近距离(nearestDistance)”
  18. if distance < nearestDistance:
  19. # 设置该宝石为离你最近的宝石
  20. nearest = coin
  21. # 设置该距离为“最近距离(nearestDistance)”
  22. nearestDistance = distance
  23.  
  24. # 如果找到离你最近的宝石,移动英雄岛宝石的位置。你需要使用moveXY,不需要你抄近路,也不会踩到陷阱。
  25. if nearest:
  26. hero.moveXY(nearest.pos.x, nearest.pos.y)

第33关:Brittle Moral
子网页:https://cn.codecombat.com/play/level/brittle-morale?

  1. # You have one arrow. Make it count!
  2.  
  3. # 这将返回一个最多生命值的敌人
  4. def findStrongestEnemy(enemies):
  5. strongest = None
  6. strongestHealth = 0
  7. enemyIndex = 0
  8. # 当 enemyIndex 少于敌人的长度
  9. while enemyIndex < len(enemies):
  10. # Set an enemy variable to enemies[enemyIndex]
  11. enemy = enemies[enemyIndex]
  12. # 如果 enemy.health 大于 strongestHealth
  13. if enemy.health > strongestHealth:
  14. # 将 strongest 赋值为 enemy
  15. strongest = enemy
  16. # Set strongestHealth to enemy.health
  17. strongestHealth = enemy.health
  18. # 让 enemyIndex 递增
  19. enemyIndex += 1
  20. return strongest
  21.  
  22. enemies = hero.findEnemies()
  23. leader = findStrongestEnemy(enemies)
  24. if leader:
  25. hero.say(leader)

第34关:一打宝石
子网页:https://cn.codecombat.com/play/level/diamond-dozen?

【装备更换】剑:攻击值越高越好,我是228.57(192.2DPS)    鞋子:速度+2.5

  1. # 打败前来劫掠的食人魔,让他们把金币交出来!
  2.  
  3. def findMostHealth(enemies):
  4. target = None
  5. targetHealth = 0
  6. enemyIndex = 0
  7. while enemyIndex < len(enemies):
  8. enemy = enemies[enemyIndex]
  9. if enemy.health > targetHealth:
  10. target = enemy
  11. targetHealth = enemy.health
  12. enemyIndex += 1
  13. return target
  14.  
  15. def valueOverDistance(item):
  16. return item.value / hero.distanceTo(item)
  17.  
  18. # 返回有最高 valueOverDistance(item) 的物品。
  19. def findBestItem(items):
  20. bestItem = None
  21. bestValue = 0
  22. itemsIndex = 0
  23.  
  24. # 循环于 items 数组内。
  25. # 发现这个物品的最高 valueOverDistance()
  26. while itemsIndex < len(items):
  27. item = items[itemsIndex]
  28. if valueOverDistance(item) > bestValue:
  29. bestItem = item
  30. bestValue = valueOverDistance(item)
  31. itemsIndex += 1
  32. return bestItem
  33.  
  34. while True:
  35. enemies = hero.findEnemies()
  36. enemy = findMostHealth(enemies)
  37. if enemy and enemy.health > 15:
  38. while enemy.health > 0:
  39. hero.attack(enemy)
  40. else:
  41. coins = hero.findItems()
  42. coin = None
  43. coin = findBestItem(coins)
  44. if coin:
  45. hero.moveXY(coin.pos.x, coin.pos.y)

第35关:Wishing Well
子网页:https://cn.codecombat.com/play/level/wishing-well?

  1. # 你需要104的金钱,不多也不少。
  2.  
  3. less = "Nimis"
  4. more = "Non satis"
  5. requiredGold = 104
  6.  
  7. # 此函数计算所有的硬币值的总和。
  8. def sumCoinValues(coins):
  9. coinIndex = 0
  10. totalValue = 0
  11. # 遍历所有的金币。
  12. while coinIndex < len(coins):
  13. totalValue += coins[coinIndex].value
  14. coinIndex += 1
  15. return totalValue
  16.  
  17. def collectAllCoins():
  18. item = hero.findNearest(hero.findItems())
  19. while item:
  20. hero.moveXY(item.pos.x, item.pos.y)
  21. item = hero.findNearest(hero.findItems())
  22.  
  23. while True:
  24. items = hero.findItems()
  25. # 获得硬币的总值
  26. goldAmount = sumCoinValues(items)
  27. # 如果有金币,那么金币数目 (goldAmount) 不会是零
  28. if goldAmount != 0:
  29. # If goldAmount is less than requiredGold
  30. # 那就说“Non satis”
  31. if goldAmount < requiredGold:
  32. hero.say("Non satis")
  33. # If goldAmount is greater than requiredGold
  34. # 那么说出“Nimis”。
  35. if goldAmount > requiredGold:
  36. hero.say("Nimis")
  37. # 如果 “goldAmount” 等于 “requiredGold”
  38. # 如果有刚好 104 金币,就全部收集。
  39. if goldAmount == requiredGold:
  40. collectAllCoins()
  41. pass

【挑战升级】第36关:Sarven 斗殴
子网页:https://cn.codecombat.com/play/level/sarven-brawl?

第一次:【我设定英雄回到的位置,比较不容易被射箭的敌人从远距离射伤】

  1. # 活两分钟。
  2. # 如果你赢了,接下来会变得更难,当然也会有更多奖励。
  3. # 如果你输了,你必须等一天之后再提交。
  4. # 记得,每一次提交都会获得不同的地图。
  5.  
  6. while True:
  7. enemy = hero.findNearestEnemy()
  8.  
  9. if enemy:
  10. if hero.isReady("bash"):
  11. hero.bash(enemy)
  12. else:
  13. hero.attack(enemy)
  14. else:
  15. hero.moveXY(50, 70)

第37关:
子网页:

第38关:
子网页:

第39关:
子网页:

第40关:
子网页:

第41关:
子网页:

第42关:
子网页:

第43关:审判
子网页:https://cn.codecombat.com/play/level/the-trials?

未通关

初学Python。

请多指教。

-----转载请注明出处,否则作者有权追究法律责任。

[Python] Codecombat 攻略 Sarven 沙漠 (1-43关)截止至36关的更多相关文章

  1. [Python] Codecombat 攻略 Sarven 沙漠 (1-43关)截止至30关

    首页:https://cn.codecombat.com/play语言:Python 第二界面:Sarven沙漠(43关)时间:4-11小时内容:算术运算,计数器,while循环,break(跳出循环 ...

  2. [Python]Codecombat攻略之远边的森林Forest(1-40关)

    首页:https://cn.codecombat.com/play语言:Python 第二界面:远边的森林Forest(40关)时间:2-6小时内容:if/else.关系操作符.对象属性.处理输入网页 ...

  3. [Python] Codecombat攻略 远边的森林 Forest (1-40关)

    首页:https://cn.codecombat.com/play语言:Python 第二界面:远边的森林Forest(40关)时间:2-6小时内容:if/else.关系操作符.对象属性.处理输入网页 ...

  4. [Python]Codecombat攻略之地牢Kithgard(1-22关)

    首页:https://cn.codecombat.com/play语言:Python 第一界面:地牢 Kithgard(22关) 时间:1-3小时 内容:语法.方法.参数.字符串.循环.变量等 网页: ...

  5. [Python] Codecombat 攻略 地牢 Kithgard (1-22关)

    首页:https://cn.codecombat.com/play语言:Python 第一界面:地牢 Kithgard(22关) 时间:1-3小时 内容:语法.方法.参数.字符串.循环.变量等 网页: ...

  6. python脚本攻略之log日志

    1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...

  7. hack游戏攻略(黑吧安全吧的黑客闯关游戏)古墓探秘

    2019.2.11 这个是找到的一个黑客游戏,就是一关一关,挺像ctf的,玩玩也挺有意思,还能涨知识. 地址:http://hkyx.myhack58.com/ 入口: 入口就是这样的.提示是 图内有 ...

  8. [摸鱼] 配置的vim的使用攻略!

    vim使用攻略 <>=f 折叠与缩进 开:<>[^fuck]cc 关:<>cu 缩进一块使用V选中,按>> [Ctrl]V 以列为单位选 za,打开或关 ...

  9. Python环境下NIPIR(ICTCLAS2014)中文分词系统使用攻略

    一.安装 官方链接:http://pynlpir.readthedocs.org/en/latest/installation.html 官方网页中介绍了几种安装方法,大家根据个人需要,自行参考!我采 ...

随机推荐

  1. 【Leetcode_easy】605. Can Place Flowers

    problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...

  2. linux服务器常用密令

    1. 什么是linux服务器load average? Load是用来度量服务器工作量的大小,即计算机cpu任务执行队列的长度,值越大,表明包括正在运行和待运行的进程数越多.参考资料:http://e ...

  3. Spring AOP 简介(三)

    Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...

  4. MySQL安装与连接

    1.安装 下载地址:https://dev.mysql.com/downloads/mysql/ 常见问题及解决办法:https://blog.csdn.net/chen97_08/article/d ...

  5. Python senium 中页面属性

    1.通过id定位元素2.通过class_name定位元素3.通过tag_name定位元素4.通过name定位元素5.通过link文字精确定位元素6.通过link文字模糊定位元素7.通过CSS定位元素8 ...

  6. 利用Python进行数据分析_Pandas_层次化索引

    申明:本系列文章是自己在学习<利用Python进行数据分析>这本书的过程中,为了方便后期自己巩固知识而整理. 层次化索引主要解决低纬度形式处理高纬度数据的问题 import pandas ...

  7. Maven web项目创建

    一.New →Other →Maven→Maven Module 二.填入Module Name 三.选择maven-archetype-webapp 四.填入Group Id.Artifact Id ...

  8. Luogu4770 NOI2018你的名字(后缀自动机+线段树合并)

    先考虑l=1,r=n,并且不要求本质不同的情况.对原串建SAM,将询问串在上面跑,得到每个前缀的最长匹配后缀即可得到答案. 然后考虑本质不同.对询问串也建SAM,统计每个节点的贡献,得到该点right ...

  9. Session共享问题---理论

    随着网站访问量增加,初期的一台服务器已经完全不能支持业务,这个时候我们就需要增加服务器设备,来抗住请求的增量,如下所示: 负载均衡的目的本来就是要为了平均分配请求,所以没有固定第一次访问和第二次访问的 ...

  10. MySQL 字段类型介绍

    MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...