Turtle graphics drawing over itself

2019-07-27 01:44发布

问题:

This should be a very simple question, however, it is proving difficult for me. I'm rather new to turtle graphics, and so, I am trying to get a simple drawing done. My turtle will draw a row, pick the pen up, move up one pixel, place the pen down, and keep drawing. Here's my code so far:

for y in range(height):
  turtle.pendown()
  for x in range(width):
    detLand(y, x) # Set the color, works just fine
    turtle.setx(x)
    turtle.sety(y)
  turtle.penup()

I figured this would be easy, however, it's still drawing over top of my lines.

回答1:

I believe the problem is that you're accidentally drawing on the backstroke. Try this instead:

for y in range(height):
    turtle.sety(y)

    turtle.pendown()

    for x in range(width):
        detLand(y, x)
        turtle.setx(x)

    turtle.penup()

    turtle.setx(0)

I believe your problem is this schism:

turtle.setx(x)
turtle.sety(y)

Think about what happens at end of line, you just set Y and then you come around with X = 0 and over draw the line you just finished before Y gets positioned correctly.