Moon landing
The module falls towards the Moon, you brake with the engine and save fuel, and you must land slowly on the yellow pad. The ground is drawn with lines and the touch is told by the colour of a point. Teaches physics in variables, decimals, conditions and a sprite over a drawing.
Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""The classic Lunar Lander: brake with the engine and land slowly on the pad. The ground is drawn with lines, point_color detects the touch. Teaches physics in variables and a sprite over a drawing."""
from random import randint
# Tokbit screen functions, not in plain Python:
from tokbit import (
cls, at, color, background, border, key, sleep, beep, note, sprite_at,
sprite_color, sprite_hide, sprite_shape, plot, line, rect, point_color,
BLACK, WHITE, YELLOW, ORANGE, LIGHT_RED, DARK_GREY, GREY, LIGHT_GREEN,
)
def draw_ground(ground: list[int], pad: int):
"""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."""
color(DARK_GREY)
# Fill: a vertical line under every point of the surface down to the bottom edge.
for x in range(320):
line(x, surface(ground, x), x, 199)
color(GREY)
# Outline: slopes and craters as lines between ground points 20 points apart.
for i in range(16):
line(i * 20, ground[i], i * 20 + 20, ground[i + 1])
color(YELLOW)
rect(pad * 20, ground[pad], 40, 4, True)
def surface(ground: list[int], x: int) -> int:
"""The height of the surface at point x: it lies on the line between two neighbouring ground points."""
i: int = x // 20
return ground[i] + (ground[i + 1] - ground[i]) * (x % 20) // 20
def stars():
"""Stars in the sky: white points in random places."""
color(WHITE)
for i in range(40):
plot(randint(0, 319), randint(10, 110))
def show_status(height: int, speed: float, fuel: int):
"""At the top: height above the surface, vertical speed and fuel."""
color(LIGHT_GREEN)
at(0, 0)
print("Height: " + str(height) + " Speed: " + str(round(speed, 1)) + " Fuel: " + str(fuel) + " ", end="")
def on_ground(x: int, y: int) -> bool:
"""point_color reads the drawing's colour at a point. Grey or yellow is the surface; the black sky and white stars are not."""
c: int = point_color(x, y)
return c == DARK_GREY or c == GREY or c == YELLOW
def touching(x: int, y: int) -> bool:
"""Has the module touched the surface? It tries points at the bottom of both legs and between them."""
return on_ground(x + 1, y + 11) or on_ground(x + 8, y + 11) or on_ground(x + 14, y + 11)
def landed(x: int, y: int, speed: float) -> bool:
"""The landing succeeds when both legs stand on the yellow pad and the module falls slower than 1.2 points per frame."""
on_pad: bool = point_color(x + 1, y + 11) == YELLOW and point_color(x + 14, y + 11) == YELLOW
return on_pad and speed < 1.2
def explosion(x: int, y: int, v1: list[str], v2: list[str]):
"""A crash: an explosion from two shapes, falling tones and a caption."""
sprite_at(0, x, y)
# The explosion animation: two shapes and colours alternate, the tone falls.
for i in range(6):
if i % 2 == 0:
sprite_shape(0, v1)
sprite_color(0, YELLOW)
else:
sprite_shape(0, v2)
sprite_color(0, ORANGE)
beep(300 - i * 40, 80)
sprite_hide(0)
color(LIGHT_RED)
at(12, 10)
print("CRASH!", end="")
def celebrate():
"""A successful landing: a caption and a fanfare."""
color(LIGHT_GREEN)
at(11, 10)
print("LANDED!", end="")
notes: list[str] = ["C4", "E4", "G4", "C5"]
for i in range(len(notes)):
note(notes[i], 150)
note("C5", 400)
def intro():
"""The start screen with a hint."""
background(BLACK)
border(BLACK)
cls()
color(GREY)
at(0, 2)
print("Up or space: engine. Arrows: sideways.", end="")
at(0, 3)
print("Land slowly on the yellow pad.", end="")
intro()
# The module's shape without a flame.
lander: list[str] = ["................", "......####......", ".....######.....", "....##.##.##....", "....########....", ".....######.....", "....########....", "...##########...", "...#.######.#...", "..##........##..", "..#..........#..", ".##..........##.", "................", "................", "................", "................"]
# The same module with the engine flame: switching makes the animation.
flame: list[str] = ["................", "......####......", ".....######.....", "....##.##.##....", "....########....", ".....######.....", "....########....", "...##########...", "...#.######.#...", "..##........##..", "..#..........#..", ".##..........##.", "......####......", "......####......", ".......##.......", "................"]
blast1: list[str] = ["................", "................", "......#..#......", "...#..#..#..#...", "....#.####.#....", ".....######.....", "..##########....", ".....######.##..", "....#.####.#....", "...#..#..#..#...", "......#..#......", "................", "................", "................", "................", "................"]
blast2: list[str] = ["#.......#......#", "...#.........#..", ".#....#..#....#.", "....#......#....", "#.............#.", "...#...##...#...", ".#............#.", "......#..#......", "#..#........#..#", ".......#........", "..#..#....#..#..", "#..............#", "....#..#..#.....", ".#............#.", "...#...#...#....", "#..............#"]
# The landing pad: two stretches of ground from point pad, that is 40 points.
pad: int = randint(0, 14)
pad_height: int = randint(150, 175)
# The height of the surface every 20 points from left to right: 17 points across the width of 320.
ground: list[int] = []
for i in range(17):
if pad <= i <= pad + 2:
ground.append(pad_height)
else:
ground.append(randint(120, 190))
stars()
draw_ground(ground, pad)
sprite_shape(0, lander)
sprite_color(0, WHITE)
x: float = 152.0
y: float = 16.0
# Vertical speed in points per frame; positive is down.
speed: float = 0.0
fuel: int = 100
# For how many more frames the engine runs after a press.
thrust: int = 0
flying: bool = True
# The game loop: key, engine, gravity, movement and status until the module touches the surface.
while flying:
k: str = key()
if (k == "up" or k == " ") and fuel > 0:
thrust = 4
if k == "left":
x = max(0.0, x - 4)
if k == "right":
x = min(304.0, x + 4)
# The engine: thrust against gravity, uses fuel, shows the flame and hums.
if thrust > 0 and fuel > 0:
speed = speed - 0.15
fuel = fuel - 1
thrust = thrust - 1
sprite_shape(0, flame)
beep(70, 15)
else:
thrust = 0
sprite_shape(0, lander)
# Gravity: a bit faster downwards every frame.
speed = speed + 0.05
y = max(16.0, y + speed)
sprite_at(0, round(x), round(y))
show_status(max(0, surface(ground, round(x) + 8) - round(y) - 11), speed, fuel)
if touching(round(x), round(y)):
flying = False
sleep(40)
if landed(round(x), round(y), speed):
sprite_shape(0, lander)
celebrate()
else:
explosion(round(x), round(y), blast1, blast2)
Follow-up tasks
Tasks in which you change the example (with a model solution):