Draw Two Checkerboards

project
python
turtle

#1

Hi, I am writing a program that draws to checkerboards. So you input the starting and ending points of the checkerboards and it draws it. My, problem is that after the first checkerboard is drawn, the cursor wanders off out of the screen instead of going to the starting point of the second checkerboard and drawing the second checkerboard. What have I done wrong? Thanks

import turtle


def draw_chess_board(startx, endx, starty, endy):
    draw_first_half_diagonal(startx, endx, starty, endy)
    draw_second_half_diagonal(startx, endx, starty, endy)
    draw_border(startx, endx, starty, endy)


def draw_first_half_diagonal(startx, endx, starty, endy):
    y_shift = (endy - starty) / 8
    for i in range(1, 7 + 1, 2):
        turtle.penup()
        turtle.goto(startx, starty + i * y_shift)
        turtle.pendown()
        turtle.begin_fill()
        turtle.fillcolor("black")
        for j in range(1, i + 1):
            turtle.setheading(0)
            turtle.forward((endx - startx) / 8)
            turtle.setheading(270)
            turtle.forward((endy - starty) / 8)
        for k in range(1, i + 1):
            turtle.setheading(180)
            turtle.forward((endx - startx) / 8)
            turtle.setheading(90)
            turtle.forward((endy - starty) / 8)
        turtle.end_fill()


def draw_second_half_diagonal(startx, endx, starty, endy):
    x_shift = (endx - startx) / 8
    for i in range(1, 7 + 1, 2):
        turtle.penup()
        turtle.goto(endx - i * x_shift, endy)
        turtle.pendown()
        turtle.begin_fill()
        turtle.fillcolor("black")
        for j in range(1, i + 1):
            turtle.setheading(0)
            turtle.forward((endx - startx) / 8)
            turtle.setheading(270)
            turtle.forward((endy - starty) / 8)
        for k in range(1, i + 1):
            turtle.setheading(180)
            turtle.forward((endx - startx) / 8)
            turtle.setheading(90)
            turtle.forward((endy - starty) / 8)
        turtle.end_fill()


def draw_border(startx, endx, starty, endy):
    turtle.penup()
    turtle.goto(startx, starty)
    turtle.pendown()
    turtle.goto(startx, endy)
    turtle.goto(endx, endy)
    turtle.goto(endx, starty)
    turtle.goto(startx, starty)


def main():
    # Get start and end coordinates for chessboard 1 and 2
    c1_startx, c1_endx, c1_starty, c1_endy = eval(input("Enter the coordinates of chessboard one's starting and ending points: "))
    c2_startx, c2_endx, c2_starty, c2_endy = eval(input("Enter the coordinates of chessboard two's starting and ending points: "))
    turtle.screensize(1000, 1000)
    turtle.showturtle()
    draw_chess_board(c1_startx, c1_endx, c1_starty, c1_endy)
    draw_chess_board(c2_startx, c2_endx, c2_starty, c2_endy)
    turtle.hideturtle()
    turtle.done()


main()


#2

Sounds like the input you gave it is important information. Easiest of all is if you put all the information in the code so that it other people won’t have to tell it anything at all and can just run it

(also, eval is almost never something that you should use, at the very least you should replace it with ast.literal_eval which only accepts values, not any code like eval)


#3

I got it. Instead of inputting (start x, end x, start y, end y) I put (start x, start y, end x, end y). So the program was actually working, I just gave it wrong input. Thanks.

Also, how do people debug programs that are lengthy. I understand that you use print statements, trace variable values, use the debugger, look at the different functions and modules. But when it comes to nest loops (say that was the only efficient method). How would you debug that?


#4

Only print the things you want to see, use smaller data, limit size of functions, don’t nest too deeply (break out into functions), read the code and reason about why it shouldn’t do what it does

def draw_board(x, y, square_size, board_size):
    for offset in range(board_size):
        draw_column(x + offset * square_size, y, square_size, board_size)

def draw_column(...):
    ... draw_square(...)

def draw_square(...):
    ... draw_line(...)

def draw_line(...):
    goto point a
    pen down
    goto point b
    pen up

There, some really obvious functions where each one is easy to debug individually.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.