Dinosaur hat: think like a snake, not like a farmer

The dinosaur hat is not a crop script. You put it on, apples appear if you can pay cactus, each apple grows a tail behind you, and you only get bones when you take the hat off. Read the idea first. Copy the long script last, if at all.

Hand-drawn cartoon of a drone in a dinosaur hat above a farm grid with one apple

Step 1 — Put the hat on

Nothing dinosaur-related happens until the drone wears the hat. One function, one hat name. After this line, if you have enough cactus, the game buys an apple and puts it under the drone.

python
change_hat(Hats.Dinosaur_Hat)

Only one drone can wear this hat. clear() also puts the straw hat back on and wipes the farm.

Step 2 — Why cactus matters

The apple is not planted with plant(). The game spends cactus from your inventory. If you have none, no apple appears and there is nothing to chase. Check the count before you start a long run.

python
if num_items(Items.Cactus) == 0:
    print("need cactus before the hat is useful")

Step 3 — Move once to eat, not to harvest

Standing on the apple is not enough. The next successful move eats it and adds one tail tile on the square you just left. harvest() is the wrong tool here. If you can afford it, a new apple is placed on a random empty tile. A planted crop on that tile blocks the spawn.

python
# hat is on, apple is under the drone
move(East)
# that move ate the apple and grew the first tail tile

Step 4 — Your tail is a wall

move() returns False if the next tile is your own tail. The last tail piece slides away as you move, so you may step onto that one tile. With the dinosaur hat on, the drone also cannot wrap around the farm edge. When the tail fills the whole farm, every move fails — that is how you know it is fully grown.

python
if can_move(North):
    move(North)
else:
    print("blocked by tail or the farm edge")

Step 5 — Ask the apple where the next one is

measure() on an apple returns a pair: the coordinates of the next apple. Do this while you are still standing on the current apple, before you move. After you eat, walk toward those two numbers. When you arrive, measure again.

python
next_x, next_y = measure()
print(next_x, next_y)

Step 6 — Bones only when the hat comes off

Switch to any other hat and the tail is harvested. You get n squared bones, where n is tail length: 1, 4, 9, 16, … A length-16 tail is 256 bones. You do not harvest the tail with harvest().

python
change_hat(Hats.Straw_Hat)
# tail is gone; inventory gained n**2 Items.Bone

Optional: one small practice farm

A full-size farm takes a long time to fill and you may not need that many bones. set_world_size() shrinks the grid (minimum 3) and also clears it. The effect ends when the program stops. Below is a practice loop that only uses ideas from the steps above: shrink, hat on, measure, walk, stop when blocked, hat off.

python
set_world_size(5)
clear()
change_hat(Hats.Dinosaur_Hat)

def walk_to(x, y):
    while get_pos_x() != x or get_pos_y() != y:
        if get_pos_x() < x:
            if not move(East):
                return False
        elif get_pos_x() > x:
            if not move(West):
                return False
        elif get_pos_y() < y:
            if not move(North):
                return False
        else:
            if not move(South):
                return False
    return True

while True:
    nxt = measure()
    if nxt == None:
        break
    next_x, next_y = nxt
    if not walk_to(next_x, next_y):
        break

change_hat(Hats.Straw_Hat)

This is a teaching path, not a leaderboard path. It will bump its own tail on purpose so you can see the stop. Wear the hat only after the farm is empty so apples can spawn.