Jacob Gilger Jacob Gilger - 5 days ago 5
Python Question

How can a turtle be moved up then down in a for loop?

So I'm learning python in a virtual programming class and I have an assignment where I have to create a picture using Turtle Graphics. I'm using things like for loops and programmer-defined functions in the program but I'm having a problem with something I want to do in my picture. I'm drawing clouds in the sky and I'm trying to get the clouds to be drawn at different positions. I'm increasing the x coordinate by increments of 40 but I'm trying to get the cloud to be placed higher and lower as it goes across. It's at the end of this list of code:

import turtle

def backFill(b, c, x, y, l, h):
b.penup()
b.setpos(x, y)
b.color(c)
b.pendown()
b.begin_fill()
for side in range(2):
b.forward(l)
b.left(90)
b.forward(h)
b.left(90)
b.end_fill()

def drawCloud(c, x, y):
c.penup()
c.setpos(x, y)
c.pendown()
c.color("grey")
c.begin_fill()
for side in range(5):
c.circle(10)
c.left(80)
c.end_fill()

def main():
print("Cars")
joe = turtle.Turtle()
joe.speed(0)

backFill(joe,"green",-200,-100,400,25)
backFill(joe,"black",-200,-75,400,75)
backFill(joe,"green",-200,0,400,25)
backFill(joe,"sky blue",-200,25,400,110)

x=-192.5
for side in range(10):
backFill(joe,"yellow",x,-40,25,5)
x=x+40

x=-180
y=100
for side in range(15):
drawCloud(joe,x,y)
x=x+40
y=y-10

main()


Currently the clouds slowly descend as each one is drawn but what I'm trying to do is make clouds at different heights like one cloud is at 100 the next 90 then back to 100, etc. I tried things like
y=y-10, y+10
to see if the first time it repeated it would go down 10 then the next time it would go up 10.

https://gyazo.com/3ad5268231b3217b81636cc070573b75

tl;dr/simpler explanation: I'm trying to move a looped picture up and down so it's not simply in a straight line. How would I move it down one time then up another time within a for loop?

Answer

You could check whether side is even or odd to decide whether to increment or decrement your cloud location. Like so:

for side in range(15):
    drawCloud(joe,x,y)
    x=x+40
    if(side%2==0):
        y=y-10
    else:
        y=y+10