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

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

边地伏击A(Backwoods Ambush A)

# In this level you will use functions with two parameters.# Look at the structure below, notice how there are two arguments.# These are both accessible within the function. def checkAndAttack(x, y):    # First move to the coordinates provided by the parameters.    hero.moveXY(x, y)    # Then check for an enemy.    enemy = hero.findNearestEnemy()        # If there is one, attack it!    if enemy:        hero.attack(enemy)    passcheckAndAttack(24, 42)checkAndAttack(27, 60)# Navigate to the last 3 x-marks and defeat any remaining munchkins.checkAndAttack(42, 50)checkAndAttack(39, 24)checkAndAttack(55, 29)

边地好伙伴

这关开始你要学习着操纵你的宠物了!!

# 你现在拥有一个宠物!def speak(event):    # 你的宠物需要用pet.say()进行回应    pet.say("Hello World")    pass# 这将告诉你的宠物,在听到声音时运行speak()函数pet.on("hear", speak)# 和你的宠物交谈!hero.say("Hello Kitty")

友人和敌人

# 农民和士兵聚集在森林。# 命令农民战斗,苦工远离!while True:    friend = hero.findNearestFriend()    if friend:        hero.say("To battle, " + friend.id + "!")    # 寻找最近的敌人,然后让他们滚蛋    enemy = hero.findNearestEnemy()    if enemy:        hero.say("Go out, " + enemy.id + "!")

Agrippa Returned(返回Agrippa)

def enemyInRange(enemy):    # 如果与敌人的距离少于5个单位,则返回true值    distance = hero.distanceTo(enemy)    if distance < 5:        return True    return Falsedef cleaveOrAttack(enemy):    if hero.isReady('cleave'):        hero.cleave(enemy)    else:        hero.attack(enemy)while True:    enemy = hero.findNearestEnemy()    if enemy:        # 调用 enemyInRange 检查敌人的距离。        if enemyInRange(enemy):            cleaveOrAttack(enemy)

去拿取

函数的另外一种调用方法,百度下仔细讲解下!

# 你被困在了陷阱中!# 派遣宠物拿取治疗药水!def goFetch():    # 你可以在处理函数中使用循环。    while True:        potion = hero.findNearestItem()        if potion:            # 用 “pet.fetch()” 去让你的宠物捡药水:            pet.fetch(potion)            pass# 当宠物被召唤出来时,会触发 "spawn" 事件。# 这让你的宠物在关卡开始时运行 goFetch()函数。pet.on("spawn", goFetch)

好伙伴的名字

# 我们需要知道新宠物的名字。# 把这个函数用作宠物 "hear" 事件的处理函数。def onHear(event):    # 不要更改这个函数    pet.say("喵呜~ 汪 喵呜~")    pet.say("汪 汪")    pet.say("喵呜~")    pet.say("喵呜~")    pet.say("喵呜~ 汪 喵呜~ 喵呜~")# 使用 “the pet.on(eventType,eventHandler) 方法”# 指派onHear函数来处理"hear"事件。pet.on("hear", onHear)# 这必须在 "pet.on" 的后面。hero.say("伙计,你叫什么名字?")hero.say("能重复一次吗?")

好伙伴的名字A

# 农民想知道宠物的名字。# 使用这个函数作为"hear"事件的处理函数。def sayName(event):    # 宠物会在函数调用时按顺序说这些。    pet.say("我名叫狂兽。")    pet.say("不过我的朋友们叫我毛球。")    # 使用pet.on("eventName", functionName)来添加事件监听函数给宠物# 在这里使用"hear" sayName及pet.on()pet.on("hear", sayName)# 你这次不需要说任何东西!# 农民会进行交谈。

博士猫咪

# 教你的宠物回答问题!# 很幸运,所有的答案都是"2"def sayTwo(event):    # 使用pet.say()来回答"2"    pet.say("2")    pass# 使用pet.on(),通过sayTwo来处理"hear"事件pet.on("hear", sayTwo)# 现在休息并观看表演吧!hero.say("一加一等于…?")hero.say("x^3 - 6x^2 + 12x - 8 = 0. x等于多少?")hero.say("火星有多少卫星?")
上一篇:CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码9
下一篇:CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码7

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年03月22日 20时54分39秒