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

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

好伙伴的名字B

# 宠物需要向英雄和农民问好。# 使用这个函数作为"hear"事件的处理函数:def sayHello(event):    # 宠物在说你好:    pet.say("致敬")# 使用宠物的pet.on("eventType", functionName)方法.# 这一关里,宠物需要在听到声音后运行sayHellopet.on("hear", sayHello)# 那么,问候宠物,等待回应。hero.say("你好,朋友!")

宠物小测试

# 编写一个事件处理函数,名为onHear# 完成onHear函数def onHear(event):    # 宠物需要在onHear中说点什么。    pet.say("必须的!")    pet.say("必须是!")    pet.say("28")    pet.say("美国!")    pet.say("Bye")    passpet.on("hear", onHear)hero.say("能听懂我的话吗?")hero.say("你是美洲狮吗?")hero.say("你多大啦?")# 再问两个问题。hero.say("你来自哪里?")hero.say("Byebye")

看门狗

# 你无法帮助农民过河。# 不过,你的宠物做得到!# 将你的狼调教成看门狗。def onSpawn(event):    while True:        # 宠物一样可以发现敌人。        enemy = pet.findNearestEnemy()        # 如果有敌人:        if enemy:            # 然后让宠物说点什么:            pet.say("鬼子来了,快跑啊!!!")pet.on("spawn", onSpawn)

漫漫长路

如果你的血较少,记得买件撑血的装备。

# 移动到右边# 补全这个函数:def onSpawn(event):    pass    # 在 while-true 循环里:    while True:        # 使用hero.findNearestItem()        item = hero.findNearestItem()        # 如果有物品:        if item:            # 使用pet.fetch(item)来拿取物品。            pet.fetch(item)# 使用pet.on()来将onSpawn指派给"spawn"事件pet.on("spawn", onSpawn)hero.moveXY(78, 35)

森林慢跑

# 你的宠物可以使用 pet.moveXY()def onSpawn(event):    while True:        # 首先,移动到左侧X标记处:        pet.moveXY(9, 24)        # 接着,移动到上面的X标记。        pet.moveXY(30, 43)        # 将宠物移动至右侧的X标记处:        pet.moveXY(51, 24)        # 将你的宠物移动到下面的X标记处:        pet.moveXY(30, 5)# 使用pet.on(),通过onSpawn来处理"spawn"事件pet.on("spawn", onSpawn)# 激励你的宠物!# 不要移除下方的命令。while True:    hero.say("狗狗真棒!")    hero.say("你能做到的!")    hero.say("跑跑跑!")    hero.say("快好了!")    hero.say("再来一圈!")

Airdrop(空投)

# Get all swords and protect the village.def onSpawn (event):    while True:        item = hero.findNearestItem()        #  The pet should fetch the item if it exists:        if item:            pet.fetch(item)# Assign onSpawn function for the pet's "spawn".pet.on("spawn", onSpawn)while True:    # Guard the left passage:     enemy = hero.findNearestEnemy()    if enemy:        hero.attack(enemy)        hero.moveXY(22, 34)    pass

森林加农炮之舞

# 你的宠物应该在X标记上来回跑动# 英雄需要时刻保持兴奋!# 为宠物编写事件函数onSpawn# 这个函数要让狼来回跑动:# 首先,跑到右侧的标记上,然后左侧的标记:def onSpawn():    while True:        pet.moveXY(48, 8)        pet.moveXY(12, 8)pet.on("spawn", onSpawn)# 鼓舞你的宠物。不要移除这个:while True:    hero.say("跑!!!")    hero.say("速度!")

Reward and Ruination(罪与罚)

# It seems like the Ogre Chieftain is stealing your gems!# Use the two artillery cannons for your goals.while True:    enemy = hero.findNearestEnemy()    if enemy:        enemyPos = enemy.pos.x + " " + enemy.pos.y        hero.say("Enemy at " + enemyPos)            # Now that you have sweet revenge,    # why not have your cake and eat it, too?    # Find the item's position and     # say it for your artillery to target.    item = hero.findNearestItem()    if item:        itemPos = item.pos.x + " " + item.pos.y        hero.say("Item at " + itemPos)

上一篇:CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码10
下一篇:CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码8

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月25日 17时24分06秒