Question

I am using turtle graphics in Python . "Write a program that displays two chessboards. Your program should define at the following function:

#Draw one chessboard who upper-left corner is at (startx, starty) and bottom right corner is at (endx, endy)
def drawChessboard(startx, endx, starty, endy):

The chessboards are 8x8. I began making this program by creating 2 loops, one main loop and another nested in it. The first loop drew squares horizontally, and the second loop drew squares vertically. The code was far from being complete but I was on the right track, then I realized that there are end coordinates and become dumbfounded; I have no idea how I could design this program using end coordinates. Making it with beginning coordinates is easy enough, but by requiring there be end coordinates too just adds a limitation to my own creative abilities.

My idea so far has been to have a function which when provided with the x,y end and start coordinates, it would find the difference between the two sets, then from there divide the difference by eight and that would be the size of each square within the 8x8 grid. The problem with that is it is dependent on the fact that the start and end coordinates are aligned to create a perfect square.

I'm not sure what route to go on this. Any advice on a simpler way to go about this?

Était-ce utile?

La solution

No, you seem to be on the right track. Just say that the width of each square is equal to (endx - startx) / 8 and that the height of each square is (endy - starty) / 8. If the person who uses your function wants the chessboard to look nice, they'll make sure that the board is perfectly square, but that shouldn't be your problem.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top