#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Some turtle graphics....


    hey guys, so i wrote the following code to draw a checkers board starting from the bottom left corner, with a white square:

    Code:
    import turtle
    
    def drawBoard(b):
    
        w = 40
        h = w
        turtle.up()
        turtle.goto((-1*(len(b)*w)), (-1*(len(b) *w)))
        turtle.setheading(90)
        turtle.down()
        for k in range(4):
            turtle.forward(len(b)*w)
            turtle.right(90)
    
        for i in range(1, len(b)*2, 2):
            turtle.fillcolor(["black"])
            turtle.begin_fill()
            for j in range(4):
                turtle.forward(w)
                turtle.right(90)
            turtle.forward(w)
            turtle.end_fill()
    im not entirely sure what went wrong, it stopped drawing up till the part where it should start coloring, any pointers would be great, thanks!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    The fill color shouldn't have been a list.
    Code:
    import turtle
    
    def rectangle_at(x, y, edge_1, edge_2 = None, fill = None):
        '''moves the turtle to x, y with heading unchanged'''
        d1 = edge_1
        if edge_2 is None:
            d2 = d1
        turtle.up()
        turtle.goto(x, y)
        if fill is not None:
            stack = [turtle.fillcolor()]
            turtle.fillcolor(fill)
            turtle.begin_fill()
        turtle.down()
        for k in 'ab':
            turtle.forward(d1)
            turtle.right(90)
            turtle.forward(d2)
            turtle.right(90)
        if fill is not None:
            turtle.end_fill()
            turtle.fillcolor(stack.pop())
    
    def odd(a):
        return a & 1
    
    def drawBoard(b):
        w = 40
        h = w
        L = len(b)*w
        turtle.setheading(90)
        rectangle_at(-L, -L, L)
        for i in range(len(b)):
            x = i*w - L
            for j in range(odd(i), len(b), 2):
                rectangle_at(x, j*w-L, w, fill='black')
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo