# file: checks.py # # date: Oct. 25, 2007 # # author: CS074 Digital World; Fall 2007 # import turtle def setup(w, h): turtle.setup(width=w, height=h) turtle.degrees() turtle.up() # turtle pen is up! Otherwise too slow. # check is responsible for drawing an individual colored check at (x,y). # def check(x, y, height, width, color): turtle.color(color) turtle.goto(x, y) turtle.begin_fill() turtle.goto(x+width, y) turtle.goto(x+width, y+height) turtle.goto(x, y+height) turtle.goto(x, y) turtle.end_fill() # rowOfChecks will draw a row of cols checks across the turtle window at the # specified y coordinate. The checks will alternate in color between color1 # and color2. rowOfChecks also accepts a colorFlag which determines which of # the two colors are used for the first (i.e., leftmost) check in the row. # This function returns the value of colorFlag to help with managing the # starting color of the next row. # def rowOfChecks(y, cols, checkHeight, color1, color2, colorFlag): windowWidth = turtle.window_width() right = windowWidth / 2 left = -right checkWidth = windowWidth / cols x = left; while x < right: # We're now ready to draw a check. NB: that the color # is determined by the status of the boolean flag colorFlag. # if colorFlag: check(x, y, checkHeight, checkWidth, color1) else: check(x, y, checkHeight, checkWidth, color2) x = x + checkWidth # The next check to the right should have the other color. # colorFlag = not(colorFlag) # We're returning the value of colorFlag to deal with even .vs. odd # row lengths. # return colorFlag # checks will draw a rows x cols checkerboard covering the entire turtle window. # The colors for the checks are specified in color1 and color2. # def checks(rows, cols, color1, color2): windowHeight = turtle.window_height() top = windowHeight / 2 bottom = -top checkHeight = windowHeight / rows y = top - checkHeight # We'll use a colorFlag to manage the alternation between the two colors. # colorFlag = True while y >= bottom: colorFlag = rowOfChecks(y, cols, checkHeight, color1, color2, colorFlag) y = y - checkHeight # If there is an odd number of columns, then the next row should start # with the same color that ended the just completed row. # if cols % 2 == 0: colorFlag = not(colorFlag)