CODECOMBAT_CPP_MODULE_02.EXE
LEVELS 8-13
🛰️

C++ 坐标与位置属性

Coordinates & Properties

.pos
对象位置属性
.x / .y
精确坐标运算
🌐

坐标系统 (Coordinate System)

CodeCombat 的地图是一个巨大的网格。每个点都由 (X, Y) 决定。

  • X 轴 (Horizontal):从左到右,数值变大。
  • Y 轴 (Vertical):从下到上,数值变大。
// 移动到具体坐标 (X=30, Y=40) hero.moveXY(30, 40);
(0,0)
📦

对象与属性 (Objects & Properties)

在 C++ 中,物品(Item)和敌人(Enemy)都是对象。对象里面存着数据,叫属性

使用 点号 (.) 来访问属性:

auto item = hero.findNearestItem(); if (item) { // 访问 item 的 pos 属性 auto position = item.pos; // 访问 pos 里的 x 和 y hero.moveXY(position.x, position.y); }
item.pos.x
⬇️
pos: { x: 45, y: 32 }
💰

实战:币屑 (Coinucopia)

直接通过属性链访问坐标,快速移动收集金币!

提示:item.pos.x 就是金币的横坐标。
int main() { while (true) { auto item = hero.findNearestItem(); if (item) { // 直接读取 x 和 y auto ix = item.pos.x; auto iy = item.pos.y; hero.moveXY(ix, iy); } } return 0; }
🏃 💨 🪙

Follow the properties

🐇

变量存储坐标 (Variables)

白兔伪装者关卡中,我们可以先把坐标存在变量里,方便多次使用。

auto item = hero.findNearestItem(); if (item) { auto pos = item.pos; // 存下位置对象 float x = pos.x; // 提取 X float y = pos.y; // 提取 Y hero.moveXY(x, y); }
📦 = 📍
(把位置存进盒子里)
🎯

坐标运算:风向校正

有时候直接瞄准是不够的!我们需要预判或者修正位置。

风向校正关卡中,大炮需要瞄准敌人下方一点点。

if (enemy) { // 获取敌人位置,但 Y 轴减去 4 float aimY = enemy.pos.y - 4; auto aimX = enemy.pos.x; // 报告坐标:数字会自动转文字拼接 hero.say(aimX + "," + aimY); }
👹
y - 4
📝

总结 (Recap)

访问属性:
object.pos.x (横坐标)
object.pos.y (纵坐标)
坐标修正:
y - 4 (向下移)
x + 10 (向右移)
字符串报告:
hero.say(x + "," + y);
🌟