{
  "format": "tokbit",
  "version": 1,
  "name": "Landing on a heavier Moon",
  "main": [
    {
      "id": "u49",
      "kind": "call",
      "call": "intro()"
    },
    {
      "id": "u50",
      "kind": "declare",
      "name": "lander",
      "type": "list[str]",
      "init": "[\"................\", \"......####......\", \".....######.....\", \"....##.##.##....\", \"....########....\", \".....######.....\", \"....########....\", \"...##########...\", \"...#.######.#...\", \"..##........##..\", \"..#..........#..\", \".##..........##.\", \"................\", \"................\", \"................\", \"................\"]",
      "comment": "The module's shape without a flame."
    },
    {
      "id": "u51",
      "kind": "declare",
      "name": "flame",
      "type": "list[str]",
      "init": "[\"................\", \"......####......\", \".....######.....\", \"....##.##.##....\", \"....########....\", \".....######.....\", \"....########....\", \"...##########...\", \"...#.######.#...\", \"..##........##..\", \"..#..........#..\", \".##..........##.\", \"......####......\", \"......####......\", \".......##.......\", \"................\"]",
      "comment": "The same module with the engine flame: switching makes the animation."
    },
    {
      "id": "u52",
      "kind": "declare",
      "name": "blast1",
      "type": "list[str]",
      "init": "[\"................\", \"................\", \"......#..#......\", \"...#..#..#..#...\", \"....#.####.#....\", \".....######.....\", \"..##########....\", \".....######.##..\", \"....#.####.#....\", \"...#..#..#..#...\", \"......#..#......\", \"................\", \"................\", \"................\", \"................\", \"................\"]"
    },
    {
      "id": "u53",
      "kind": "declare",
      "name": "blast2",
      "type": "list[str]",
      "init": "[\"#.......#......#\", \"...#.........#..\", \".#....#..#....#.\", \"....#......#....\", \"#.............#.\", \"...#...##...#...\", \".#............#.\", \"......#..#......\", \"#..#........#..#\", \".......#........\", \"..#..#....#..#..\", \"#..............#\", \"....#..#..#.....\", \".#............#.\", \"...#...#...#....\", \"#..............#\"]"
    },
    {
      "id": "u54",
      "kind": "declare",
      "name": "pad",
      "type": "int",
      "init": "randint(0, 14)",
      "comment": "The landing pad: two stretches of ground from point pad, that is 40 points."
    },
    {
      "id": "u55",
      "kind": "declare",
      "name": "pad_height",
      "type": "int",
      "init": "randint(150, 175)"
    },
    {
      "id": "u56",
      "kind": "declare",
      "name": "ground",
      "type": "list[int]",
      "init": "[]",
      "comment": "The height of the surface every 20 points from left to right: 17 points across the width of 320."
    },
    {
      "id": "u57",
      "kind": "for",
      "variable": "i",
      "range": "17",
      "body": [
        {
          "id": "u58",
          "kind": "if",
          "condition": "pad <= i <= pad + 2",
          "then": [
            {
              "id": "u59",
              "kind": "call",
              "call": "ground.append(pad_height)"
            }
          ],
          "else": [
            {
              "id": "u60",
              "kind": "call",
              "call": "ground.append(randint(120, 190))"
            }
          ]
        }
      ]
    },
    {
      "id": "u61",
      "kind": "call",
      "call": "stars()"
    },
    {
      "id": "u62",
      "kind": "call",
      "call": "draw_ground(ground, pad)"
    },
    {
      "id": "u63",
      "kind": "call",
      "call": "sprite_shape(0, lander)"
    },
    {
      "id": "u64",
      "kind": "call",
      "call": "sprite_color(0, WHITE)"
    },
    {
      "id": "u65",
      "kind": "declare",
      "name": "x",
      "type": "float",
      "init": "152.0"
    },
    {
      "id": "u66",
      "kind": "declare",
      "name": "y",
      "type": "float",
      "init": "16.0"
    },
    {
      "id": "u67",
      "kind": "declare",
      "name": "speed",
      "type": "float",
      "init": "0.0",
      "comment": "Vertical speed in points per frame; positive is down."
    },
    {
      "id": "u68",
      "kind": "declare",
      "name": "fuel",
      "type": "int",
      "init": "100"
    },
    {
      "id": "u69",
      "kind": "declare",
      "name": "thrust",
      "type": "int",
      "init": "0",
      "comment": "For how many more frames the engine runs after a press."
    },
    {
      "id": "u70",
      "kind": "declare",
      "name": "flying",
      "type": "bool",
      "init": "True"
    },
    {
      "id": "u71",
      "kind": "while",
      "condition": "flying",
      "body": [
        {
          "id": "u72",
          "kind": "declare",
          "name": "k",
          "type": "str",
          "init": "key()"
        },
        {
          "id": "u73",
          "kind": "if",
          "condition": "(k == \"up\" or k == \" \") and fuel > 0",
          "then": [
            {
              "id": "u74",
              "kind": "assign",
              "target": "thrust",
              "value": "4"
            }
          ],
          "else": []
        },
        {
          "id": "u75",
          "kind": "if",
          "condition": "k == \"left\"",
          "then": [
            {
              "id": "u76",
              "kind": "assign",
              "target": "x",
              "value": "max(0.0, x - 4)"
            }
          ],
          "else": []
        },
        {
          "id": "u77",
          "kind": "if",
          "condition": "k == \"right\"",
          "then": [
            {
              "id": "u78",
              "kind": "assign",
              "target": "x",
              "value": "min(304.0, x + 4)"
            }
          ],
          "else": []
        },
        {
          "id": "u79",
          "kind": "if",
          "condition": "thrust > 0 and fuel > 0",
          "then": [
            {
              "id": "u80",
              "kind": "assign",
              "target": "speed",
              "value": "speed - 0.15"
            },
            {
              "id": "u81",
              "kind": "assign",
              "target": "fuel",
              "value": "fuel - 1"
            },
            {
              "id": "u82",
              "kind": "assign",
              "target": "thrust",
              "value": "thrust - 1"
            },
            {
              "id": "u83",
              "kind": "call",
              "call": "sprite_shape(0, flame)"
            },
            {
              "id": "u84",
              "kind": "call",
              "call": "beep(70, 15)"
            }
          ],
          "else": [
            {
              "id": "u85",
              "kind": "assign",
              "target": "thrust",
              "value": "0"
            },
            {
              "id": "u86",
              "kind": "call",
              "call": "sprite_shape(0, lander)"
            }
          ],
          "comment": "The engine: thrust against gravity, uses fuel, shows the flame and hums."
        },
        {
          "id": "u87",
          "kind": "assign",
          "target": "speed",
          "value": "speed + 0.08",
          "comment": "Gravity: a bit faster downwards every frame."
        },
        {
          "id": "u88",
          "kind": "assign",
          "target": "y",
          "value": "max(16.0, y + speed)"
        },
        {
          "id": "u89",
          "kind": "call",
          "call": "sprite_at(0, round(x), round(y))"
        },
        {
          "id": "u90",
          "kind": "call",
          "call": "show_status(max(0, surface(ground, round(x) + 8) - round(y) - 11), speed, fuel)"
        },
        {
          "id": "u91",
          "kind": "if",
          "condition": "touching(round(x), round(y))",
          "then": [
            {
              "id": "u92",
              "kind": "assign",
              "target": "flying",
              "value": "False"
            }
          ],
          "else": []
        },
        {
          "id": "u93",
          "kind": "call",
          "call": "sleep(40)"
        }
      ],
      "comment": "The game loop: key, engine, gravity, movement and status until the module touches the surface."
    },
    {
      "id": "u94",
      "kind": "if",
      "condition": "landed(round(x), round(y), speed)",
      "then": [
        {
          "id": "u95",
          "kind": "call",
          "call": "sprite_shape(0, lander)"
        },
        {
          "id": "u96",
          "kind": "call",
          "call": "celebrate()"
        }
      ],
      "else": [
        {
          "id": "u97",
          "kind": "call",
          "call": "explosion(round(x), round(y), blast1, blast2)"
        }
      ]
    }
  ],
  "functions": [
    {
      "name": "draw_ground",
      "params": [
        {
          "name": "ground",
          "type": "list[int]"
        },
        {
          "name": "pad",
          "type": "int"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u1",
          "kind": "call",
          "call": "color(DARK_GREY)"
        },
        {
          "id": "u2",
          "kind": "for",
          "variable": "x",
          "range": "320",
          "body": [
            {
              "id": "u3",
              "kind": "call",
              "call": "line(x, surface(ground, x), x, 199)"
            }
          ],
          "comment": "Fill: a vertical line under every point of the surface down to the bottom edge."
        },
        {
          "id": "u4",
          "kind": "call",
          "call": "color(GREY)"
        },
        {
          "id": "u5",
          "kind": "for",
          "variable": "i",
          "range": "16",
          "body": [
            {
              "id": "u6",
              "kind": "call",
              "call": "line(i * 20, ground[i], i * 20 + 20, ground[i + 1])"
            }
          ],
          "comment": "Outline: slopes and craters as lines between ground points 20 points apart."
        },
        {
          "id": "u7",
          "kind": "call",
          "call": "color(YELLOW)"
        },
        {
          "id": "u8",
          "kind": "call",
          "call": "rect(pad * 20, ground[pad], 40, 4, True)"
        }
      ],
      "comment": "The Moon's surface from lines: vertical lines fill the rocks, lighter lines trace the slopes and craters, a yellow rectangle is the landing pad."
    },
    {
      "name": "surface",
      "params": [
        {
          "name": "ground",
          "type": "list[int]"
        },
        {
          "name": "x",
          "type": "int"
        }
      ],
      "returnType": "int",
      "blocks": [
        {
          "id": "u9",
          "kind": "declare",
          "name": "i",
          "type": "int",
          "init": "x // 20"
        },
        {
          "id": "u10",
          "kind": "return",
          "value": "ground[i] + (ground[i + 1] - ground[i]) * (x % 20) // 20"
        }
      ],
      "comment": "The height of the surface at point x: it lies on the line between two neighbouring ground points."
    },
    {
      "name": "stars",
      "params": [],
      "returnType": null,
      "blocks": [
        {
          "id": "u11",
          "kind": "call",
          "call": "color(WHITE)"
        },
        {
          "id": "u12",
          "kind": "for",
          "variable": "i",
          "range": "40",
          "body": [
            {
              "id": "u13",
              "kind": "call",
              "call": "plot(randint(0, 319), randint(10, 110))"
            }
          ]
        }
      ],
      "comment": "Stars in the sky: white points in random places."
    },
    {
      "name": "show_status",
      "params": [
        {
          "name": "height",
          "type": "int"
        },
        {
          "name": "speed",
          "type": "float"
        },
        {
          "name": "fuel",
          "type": "int"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u14",
          "kind": "call",
          "call": "color(LIGHT_GREEN)"
        },
        {
          "id": "u15",
          "kind": "call",
          "call": "at(0, 0)"
        },
        {
          "id": "u16",
          "kind": "output",
          "values": "\"Height: \" + str(height) + \"  Speed: \" + str(round(speed, 1)) + \"  Fuel: \" + str(fuel) + \"     \"",
          "newline": false
        }
      ],
      "comment": "At the top: height above the surface, vertical speed and fuel."
    },
    {
      "name": "on_ground",
      "params": [
        {
          "name": "x",
          "type": "int"
        },
        {
          "name": "y",
          "type": "int"
        }
      ],
      "returnType": "bool",
      "blocks": [
        {
          "id": "u17",
          "kind": "declare",
          "name": "c",
          "type": "int",
          "init": "point_color(x, y)"
        },
        {
          "id": "u18",
          "kind": "return",
          "value": "c == DARK_GREY or c == GREY or c == YELLOW"
        }
      ],
      "comment": "point_color reads the drawing's colour at a point. Grey or yellow is the surface; the black sky and white stars are not."
    },
    {
      "name": "touching",
      "params": [
        {
          "name": "x",
          "type": "int"
        },
        {
          "name": "y",
          "type": "int"
        }
      ],
      "returnType": "bool",
      "blocks": [
        {
          "id": "u19",
          "kind": "return",
          "value": "on_ground(x + 1, y + 11) or on_ground(x + 8, y + 11) or on_ground(x + 14, y + 11)"
        }
      ],
      "comment": "Has the module touched the surface? It tries points at the bottom of both legs and between them."
    },
    {
      "name": "landed",
      "params": [
        {
          "name": "x",
          "type": "int"
        },
        {
          "name": "y",
          "type": "int"
        },
        {
          "name": "speed",
          "type": "float"
        }
      ],
      "returnType": "bool",
      "blocks": [
        {
          "id": "u20",
          "kind": "declare",
          "name": "on_pad",
          "type": "bool",
          "init": "point_color(x + 1, y + 11) == YELLOW and point_color(x + 14, y + 11) == YELLOW"
        },
        {
          "id": "u21",
          "kind": "return",
          "value": "on_pad and speed < 1.2"
        }
      ],
      "comment": "The landing succeeds when both legs stand on the yellow pad and the module falls slower than 1.2 points per frame."
    },
    {
      "name": "explosion",
      "params": [
        {
          "name": "x",
          "type": "int"
        },
        {
          "name": "y",
          "type": "int"
        },
        {
          "name": "v1",
          "type": "list[str]"
        },
        {
          "name": "v2",
          "type": "list[str]"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u22",
          "kind": "call",
          "call": "sprite_at(0, x, y)"
        },
        {
          "id": "u23",
          "kind": "for",
          "variable": "i",
          "range": "6",
          "body": [
            {
              "id": "u24",
              "kind": "if",
              "condition": "i % 2 == 0",
              "then": [
                {
                  "id": "u25",
                  "kind": "call",
                  "call": "sprite_shape(0, v1)"
                },
                {
                  "id": "u26",
                  "kind": "call",
                  "call": "sprite_color(0, YELLOW)"
                }
              ],
              "else": [
                {
                  "id": "u27",
                  "kind": "call",
                  "call": "sprite_shape(0, v2)"
                },
                {
                  "id": "u28",
                  "kind": "call",
                  "call": "sprite_color(0, ORANGE)"
                }
              ]
            },
            {
              "id": "u29",
              "kind": "call",
              "call": "beep(300 - i * 40, 80)"
            }
          ],
          "comment": "The explosion animation: two shapes and colours alternate, the tone falls."
        },
        {
          "id": "u30",
          "kind": "call",
          "call": "sprite_hide(0)"
        },
        {
          "id": "u31",
          "kind": "call",
          "call": "color(LIGHT_RED)"
        },
        {
          "id": "u32",
          "kind": "call",
          "call": "at(12, 10)"
        },
        {
          "id": "u33",
          "kind": "output",
          "values": "\"CRASH!\"",
          "newline": false
        }
      ],
      "comment": "A crash: an explosion from two shapes, falling tones and a caption."
    },
    {
      "name": "celebrate",
      "params": [],
      "returnType": null,
      "blocks": [
        {
          "id": "u34",
          "kind": "call",
          "call": "color(LIGHT_GREEN)"
        },
        {
          "id": "u35",
          "kind": "call",
          "call": "at(11, 10)"
        },
        {
          "id": "u36",
          "kind": "output",
          "values": "\"LANDED!\"",
          "newline": false
        },
        {
          "id": "u37",
          "kind": "declare",
          "name": "notes",
          "type": "list[str]",
          "init": "[\"C4\", \"E4\", \"G4\", \"C5\"]"
        },
        {
          "id": "u38",
          "kind": "for",
          "variable": "i",
          "range": "len(notes)",
          "body": [
            {
              "id": "u39",
              "kind": "call",
              "call": "note(notes[i], 150)"
            }
          ]
        },
        {
          "id": "u40",
          "kind": "call",
          "call": "note(\"C5\", 400)"
        }
      ],
      "comment": "A successful landing: a caption and a fanfare."
    },
    {
      "name": "intro",
      "params": [],
      "returnType": null,
      "blocks": [
        {
          "id": "u41",
          "kind": "call",
          "call": "background(BLACK)"
        },
        {
          "id": "u42",
          "kind": "call",
          "call": "border(BLACK)"
        },
        {
          "id": "u43",
          "kind": "call",
          "call": "cls()"
        },
        {
          "id": "u44",
          "kind": "call",
          "call": "color(GREY)"
        },
        {
          "id": "u45",
          "kind": "call",
          "call": "at(0, 2)"
        },
        {
          "id": "u46",
          "kind": "output",
          "values": "\"Up or space: engine. Arrows: sideways.\"",
          "newline": false
        },
        {
          "id": "u47",
          "kind": "call",
          "call": "at(0, 3)"
        },
        {
          "id": "u48",
          "kind": "output",
          "values": "\"Land slowly on the yellow pad.\"",
          "newline": false
        }
      ],
      "comment": "The start screen with a hint."
    }
  ],
  "comment": "Solution to the task “A heavier Moon”: Raise gravity to 0.08 and try whether you can still land. What else would you have to change to make it possible?"
}
