API 与对象
约 1038 字大约 3 分钟
2026-09-23
游戏里的函数不是完整 Python 标准库,能用什么取决于对应的解锁项
移动与感知
| 函数 | 返回值 | 用途 |
|---|---|---|
move(direction) | bool | 向一个方向移动一格,越过边界会从另一侧出现 |
can_move(direction) | bool | 检查是否可以移动,迷宫里尤其有用 |
get_pos_x() / get_pos_y() | int | 获取当前坐标,西南角为 (0, 0) |
get_world_size() | int | 获取农场边长 |
get_entity_type() | Entity 或 None | 获取当前格的实体 |
get_ground_type() | Ground | 获取当前格地面类型 |
get_water() | float | 获取当前格含水量,范围约为 0 到 1 |
坐标方向固定为 West → East 增大 x,South → North 增大 y
x = get_pos_x()
y = get_pos_y()
size = get_world_size()
for _ in range(size):
move(North)种植与收获
| 函数 | 返回值 | 用途 |
|---|---|---|
plant(entity) | bool | 在脚下种植实体,资源或地面不满足时返回 False |
harvest() | bool | 收获脚下实体,不可收获时会失败 |
can_harvest() | bool | 判断脚下作物是否成熟 |
till() | None | 在 Grassland 和 Soil 之间切换 |
swap(direction) | None | 和相邻格交换实体,仙人掌排序的核心函数 |
use_item(item, n=1) | bool | 使用水、肥料等物品 |
measure(direction=None) | int 或 tuple 或 None | 测量向日葵花瓣、仙人掌大小、南瓜编号、宝藏位置等 |
measure() 的返回值取决于实体,不能把它当成所有作物通用的数值接口
if get_ground_type() != Grounds.Soil:
till()
if get_water() < 0.5:
use_item(Items.Water)
if can_harvest():
harvest()资源与解锁
| 函数 | 用途 |
|---|---|
num_items(item) | 查询背包里物品数量 |
get_cost(thing) | 查询种植实体或解锁节点所需资源,返回字典 |
unlock(unlock) | 用代码购买解锁节点 |
num_unlocked(thing) | 查询解锁状态或升级等级 |
get_companion() | 查询多样性种植中当前植物需要的伴生植物 |
clear() | 清空农场并把当前无人机送回 (0, 0) |
num_unlocked() 对可升级节点返回 1 + 升级次数,对普通解锁返回 0 或 1
cost = get_cost(Unlocks.Pumpkin)
for item in cost:
if num_items(item) < cost[item]:
quick_print("资源不足", item)多无人机
| 函数 | 用途 |
|---|---|
spawn_drone(task, *args) | 在当前无人机位置生成无人机并执行任务,失败时返回 None |
num_drones() | 当前正在运行的无人机数量 |
max_drones() | 当前最多可用的无人机数量 |
has_finished(drone) | 检查无人机是否结束 |
wait_for(drone) | 等待无人机结束,并返回任务函数的返回值 |
子无人机会从调用 spawn_drone() 的位置开始,额外参数会复制给任务函数
def harvest_row(message):
for _ in range(get_world_size()):
if can_harvest():
harvest()
move(East)
quick_print(message)
drone = spawn_drone(harvest_row, "done")
if drone is not None:
wait_for(drone)计时、调试与排行榜
| 函数 | 用途 |
|---|---|
get_tick_count() | 查询当前代码执行的 tick 数,适合比较算法开销 |
get_time() | 查询当前游戏时间,适合测量实际耗时 |
print(value) | 在无人机上方显示输出,会消耗时间 |
quick_print(value) | 写入输出页,几乎不影响计时 |
leaderboard_run(board, file, speedup) | 启动固定条件的排行榜模拟 |
simulate(file, unlocks, items, globals, seed, speedup) | 用自定义初始条件运行模拟 |
排行榜和模拟程序必须自己结束,达到目标后继续 while True 不会自动提交成绩
常量对象
Entities
Grass、Bush、Tree、Carrot、Pumpkin、Sunflower、Cactus、Hedge、Treasure、Dinosaur
枯死南瓜在相关版本中使用 Entities.Dead_Pumpkin 判断,具体以游戏内自动补全为准
Items
Hay、Wood、Carrot、Carrot_Seed、Pumpkin、Pumpkin_Seed、Sunflower_Seed、Power、Water、Fertilizer、Egg、Cactus、Cactus_Seed、Bone、Gold、Weird_Substance
Grounds 与方向
Grounds.Grassland
Grounds.Soil
North South East WestUnlocks
常见节点包括 Loops、Variables、Operators、Functions、Lists、Dictionaries、Senses、Speed、Expand、Grass、Trees、Carrots、Pumpkins、Watering、Fertilizer、Sunflowers、Cactus、Mazes、Dinosaurs、Polyculture、Megafarm、Import、Simulation、Leaderboard、Utilities、Debug
操作耗时
- 查询类函数一般是
1tick - 成功移动、种植、收获、交换和使用物品通常是
200ticks - 失败的可失败操作通常只消耗
1tick len()、索引、break、continue、return等基础操作有单独的低成本规则
做排行榜优化时,先减少成功的 move()、plant()、harvest() 和 swap(),单纯减少查询调用通常收益有限
