编程农场 - 代码示例

游戏完整代码示例,包含详细解释。直接复制粘贴这些代码到游戏中,提升你的农场自动化技能。

示例1:基础自动收获

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

# 自动收获草地
while True:
    if can_harvest():
        harvest()

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

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

# 遍历整个农场收获
while True:
    if can_harvest():
        harvest()

    # 向东移动
    move(East)

    # 到达行尾时向北移动并返回行首
    if get_pos_x() == get_world_size() - 1:
        move(North)

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

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

# 第一列种草,第二列种灌木,第三列种胡萝卜
while True:
    if can_harvest():
        harvest()

    x = get_pos_x()

    if x == 0:
        # 第一列:草(不需要种植)
        pass
    elif x == 1:
        # 第二列:灌木
        plant(Entities.Bush)
    elif x == 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:树木棋盘格种植

树木需要棋盘格种植以避免相邻。

# 棋盘格模式种植树木和灌木
while True:
    if can_harvest():
        harvest()

    x = get_pos_x()
    y = get_pos_y()

    # 棋盘格判断
    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:资源优先级管理

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

# 根据资源量自动调整种植策略
while True:
    if can_harvest():
        harvest()

    x = get_pos_x()

    # 优先确保干草库存
    if num_items(Items.Hay) < 500:
        # 不种植,让草自然生长
        pass
    # 其次确保木材库存
    elif num_items(Items.Wood) < 300:
        plant(Entities.Bush)
    # 最后种植胡萝卜
    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倍速度。

# 自动购买水箱并浇水
while True:
    # 确保有足够的水箱
    if num_items(Items.Water_Tank) < 100:
        trade(Items.Empty_Tank)

    # 检测并浇水
    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:函数封装

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

# 定义移动到下一个方块的函数
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:向日葵能量优化

向日葵优化策略,只收获高能量的花。

# 测量并收获最高能量的向日葵
sunflowers = []
world_size = get_world_size()

# 种植并测量所有向日葵
for i in range(world_size * world_size):
    if get_ground_type() == Grounds.Turf:
        till()
    if num_items(Items.Sunflower_Seed) > 0 or trade(Items.Sunflower_Seed):
        plant(Entities.Sunflower)

    if get_entity_type() == Entities.Sunflower:
        sunflowers.append(measure())

    move(East)
    if get_pos_x() == 0:
        move(North)

# 找到最高能量值
max_val = 0
max_index = 0
for i in range(len(sunflowers)):
    if sunflowers[i] > max_val:
        max_val = sunflowers[i]
        max_index = i

# 移动到最高能量向日葵并收获
# (移动逻辑省略)
if measure() == max_val:
    if can_harvest():
        harvest()