Files
018-turtle/src/turtle_challenges/random_walk.py
T
Tirsvad 949d2edb63 Add random_walk with tests
200 steps of equal length, each in a random heading of 0, 90, 180 or 270 degrees and
a random RGB color, with a thick line and the fastest speed.

Task: MIL-004#1
2026-10-07 23:59:10 +08:00

50 lines
1.4 KiB
Python

"""!
@file random_walk.py
@brief Turtle Challenge 4: generate a random walk.
"""
import random
from turtle_challenges.colors import random_color
from turtle_challenges.constants import (
FASTEST_SPEED,
WALK_HEADINGS,
WALK_PEN_SIZE,
WALK_STEP_DISTANCE,
WALK_STEPS,
)
from turtle_challenges.pen import Pen
def random_walk(
pen: Pen,
steps: int = WALK_STEPS,
distance: float = WALK_STEP_DISTANCE,
rng: random.Random | None = None,
) -> None:
"""!
@brief Walk in random directions, changing color at every step.
Each step faces east, north, west or south at random and moves the same
distance in a new random RGB color.
@param pen The turtle that draws.
@param steps Number of steps to take.
@param distance Distance of one step, in turtle units.
@param rng Random generator for headings and colors; a shared one is used
if omitted.
@exception ValueError If @p steps is negative, or @p distance is not
positive.
"""
if steps < 0:
raise ValueError(f"steps must not be negative, got {steps}")
if distance <= 0:
raise ValueError(f"distance must be positive, got {distance}")
generator = rng if rng is not None else random.Random()
pen.pensize(WALK_PEN_SIZE)
pen.speed(FASTEST_SPEED)
for _ in range(steps):
pen.color(random_color(generator))
pen.setheading(generator.choice(WALK_HEADINGS))
pen.forward(distance)