
CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码12
发布日期:2021-05-07 10:58:09
浏览次数:19
分类:原创文章
本文共 3625 字,大约阅读时间需要 12 分钟。
Metal Detector(金属探测器)
# The artillery uses coins as a target.# You'll be the rangefinder for the artillery.# Write the function.def coinDistance(): # Find the nearest coin, coin = hero.findNearestItem() # If there is a coin, return the distance to it. if coin: distance = hero.distanceTo(coin) return distance # Else, return 0 (zero). return 0 passwhile True: distance = coinDistance() if distance > 0: # Say the distance. hero.say(distance) pass
流星雨
本章我们开始学习分类的内容。
# Pick up coins only if they are closer than 20m.# Pick up all gems.while True: item = hero.findNearestItem() distance = hero.distanceTo(item) # If the item's type is "gem" # OR the distance to the item less than 20 meters: if item.type == 'gem' or distance < 20: # Move to item's position. hero.moveXY(item.pos.x, item.pos.y)
春雷
# Certain coins and gems attract lightning.# 这个英雄应只收集银币和蓝宝石while True: item = hero.findNearestItem() # A silver coin has a value of 2. # Collect if item.type is equal to "coin" # AND item.value 相等于2 if item.type == "coin" and item.value == 2: hero.moveXY(item.pos.x, item.pos.y) # 一个蓝宝石价值10 # Collect if item.type is equal to "gem" # AND item.value is equal to 10. if item.type == "gem" and item.value == 10: hero.moveXY(item.pos.x, item.pos.y)
Forest Shadow(森林阴影)
# Big ogres can't see you in the forest.# Attack only the small ogres in the forest.# Collect coins and gems only.# Don't leave the forest and don't eat/drink anything.while True: # Find the nearest enemy. enemy = hero.findNearestEnemy() # Attack it only if its type is "thrower" or "munchkin". if enemy.type == "thrower" or enemy.type == "munchkin": hero.attack(enemy) # Find the nearest item. item = hero.findNearestItem() # Collect it only if its type is "gem" or "coin". if item.type == "gem" or item.type == "coin": hero.moveXY(item.pos.x, item.pos.y) pass
Teleport Lasso(传送套锁)
# Our wizards teleport ogres from their camp here.# They appear for a short period and they are stunned.# Attack only weak and near ogres.while True: enemy = hero.findNearestEnemy() distance = hero.distanceTo(enemy) # If enemy.type is "munchkin" # AND the distance to it is less than 20m if enemy.type == "munchkin" and distance < 20: # Then attack it. hero.attack(enemy)
平常的一天
# 打败食人魔,收集金币。一切都那么平常。# 使用 与(AND) 在同一行检查存在性和类型。while True: enemy = hero.findNearestEnemy() # 有了与(AND),只在敌人存在时检查类型 if enemy and enemy.type == "munchkin": hero.attack(enemy) # 寻找最近的物品 item = hero.findNearestItem() # 如果有名为 “coin” (金币)的物品存在,那就快去收集它! if item and item.type == "coin": hero.moveXY(item.pos.x, item.pos.y)
穿越
# Don't insult this tribe of peaceful ogres.while True: item = hero.findNearestItem() if item: # If item.type IS NOT EQUAL TO "gem" if item.type != "gem": # 然后跟随你的宠物。 hero.moveXY(pet.pos.x, pet.pos.y) # 否则: else: # 移动到宝石的坐标。 hero.moveXY(item.pos.x, item.pos.y)
Brawler Hunt(狩猎斗士)
# Don't worry about small and medium-sized ogres.# Your targets are type "brawler".# When a "brawler" is closer than 50m, fire artillery.while True: # Find the nearest enemy and the distance to it. enemy = hero.findNearestEnemy() distance = hero.distanceTo(enemy) # If the enemy's type is "brawler" # AND the distance to it is less than 50 meters, # Then say "Fire!" to signal the artillery. if enemy.type == "brawler" and distance < 50: hero.say("Fire!") pass
发表评论
最新留言
不错!
[***.144.177.141]2025年03月30日 11时04分10秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
解决Genymotion无法拖拽的问题
2019-03-05
中国石油大学《计算机文化基础》在线考试(客观题)
2019-03-05
中国石油大学《 管理心理学(行政管理专业禁选)》在线考试
2019-03-05
机器学习(numpy/matplotlib/scipy)学习笔记
2019-03-05
HTML CSS JS 特殊字符表
2019-03-05
codeforces The Eternal Immortality 题解
2019-03-05
蓝桥杯 历届试题 幸运数 (堆+DFS)
2019-03-05
微信js-sdk使用简述(分享,扫码功能等)
2019-03-05
selenium 的介绍和爬取 jd数据
2019-03-05
python-selenium优化方案
2019-03-05
服务器 centos 系统漏洞快速修复简易方法
2019-03-05
【分享-一键在线抠图】在线免费去除图片背景
2019-03-05
图片预览自适应固定宽高div
2019-03-05
layui表格checkbox选择全选样式及功能
2019-03-05
mxsrvs支持thinkphp3.2伪静态
2019-03-05
mui HTML5 plus 下载文件
2019-03-05
环信SDK 踩坑记webIM篇(一)
2019-03-05
短信验证码倒计时代码
2019-03-05