CodeCombat探索地图
LEVEL COMMAND REFERENCE

关卡指令文档

这里按用途整理 CodeCombat 中常见的方法、属性和代码模式,帮助你快速看懂“能调用什么、参数怎么写、返回什么”。内容覆盖基础闯关、英雄与装备能力、宠物、事件和游戏开发等场景,代码提供清晰的 Python 与 JavaScript 语法高亮。

使用前须知

CodeCombat 的关卡指令不是每一关都完全相同。英雄、装备、关卡类型和所选编程语言,都会影响当前可用的方法。

判断标准:游戏代码编辑器中显示的关卡文档,是当前关卡可用指令的最终依据。若本站列出的方法无法调用,请先检查关卡限制、英雄与装备,不要通过猜测添加参数。
  • 对象:hero 表示自己的英雄;enemyitemfriend 通常是代码中保存的查找结果。
  • 方法:hero.moveXY(40, 30) 中,moveXY 是方法,坐标是参数。
  • 属性:hero.health 直接读取数值,不在末尾加括号。
  • 空结果:没有找到目标时,查找方法可能返回空值;使用目标前应先判断它是否存在。

对象与常用属性

属性用于读取对象的当前状态。部分属性只在特定关卡或单位上提供。

属性含义常见值
hero.pos英雄当前位置带有 xy 的位置对象
hero.health当前生命值数字
hero.maxHealth最大生命值数字
hero.gold当前金币数字
hero.time关卡已经过的时间秒数;需当前关卡或装备支持
unit.pos单位当前位置unit.pos.xunit.pos.y
unit.health单位当前生命值数字
unit.type单位类型例如 "soldier"
unit.team单位所属阵营文本;仅部分关卡需要
item.pos物品所在位置位置对象
item.value物品价值数字;视物品与关卡而定

常用指令速查

以下是最常见的学习与闯关指令。标签提示其通常依赖的能力类型。

hero.moveXY(x, y) 移动

移动到指定坐标。两个参数都必须是数字。

返回:无须使用返回值

hero.move(position) 移动

朝位置对象移动,常与 item.posenemy.pos 配合。

返回:无须使用返回值

hero.findNearestEnemy() 侦测

查找最近的敌人;没有敌人时可能返回空值。

返回:敌人对象或空值

hero.findEnemies() 侦测

取得当前可侦测到的敌人集合。

返回:敌人数组/列表

hero.findNearestItem() 侦测

查找最近的可见物品。

返回:物品对象或空值

hero.findItems() 侦测

取得当前可侦测到的物品集合。

返回:物品数组/列表

hero.distanceTo(target) 位置

计算英雄与目标对象或位置之间的距离。

返回:数字

hero.attack(enemy) 战斗

攻击指定敌人,需要武器及关卡支持。

返回:无须使用返回值

hero.isReady("ability") 技能

判断某个技能是否已经结束冷却,可把技能名替换为当前可用能力。

返回:布尔值

hero.canCast("spell", target) 施法

判断指定法术当前能否对目标施放;有些法术不需要目标参数。

返回:布尔值

hero.say(value) 交流

让英雄说出文本或数值;部分关卡用它传递口令或调试信息。

返回:无须使用返回值

hero.findNearest(objects) 侦测

从给定的对象集合中找出距离英雄最近的对象。

返回:对象或空值

移动与位置

moveXY 接收两个坐标数字;move 接收一个位置对象。不要把完整对象误写成 x 坐标。

Python · 移动到物品
item = hero.findNearestItem()
if item:
    hero.move(item.pos)
JavaScript · 移动到物品
const item = hero.findNearestItem();
if (item) {
    hero.move(item.pos);
}

侦测与查找

“最近目标”方法适合处理一个对象;“查找全部”方法适合遍历、计数或筛选。使用最近目标前要先判断结果是否存在。

hero.findFriends()

取得当前可见或可指挥的友方单位集合。

返回:友方数组/列表

hero.findNearestFriend()

查找最近的友方单位。

返回:友方对象或空值

hero.findByType(type, objects)

按类型筛选对象。第二个集合参数是否可用取决于关卡文档。

返回:匹配对象的数组/列表

hero.findFlag()

查找玩家放置的旗帜;仅在支持旗帜的关卡中使用。

返回:旗帜对象或空值

战斗、技能与施法

普通攻击、特殊技能和法术通常依赖英雄或装备。使用带冷却时间的能力前先调用 isReady;施法前优先使用 canCast

hero.attack(enemy)

使用当前武器攻击敌人。

hero.shield()

举盾进行防御,需要盾牌提供该能力。

hero.cleave(enemy)

使用顺劈斩;先用 isReady("cleave") 判断是否就绪。

hero.cast("spell", target)

施放指定法术。参数随法术而异,应严格按照当前关卡文档填写。

hero.bash(enemy)

用盾牌猛击敌人;需要提供盾击能力的盾牌。

hero.backstab(enemy)

对目标发动背刺;范围、伤害和冷却由装备决定。

hero.heal(target)

治疗目标;仅在英雄、装备或关卡提供治疗能力时可用。

hero.throw(enemy)

使用支持投掷的武器攻击目标;部分特殊关卡可能改用坐标参数。

物品与旗帜

物品通常提供金币、宝石或关卡目标;旗帜只在支持玩家放旗的关卡中出现。

hero.findItems()

取得可侦测到的全部物品。

返回:物品数组/列表

hero.findNearestItem()

取得最近的可见物品;使用前先判断是否存在。

返回:物品对象或空值

hero.findFlags()

取得当前已放置的旗帜集合。

返回:旗帜数组/列表

hero.findFlag(color)

查找旗帜;部分关卡允许通过颜色筛选。

返回:旗帜对象或空值

hero.pickUpFlag(flag)

让英雄前往并拾取指定旗帜。

hero.removeFlag(flag)

移除指定旗帜;是否可用取决于关卡。

建造、召唤与指挥

以下方法只在相应关卡、装备或英雄支持时出现。可建造和召唤的类型也由当前场景决定。

hero.buildXY(type, x, y)

在坐标处建造指定类型,例如关卡允许的围栏、陷阱或召唤物。

hero.build(type, x, y)

部分建造或塔防关卡使用的建造写法;位置参数以关卡文档为准。

hero.costOf(type)

查询召唤某种单位所需的金币。

返回:数字

hero.summon(type)

召唤指定类型的友方单位。

hero.command(friend, action, target)

命令友方单位执行动作。第三个参数可以是目标对象或位置,取决于动作。

hero.findFriends()

取得友方单位,通常用于循环下达命令。

返回:友方数组/列表

hero.findByType(type, objects)

从单位集合中筛选某一类型;第二参数是否必需由关卡决定。

hero.gold

读取当前金币,常用于召唤前的费用判断。

返回:数字

宠物指令

宠物拥有自己的方法,并可通过事件执行任务。具体能力取决于已装备宠物和当前关卡。

pet.on(eventType, handler)

为宠物注册事件处理函数,例如响应生成事件。

pet.fetch(item)

让宠物取回可搬运物品。

pet.moveXY(x, y)

让宠物移动到指定坐标。

pet.say(message)

让宠物说出信息;部分关卡用于通信或调试。

pet.findNearestByType(type)

查找距离宠物最近的指定类型对象。

pet.isReady(ability)

判断宠物能力是否就绪。

返回:布尔值

pet.chase(enemy)

让支持该能力的宠物追赶敌人。

pet.coldBlast()

使用寒冰冲击等宠物专属能力;需要对应宠物支持。

事件处理

事件让代码在“某件事发生时”自动调用函数,常用于宠物关卡和游戏开发。先定义处理函数,再用 on 注册。

Python · 宠物响应 spawn 事件
def on_spawn(event):
    item = hero.findNearestItem()
    if item:
        pet.fetch(item)

pet.on("spawn", on_spawn)
JavaScript · 宠物响应 spawn 事件
function onSpawn(event) {
    const item = hero.findNearestItem();
    if (item) {
        pet.fetch(item);
    }
}

pet.on("spawn", onSpawn);

hero.on(event, handler)

在支持事件的特殊关卡中,为英雄注册处理函数。

unit.on(event, handler)

游戏开发关卡中,为单位的生成、碰撞、击败等事件设置处理逻辑。

游戏开发指令

游戏开发关卡常使用 game 对象创建场景、单位、目标与界面。不同开发关卡开放的资源名称和事件并不相同。

game.spawn(type)

在支持该竞技模板的关卡中,从基地生成指定类型单位。

game.spawnArray(types)

按数组批量生成单位;费用与可用类型由对应模板决定。

game.setBackground(name)

设置游戏背景资源。

game.setMapSize(name)

使用关卡提供的尺寸名称设置地图大小。

game.addTextDirections(text)

在游戏界面中显示玩法说明。

game.setActionFor(type, event, handler)

为某一类单位设置 spawn、attack、defend 或 update 等行为。

game.setPatrolFor(type, range, points)

为指定类型单位设置巡逻范围与路径点。

game.randomInt(max)

生成 0 到上限范围内的随机整数;边界规则以关卡文档为准。

返回:数字

game.time / game.score

读取已经过的游戏时间或当前分数。

返回:数字

game.win()

在自定义获胜条件满足时结束游戏并判定胜利。

ui.track(object, property)

把对象的指定属性显示在游戏界面上。

特殊关卡与竞技指令

原始资料还包含塔防、波次、元素地图、多人竞技和运动类关卡指令。它们只在对应关卡出现,不应直接套用到普通冒险关卡。

hero.getWaterMap() / waterAtXY(x, y)

读取水域地图或判断坐标的水元素状态。

hero.getLavaMap() / lavaAtXY(x, y)

读取岩浆地图或判断坐标的岩浆状态。

hero.getSolarMap() / solarAtXY(x, y)

读取太阳能地图或判断坐标状态。

hero.getWaves() / getCurrentWave()

读取波次信息和当前波次。

hero.getTowerAt(place)

取得指定位置的塔,配合建造、升级或售卖逻辑。

hero.getTowerParameters(type, level)

读取塔类型在指定等级的参数。

hero.getUnitParameters(type)

读取特殊竞技关卡中的单位参数。

hero.getSpellData(name)

读取特殊关卡中某个法术的费用或其他数据。

hero.opponent

取得对手英雄对象;仅在支持对战的关卡中存在。

hero.score / hero.opponentScore

读取自己与对手的得分。

完整示例

这些示例展示安全的基本结构。技能、法术和单位类型需要替换成当前关卡实际提供的内容。

Python · 持续侦测与战斗
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        else:
            hero.attack(enemy)
JavaScript · 持续侦测与战斗
while (true) {
    const enemy = hero.findNearestEnemy();
    if (enemy) {
        if (hero.isReady("cleave")) {
            hero.cleave(enemy);
        } else {
            hero.attack(enemy);
        }
    }
}
Python · 召唤并指挥士兵
soldier_cost = hero.costOf("soldier")
if hero.gold >= soldier_cost:
    hero.summon("soldier")

enemy = hero.findNearestEnemy()
if enemy:
    for friend in hero.findFriends():
        hero.command(friend, "attack", enemy)
JavaScript · 召唤并指挥士兵
const soldierCost = hero.costOf("soldier");
if (hero.gold >= soldierCost) {
    hero.summon("soldier");
}

const enemy = hero.findNearestEnemy();
if (enemy) {
    for (const friend of hero.findFriends()) {
        hero.command(friend, "attack", enemy);
    }
}

完整指令索引

下面汇总原始资料中最常出现的可编程对象与指令名称,方便按英文名称定位。原始资料还包含大量内部组件、比赛专用能力和关卡裁判字段,它们不一定能由玩家代码直接调用。

英雄、单位与物品指令
hero.moveXY(x, y)hero.move(position)hero.moveUp(times)hero.moveDown(times)hero.moveLeft(times)hero.moveRight(times)hero.moveForward(times)hero.moveTo(position)hero.moveToward(position)hero.turn(direction)hero.jump()hero.dash()hero.wait(seconds)hero.distanceTo(target)hero.findNearest(objects)hero.findNearestEnemy()hero.findNearestFriend()hero.findEnemies()hero.findFriends()hero.findItems()hero.findNearestItem()hero.findByType(type)hero.findCorpses()hero.findEnemyMissiles()hero.findFriendlyMissiles()hero.findFlags()hero.findFlag(color)hero.pickUpFlag(flag)hero.removeFlag(flag)hero.attack(enemy)hero.attackXY(x, y)hero.shield()hero.cleave(enemy)hero.bash(enemy)hero.backstab(enemy)hero.heal(target)hero.cast(spell, target)hero.canCast(spell, target)hero.isReady(ability)hero.say(value)hero.buildXY(type, x, y)hero.build(type, place)hero.costOf(type)hero.summon(type)hero.command(friend, action, target)hero.on(event, handler)hero.healthhero.maxHealthhero.goldhero.timehero.poshero.xhero.yhero.typehero.teamunit.move(position)unit.moveXY(x, y)unit.distanceTo(target)unit.findNearestEnemy()unit.findEnemies()unit.findFriends()unit.attack(target)unit.healthunit.maxHealthunit.maxSpeedunit.attackDamageunit.attackRangeunit.posunit.typeunit.teamitem.positem.valueflag.posflag.color
宠物、事件与游戏开发指令
pet.on(event, handler)pet.fetch(item)pet.moveXY(x, y)pet.say(message)pet.findNearestByType(type)pet.isReady(ability)pet.chase(enemy)pet.carryUnit(target, x, y)pet.catchProjectile(projectile)pet.charm(enemy)pet.coldBlast()pet.distractionNoise()pet.quack()pet.shapeShift()game.spawn(type)game.spawnArray(types)game.costOf(type)game.costOfSpawnArray(types)game.setBackground(name)game.setEnvironment(name)game.setMapSize(name)game.setGravity(strength)game.addTextDirections(text)game.setActionFor(type, event, handler)game.changeActionFor(type, event)game.setPatrolFor(type, range, points)game.changePatrolFor(type, range, points)game.removeActionForUnit(unit)game.randomInt(max)game.on(event, handler)game.say(text)game.win()game.endStory()game.timegame.scoreui.track(object, property)
塔防、波次、元素地图与竞技指令
hero.get2dMap()hero.getWaterMap()hero.waterAtXY(x, y)hero.getLavaMap()hero.lavaAtXY(x, y)hero.getSolarMap()hero.solarAtXY(x, y)hero.getObstacleAt(x, y)hero.getWaves()hero.getWave(number)hero.getWaveNumber()hero.getCurrentWave()hero.getNextWave()hero.findTowers()hero.findMyTowers()hero.findEnemyTowers()hero.getTowerAt(place)hero.getTowerParameters(type, level)hero.setTargeting(tower, type)hero.sell(place)hero.findMyUnits()hero.findEnemyUnits()hero.getUnitParameters(type)hero.getSpellData(name)hero.getSpellParameters(name)hero.getTotalMana(type)hero.opponenthero.scorehero.opponentScorehero.roundhero.roundTimehero.gameTimehero.gameActive
查看完整英文原始资料

下方直接载入项目中的 api英文文档.txt,保留其全部组件、系统和方法记录。它适合高级检索,不应被理解为每一关都能直接使用的玩家指令。

在新页面打开原始资料

排错清单

出现错误或英雄没有按预期行动时,按下面顺序检查通常最快。

  1. 方法名、变量名和字符串是否拼写正确,英文大小写是否一致。
  2. Python 缩进、JavaScript 花括号和括号是否完整。
  3. 目标是否可能为空;是否在使用前写了 if enemy:if (enemy)
  4. 参数数量和类型是否符合当前关卡文档,例如坐标数字与位置对象不要混用。
  5. 技能是否冷却完成,法术目标是否合法。
  6. 当前英雄、装备和关卡是否真正提供这个方法。
仍有问题?查看常见问题
← 返回 CodeCombat探索地图