2025年CodeCombat代码全记录(Python学习利器)--SARVEN沙漠(第三章)代码10

CodeCombat代码全记录(Python学习利器)--SARVEN沙漠(第三章)代码10毒气攻击 计算所有食人魔的总生命值 def sumHealth enemies 创建一个变量 将它设为 0 后开始运算 totalHealth 0 初始化循环索引为 0 enemyIndex 0 當 計數 小于敌人數列長度的時候 while enemyIndex lt len enemies

大家好,我是讯享网,很高兴认识大家。

毒气攻击

# 计算所有食人魔的总生命值。 def sumHealth(enemies): # 创建一个变量,将它设为0后开始运算 totalHealth = 0 # 初始化循环索引为0 enemyIndex = 0 # 當 計數 小于敌人數列長度的時候 while enemyIndex < len(enemies): # 将当前敌人的生命值添加到总生命值里 totalHealth += enemies[enemyIndex].health # 让 index 递增 enemyIndex = enemyIndex + 1 #return totalHealth return totalHealth # 使用加农炮来打败食人魔。 cannon = hero.findNearest(hero.findFriends()) # 加农炮可以穿透墙壁。 enemies = cannon.findEnemies() # 计算食人魔生命值的总和。 ogreSummaryHealth = sumHealth(enemies) hero.say("使用 " + ogreSummaryHealth + " 克。") 

讯享网

公平之战

讯享网# 直到你士兵的总生命值大于兽人的. # 在你的士兵取得优势前不要发起进攻. # This function return the sum of all the units' health. def sumHealth(units): totalHealth = 0 # Complete this function: index = 0 while index < len(units): totalHealth += units[index].health index += 1 return totalHealth while True: friends = hero.findFriends() enemies = hero.findEnemies() # 计算并比较你的士兵和兽人的总生命值. if sumHealth(friends) <= sumHealth(enemies): hero.say("Wait") # 当你准备好后说“Attack”. else: hero.say("ATTACK!!!") 

空中桥梁

# 帮助农民逃跑。 def onSpawn(event): # 我们需要拯救三个农民。 remainingPeasants = 3 while remainingPeasants > 0: # 找到一个好位置。 pet.moveXY(40, 55) peasant = pet.findNearestByType("peasant") if peasant: # 把农民带到中间的通道。 pet.carryUnit(peasant, 40, 34) remainingPeasants -= 1 munchkin = pet.findNearestByType("munchkin") # Carry a munchkin to the fire traps: pet.carryUnit(munchkin, 35, 21) pet.on("spawn", onSpawn) # 战斗! enemy = hero.findNearestEnemy() if enemy: if hero.isReady("cleave"): hero.cleave(enemy) else: hero.attack(enemy) 

疯狂Maxer反击

讯享网# 小一点的食人魔会造成更多的伤害! # 优先攻击血少的敌人 while True: weakest = None leastHealth = 99999 enemyIndex = 0 enemies = hero.findEnemies() # 循环检查所有敌人。 while enemyIndex < len(enemies): # 如果当前对象的血量更少 if enemies[enemyIndex].health < leastHealth: # 标为最弱的,更新 leastHealth 变量 leastHealth = enemies[enemyIndex].health weakest = enemies[enemyIndex] enemyIndex += 1 if weakest: # 攻击最弱的食人魔。 hero.attack(weakest) pass 

疯狂Maxer卖光了

# 金币会在几秒钟之后消失! # 在他们消失前,收集所有的金币。 while True: closestGold = None minGoldDist = 9001 coinIndex = 0 coins = hero.findItems() # 找到最近的金币 # 记住,金币价值3点。 while coinIndex < len(coins): coin = coins[coinIndex] distance = hero.distanceTo(coin) if distance < minGoldDist and coin.value == 3: minGoldDist = distance closestGold = coin coinIndex += 1 if closestGold: #现在去到最近的金币,并得到它! hero.moveXY(closestGold.pos.x, closestGold.pos.y) pass 

疯狂的Maxer变得贪婪

注意使用的是hero.move,而不是hero.moveXY

讯享网# 比你的分身收集的金币多。 # 你只有几秒钟来收集金币,聪明的选择你的路线! while True: bestCoin = None maxRating = 0 coinIndex = 0 coins = hero.findItems() # 试着计算"价值/距离"来决定你要收集哪个金币。 while coinIndex < len(coins): coin = coins[coinIndex] coinIndex += 1 #distance = hero.distanceTo(coin) bestValue = coin.value / hero.distanceTo(coin) #hero.say(bestValue) if bestValue > maxRating: bestCoin = coin maxRating = bestValue if bestCoin: hero.move({"x": bestCoin.pos.x, "y": bestCoin.pos.y}) 

疯狂的Maxer:兑现

# 你不能到你朋友那边去保卫他们! # 告诉他们回家,弓箭手会帮助他们 while True: weakestFriend = None leastHealth = 9999 friendIndex = 0 # 找出哪个朋友的生命值最差。 friends = hero.findFriends() while friendIndex < len(friends): friend = friends[friendIndex] if friend.health < leastHealth: leastHealth = friend.health weakestFriend = friend friendIndex += 1 # 告诉最弱的朋友先回家。 if weakestFriend: hero.say('Hey ' + weakestFriend.id + ', go home!') 

最弱者最快

讯享网# 击败萨满并生存下去。 # 该函数找到最弱的敌人: def findWeakestEnemy(): enemies = hero.findEnemies() weakest = None leastHealth = 99999 enemyIndex = 0 # 遍历所有敌人: while enemyIndex < len(enemies): enemy = enemies[enemyIndex] # 如果敌人血量低于 leastHealth: if enemy.health < leastHealth: # 将其设为最弱的 weakest = enemy # 并将 leastHealth 设为其血量 leastHealth = enemy.health enemyIndex += 1 return weakest while True: # 使用函数找到最弱的敌人: weakestShaman = findWeakestEnemy() # 如果这里是那个最弱的敌人: if weakestShaman: # 攻击它! hero.attack(weakestShaman) 

克隆冲突

请开发你自己的想象,尽可能多的使用技能,这里仅简单的举例示例代码。

# 你需要好策略来赢得这关。 # 你的克隆体将会拥有和你一样的装备! # 但是,他们不大会使用特殊技能。 while True: weakEnemy = None weakHealth = 9999 index = 0 enemies = hero.findEnemies() while index < len(enemies): enemy = enemies[index] if enemy.health < weakHealth: weakEnemy = enemy weakHealth = enemy.health index += 1 if weakEnemy and weakEnemy.health > 0: if hero.isReady("cleave"): hero.cleave(weakEnemy) else: hero.attack(weakEnemy) if hero.health < hero.maxHealth / 4: hero.moveXY(63, 51) hero.moveXY(63, 82) 

好,到这里我们SARVEN沙漠所有的基础关卡就完事了。目前还剩余挑战关卡,需要大家去动动脑筋自行处理了。


讯享网

剩余挑战关卡记录(十关):
神圣雕像;Sarven斗殴;
金色幻影;Sarven总数;苦痛之土;
重重试炼;Sarven的宝藏;Sarven围困;士兵、食人魔和农民;守门员

前面我们说过,当我们完成所有关卡的内容后在来看挑战关卡,你会认为挑战关卡也不过如此。

congratulation,你已经完成了第三章所有内容的学习,已经可以更进一步去第四章了。后面的内容更加的深入,而且会有更新的东西出现。

大家不要灰心,在第三章中,相信大家会遇到很多的困难,不要紧,我们多加练习,多加坚持!!!不会的内容我们练他3遍,不行就5遍!!在不行就10遍!!!!!孰能生巧!~~

加油!!!!

小讯
上一篇 2025-02-13 19:53
下一篇 2025-02-17 08:10

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/25817.html