# file: slide.py # # author: Bob Muller # date: October 28, 2007 # # This file contains python code using the turtle graphics system. # The code in this file, slides a box across the x-axis. There are two # versions, one using a while-loop and one using recursion. # import turtle def setup(h,w): turtle.setup(height = h, width = w) turtle.degrees() turtle.up() # box is a helper function that simply draws a square box of a specified # color. NB: the pen is up(!) because things are too slow when the pen is # down. # def box(x,y,side,color): turtle.color(color) turtle.goto(x,y) turtle.begin_fill() turtle.goto(x+side,y) turtle.goto(x+side,y+side) turtle.goto(x,y+side) turtle.goto(x,y) turtle.end_fill() # slide1 slides a square box across the x-axis (y == 0) of the turtle # graphics window. # def slide1(side, color, delta): x = - (turtle.window_width() / 2.0) while x < (turtle.window_width() / 2.0) - side: box(x, 0, side, color) box(x, 0, side, 'White') x = x + delta # slide2 slides a square box across the x-axis (y == 0) of the turtle # graphics display. slide2 uses recursion to effect repetition. # def slide2(side,color,delta): slide2Help(-(turtle.window_width() / 2.0), side, color, delta) def slide2Help(x,side,color,delta): box(x, 0, side, color) if x < turtle.window_width() / 2.0: box(x, 0, side, 'White') slide2Help(x + delta, side, color, delta)