예제 #1
0
def draw_two_lines(w, sp):
    """
    Draws two lines on to window w.

    This function clears w of any previous drawings. Then, in the middle of
    the window w, this function draws a green line 100 pixels to the east,
    and then a blue line 200 pixels to the north. It uses a new turtle that
    moves at speed sp, 0 <= sp <= 10, with 1 being slowest and 10 fastest
    (and 0 being "instant").

    REMEMBER: You need to flush the turtle if the speed is 0.

    This procedure asserts all preconditions.

    Parameter w: The window to draw upon.
    Precondition: w is a introcs Window object.

    Parameter sp: The turtle speed.
    Precondition: sp is a valid turtle speed.
    """
    # Assert the preconditions
    assert is_window(w), report_error('w is not a valid window', w)
    assert is_valid_speed(sp), report_error('sp is not a valid speed', sp)

    # Clear the window first!
    w.clear()

    # Create a turtle and draw
    t = Turtle(w)
    t.speed = sp
    t.color = 'green'
    t.forward(100)  # draw a line 100 pixels in the current direction
    t.left(90)  # add 90 degrees to the angle
    t.color = 'blue'
    t.forward(200)

    # This is necessary if speed is 0!
    t.flush()
예제 #2
0
def island(w, side, d, sp):
    """
    Draws a Minkowski island with the given side length and depth d.

    This function clears the window and makes a new Turtle t.  This turtle starts in
    lower right corner of the square centered at (0,0) with side length side. It is
    facing straight up. It draws by calling the function island_edge(t, side, d) four
    times, rotating the turtle left after each call to form a square.

    The turtle should be visible while drawing, but hidden at the end. The turtle color
    is 'sea green'.

    REMEMBER: You need to flush the turtle if the speed is 0.

    Parameter w: The window to draw upon.
    Precondition: w is a Window object.

    Parameter side: The side-length of the island
    Precondition: side is a valid side length (number >= 0)

    Parameter d: The recursive depth of the island
    Precondition: d is a valid depth (int >= 0)

    Parameter sp: The drawing speed.
    Precondition: sp is a valid turtle/pen speed.
    """
    # ARE THESE ALL OF THE PRECONDITIONS?
    assert is_window(w), report_error('w is not a valid window', w)
    assert is_valid_length(side), \
    report_error('side is not a valid length', side)
    assert is_valid_depth(d), report_error('d is not a valid depth', d)
    assert is_valid_speed(sp), \
    report_error('sp is not a valid turtle/pen speed',sp)

    w.clear()
    t = Turtle(w)

    t.visible = True

    t.speed = sp
    t.color = 'sea green'

    t.move(side / 2, -side / 2)
    t.heading = 90
    for i in range(4):
        island_edge(t, side, d)
        t.left(90)

    t.visible = False
    t.flush()