编程农场代码 — 可复制收割脚本

先粘贴下面第一段,开始收割。树木、胡萝卜、浇水在后面。胡萝卜完整页: 胡萝卜代码页

示例1:基础自动收获

最简单的自动化脚本,持续收获成熟的草。

python
# 基础自动收获 - 最简单的自动化脚本
# 这会持续检查并收获成熟的作物
while True:
    if can_harvest():
        harvest()
    # 游戏会在迭代之间自动暂停

📊 Code Analysis

6
Lines
0
Functions
1
Loops
1
Conditions

示例2:3×3农场自动遍历

自动遍历所有方块并收获。

python
# 3x3农场自动遍历 - 系统性的耕作模式
# 这个脚本以网格模式移动,覆盖整个农场
while True:
    # 总是先检查可收获的作物
    if can_harvest():
        harvest()

    # 移动到下一个格子(东方向)
    move(East)

    # 当到达行末尾时,移动到下一行(北方向)
    # get_world_size() 返回你的农场大小(通常是3x3或5x5)
    if get_pos_x() == get_world_size() - 1:
        move(North)

示例3:多作物自动化(草+灌木+胡萝卜)

根据列位置种植不同作物。

python
# 多作物自动化 - 按列种植不同作物
# 演示条件逻辑和资源管理
while True:
    # 总是先收获已准备好的作物
    if can_harvest():
        harvest()

    # 获取当前位置(x坐标)
    x = get_pos_x()

    # 基于列的种植策略:
    if x == 0:
        # 第0列:让草自然生长(免费资源)
        pass  # 无需操作
    elif x == 1:
        # 第1列:种植灌木获取木材
        plant(Entities.Bush)
    elif x == 2:
        # 第2列:种植胡萝卜(需要土壤准备)
        # 检查地面是否为土壤,如果不是就耕作
        if get_ground_type() != Grounds.Soil:
            till()
        # 确保有胡萝卜种子再种植
        if num_items(Items.Carrot_Seed) < 1:
            trade(Items.Carrot_Seed)  # 需要时购买种子
        plant(Entities.Carrot)

    # 移动到下一列
    move(East)
    # 当到达行末尾时,移动到下一行
    if x == get_world_size() - 1:
        move(North)

示例4:树木棋盘格种植

树木需要棋盘格种植以避免相邻。完整说明: 树木代码页

python
# 棋盘格种植模式 - 空间高效的耕作
# 树木不能相邻种植,因此使用棋盘格模式
while True:
    # 收获任何成熟的作物
    if can_harvest():
        harvest()

    # 获取当前位置坐标
    x = get_pos_x()  # 当前列(0, 1, 2, 等)
    y = get_pos_y()  # 当前行(0, 1, 2, 等)

    # 棋盘格逻辑:在"黑格"中种树,在"白格"中种灌木
    # 这确保没有两棵树相邻(树需要生长空间)
    if (x % 2 == 0 and y % 2 == 0) or (x % 2 == 1 and y % 2 == 1):
        # "黑格" - 种植树木
        plant(Entities.Tree)
    else:
        # "白格" - 种植灌木
        plant(Entities.Bush)

    # 移动到下一个格子
    move(East)
    # 当到达当前行末尾时,换到下一行
    if x == get_world_size() - 1:
        move(North)

示例5:资源优先级管理

智能资源管理,优先收集稀缺资源。

python
# 资源优先级管理 - 智能资源分配
# 根据当前库存水平自动调整耕作策略
while True:
    # 总是先收获 - 这是我们的主要行动
    if can_harvest():
        harvest()

    # 获取当前位置用于种植决策
    x = get_pos_x()

    # 基于资源的种植策略,按优先级排序:

    # 优先级1:确保干草供应(交易和喂养必需品)
    if num_items(Items.Hay) < 500:
        # 什么都不种 - 让草自然生长获得免费干草
        pass  # 无需操作

    # 优先级2:维持木材供应(各种升级所需)
    elif num_items(Items.Wood) < 300:
        # 种植灌木生成木材
        plant(Entities.Bush)

    # 优先级3:建立胡萝卜储备(食物和高级交易)
    elif num_items(Items.Carrot) < 200:
        # 种植胡萝卜(需要土壤准备)
        if get_ground_type() != Grounds.Soil:
            till()  # 需要时准备土壤
        if num_items(Items.Carrot_Seed) == 0:
            trade(Items.Carrot_Seed)  # 如果没有种子就购买
        plant(Entities.Carrot)

    # 以系统化模式移动到下一个格子
    move(East)
    if x == get_world_size() - 1:
        move(North)

示例6:自动浇水系统

自动化浇水系统,加速作物生长至5倍速度。

python
# 自动浇水系统 - 高级作物加速
# 维持水箱供应,将作物生长速度加速至5倍
while True:
    # 水资源管理:确保充足的水供应
    # 水箱对于加速作物生长至关重要
    if num_items(Items.Water_Tank) < 100:
        # 如果没有足够的空水箱就交易获取
        trade(Items.Empty_Tank)

    # 水分应用:作物需要时使用水
    # get_water() 返回水分水平(0.0到1.0)
    # 0.75意味着作物水分达75% - 低于此值时浇水
    if get_water() < 0.75:
        use_item(Items.Water_Tank)  # 对当前格子应用水

    # 收获:总是先检查成熟的作物
    if can_harvest():
        harvest()

    # 种植:维持胡萝卜作物周期
    # 需要时准备土壤(胡萝卜需要耕作过的土壤)
    if get_ground_type() != Grounds.Soil:
        till()  # 将草地转换为土壤
    # 确保种植前有种子
    if num_items(Items.Carrot_Seed) < 1:
        trade(Items.Carrot_Seed)  # 需要时购买种子
    plant(Entities.Carrot)  # 种植胡萝卜种子

    # 移动:继续系统化遍历
    move(East)
    if get_pos_x() == get_world_size() - 1:
        move(North)

示例7:函数封装

使用函数使代码更清晰易读。

python
# 函数封装 - 代码组织和重用性
# 将复杂逻辑分解为可重用的函数

# 函数:处理网格模式下移动到下一个格子的逻辑
def move_to_next():
    """移动到下一个格子,到达行末时换到下一行"""
    x = get_pos_x()
    move(East)  # 总是先尝试向东移动
    # 如果到达当前行末尾,向北移动到下一行
    if x == get_world_size() - 1:
        move(North)

# 函数:处理胡萝卜种植的所有必要准备工作
def plant_carrot():
    """种植胡萝卜,包括土壤准备和种子管理"""
    # 确保有适合胡萝卜的土壤
    if get_ground_type() != Grounds.Soil:
        till()  # 需要时准备土壤
    # 确保有胡萝卜种子
    if num_items(Items.Carrot_Seed) < 1:
        trade(Items.Carrot_Seed)  # 如果没有就购买种子
    # 种植胡萝卜
    plant(Entities.Carrot)

# 主程序:使用函数的清晰、可读逻辑
while True:
    # 收获任何成熟的作物(总是先做这个)
    if can_harvest():
        harvest()

    # 如果库存不足,维持胡萝卜生产
    if num_items(Items.Carrot) < 100:
        plant_carrot()  # 使用我们的函数保持代码整洁

    # 使用移动函数移动到下一个位置
    move_to_next()  # 整洁、可重用的移动逻辑

示例8:向日葵能量优化

只收瓣数最多的那朵,然后再扫一遍。完整说明: 向日葵代码页

python
def go_to(x, y):
    while get_pos_x() != x:
        if get_pos_x() < x:
            move(East)
        else:
            move(West)
    while get_pos_y() != y:
        if get_pos_y() < y:
            move(North)
        else:
            move(South)

size = get_world_size()
best = 0
bx = 0
by = 0
found = False
for y in range(size):
    for x in range(size):
        go_to(x, y)
        if get_entity_type() == Entities.Sunflower and can_harvest():
            petals = measure()
            if (not found) or petals > best:
                best = petals
                bx = x
                by = y
                found = True
if found:
    go_to(bx, by)
    harvest()