0

For some reason my code works until it gets to line 67 (bold text). The cursor should move down 100px on the y axis but it stays there and draws the boxes over the second darkblue one. Does anybody know why that is. Help would be gladly appreciated. I'm still new to Python.

def drawbox():
    for x in range (4):
        avo.color('white', 'blue')
        avo.begin_fill()

        for x in range(2):
            avo.forward(400)
            avo.right(90)
            avo.forward(100)
            avo.right(90)
        avo.end_fill()

        avo.penup()
        avo.setpos(0,-100)
        avo.pendown()

        avo.color('white', 'darkblue')
        avo.begin_fill()
        for x in range(2):
            avo.forward(400)
            avo.right(90)
            avo.forward(100)
            avo.right(90)
        avo.end_fill()

        avo.penup()
        **avo.setpos(0,-100)**
        avo.pendown()
        continue


    turtle.done()


drawbox()
orellana
  • 1
  • 1

1 Answers1

0

The problem is that setpos() works in absolute positions whereas you want to move to a position relative to where you are now. By doing:

avo.setpos(0,-100)

You always return to the same spot on the screen. Try changing both of your setpos() calls to instead do:

avo.sety(avo.ycor() - 100)

Or, better yet, redesign your program slightly to point the turtle in the right direction and simply move forward 100 pixels after each box:

from turtle import Screen, Turtle

def drawboxes():
    turtle.setheading(270)

    for _ in range(2):
        for fill_color in ('blue', 'darkblue'):
            turtle.fillcolor(fill_color)
            turtle.begin_fill()

            for _ in range(2):
                turtle.forward(100)
                turtle.left(90)
                turtle.forward(400)
                turtle.left(90)

            turtle.end_fill()

            turtle.penup()
            turtle.forward(100)
            turtle.pendown()

screen = Screen()

turtle = Turtle()
turtle.pencolor('white')

drawboxes()

screen.exitonclick()
cdlane
  • 40,441
  • 5
  • 32
  • 81