CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码7
发布日期:2021-05-07 10:58:03 浏览次数:22 分类:原创文章

本文共 3526 字,大约阅读时间需要 11 分钟。

盗墓者

记得参数是一种给函数传递信息的方式,它是函数被调用时预定义的变量,如果你定义的函数存在参数,那么在调用时,也记得添加参数!!

# 森林中一座被遗忘的墓地!# 但是食人魔紧追不舍。# 在防御矮人的同时破开坟墓# 这个函数应该在敌人存在时攻击,否则攻击门!def checkToDefend(target):    # 检查目标是否存在    enemy = hero.findNearestEnemy()    if enemy:        # 如果是这样,攻击目标。        hero.attack(enemy)    # 如果没有目标,使用else去做点别的事    else:        # 否则攻击 "Door"        hero.attack("Door")    passwhile True:    enemy = hero.findNearestEnemy()    checkToDefend(enemy)

交给劈斩者

# 这里展示了如何定义一个叫作cleaveWhenClose的函数# 函数定义了一个参数,名为targetdef cleaveWhenClose(target):    if hero.distanceTo(target) < 5:        # 将你的攻击代码放到这里。        # 如果cleave准备就绪,那就劈斩目标        if hero.isReady("cleave"):            hero.cleave(target)        # 否则,使用attack攻击目标!        hero.attack(target)# 这段代码不是函数的一部分。while True:    enemy = hero.findNearestEnemy()    if enemy:        # 注意在cleaveWhenClose内部,我们用target指向敌人。        cleaveWhenClose(enemy)

墓地阴魂

# 唯一的出口被食人魔堵住了。# 躲着骷髅怪,并一个个击败食人魔# 这个函数需要攻击敌人并隐藏。def hitOrHide(target):    # 如果'目标'存在:    if target:        # 攻击'目标'        hero.attack(target)        # 然后移动到红色标记。        hero.moveXY(32,17)    passwhile True:    enemy = hero.findNearestEnemy()    hitOrHide(enemy)

Mail Interceptor(邮件截停)

# Intercept all ogre messengers from ambush. def ambushAttack(target):    #  Attack the target if it exists and return to the mark.    # Write the function:    if target:        hero.attack(target)        hero.moveXY(52, 36)    passwhile True:    ogre = hero.findNearestEnemy()    ambushAttack(ogre)

捉迷藏

def checkTakeHide(item):    if item:        # 物品在此,拿着它。        hero.moveXY(item.pos.x, item.pos.y)        # 然后移动到营地中央(40, 34)        hero.moveXY(40, 34)while True:    # 移动到右上的X标记。    hero.moveXY(68, 56)    # 在那里搜索一块发光石。    lightstone = hero.findNearestItem()    # 调用checkTakeHide,并使用参数:lightstone    checkTakeHide(lightstone)        # 移动到左上角的标记。    hero.moveXY(12, 56)    # 搜索发光石。    lightstone = hero.findNearestItem()    # 调用checkTakeHide函数。    # 将搜索的结果作为参数传入。    checkTakeHide(lightstone)

森林矿工

# 检查工人们是否能安全通过雷区。def checkEnemyOrSafe(target):    # 如果目标(参数)存在:    if target:        # 然后攻击目标        hero.attack(target)    # 否则:    else:        # 使用say()来叫农民。        hero.say("farmer come here!!")    passwhile True:    # 移动到并检查右上的X标记。    hero.moveXY(64, 54)    enemy1 = hero.findNearestEnemy()    checkEnemyOrSafe(enemy1)        # 移动到左下的X标记处。    hero.moveXY(16, 14)    # 将findNearestEnemy()的结果存到一个变量中。    enemy2 = hero.findNearestEnemy();    # 调用checkEnemyOrSafe,并传递    # findNearestEnemy的结果作为参数    checkEnemyOrSafe(enemy2)

Short-sighted Burl(短视的树精)

注意:在你编写的函数中要调用其他的函数,学习看好是怎么进行调用的。

# Collect coins and run lest the burl will find you.# The function allows your hero take an item.def takeItem(item):    hero.moveXY(item.pos.x, item.pos.y)# Write the function "checkTakeRun" with one parameter.# If the item exists, use "takeItem" function to take it.# Move to the start point (the green mark) whether the item or no.def checkTakeRun(item):    if item:        takeItem(item)    hero.moveXY(40, 12)# Don't change this code.while True:    hero.moveXY(16, 56)    coin = hero.findNearestItem()    checkTakeRun(coin)    hero.moveXY(64, 56)    coin = hero.findNearestItem()    checkTakeRun(coin)

Agrippa 重构

def cleaveOrAttack(enemy):    # 如果 “cleave” 技能冷却完毕,那就使用它!否则,使用普通攻击。    if hero.isReady("cleave"):        hero.cleave(enemy)    else:        hero.attack(enemy)    passwhile True:    enemy = hero.findNearestEnemy()    if enemy:        distance = hero.distanceTo(enemy)        if distance < 5:            # 调用上面定义的 “cleaveOrAttack” 函数            cleaveOrAttack(enemy)
上一篇:CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码8
下一篇:ansible离线安装操作手册(含所有安装包内容)

发表评论

最新留言

很好
[***.229.124.182]2025年04月09日 21时39分20秒