Friday, 13 September 2013

Change color based on recursion depth Python Turtle

Change color based on recursion depth Python Turtle

I have a coding problem I'm having trouble with. I am learning recursion
and so far having a pretty good time with it. We've starting with basic
turtle drawings using the python turtle graphics module. I've got the
picture code down, but I'm also supposed to change the color of the
turtle's pen based on depth. My professor only briefly touched on mod (%)
to achieve this, but I have no idea where to begin and was hoping for some
assistance. Thanks in advance. I can't add pics because my rep isn't high
enough, but basically if you run the code it draws "S" figures. The first
"S" should be green, second two red, third three green, etc. Thanks again.
Here's the code:
from turtle import *
def drawzig2(depth,size):
if depth == 0:
pass
elif depth:
left(90)
fd(size/2)
right(90)
fd(size)
left(45)
drawzig2(depth-1,size/2)
right(45)
fd(-size)
left(90)
fd(-size)
right(90)
fd(-size)
left(45)
drawzig2(depth-1,size/2)
right(45)
fd(size)
left(90)
fd(size/2)
right(90)
drawzig2(4,100)

No comments:

Post a Comment