"""Example of random walk using turtle graphics."""

import random
import turtle

MARGIN = 20


def main():
    """Make the turtle walk randomly around the screen."""

    # Set up the screen.
    screen = turtle.Screen()
    screen.bgcolor("white")
    screen.setup(width=500, height=500)

    # Create a turtle and set its properties.
    walker = turtle.Turtle()
    walker.shape("turtle")
    walker.color("purple")
    walker.pensize(2)

    # Keep walking until the turtle reaches the edge.
    while True:

        # Move the turtle in a random direction and distance.
        angle = random.randint(0, 359)
        distance = random.randint(10, 30)
        walker.setheading(angle)
        walker.forward(distance)

        # Check whether the turtle is near the edge.
        if (abs(walker.xcor()) > screen.window_width() / 2 - MARGIN or
            abs(walker.ycor()) > screen.window_height() / 2 - MARGIN):
            break

    # Wait for the user to click before closing the window.
    screen.exitonclick()


if __name__ == "__main__":
    main()
