Turtle module can be used to draw some very nice patterns in Python. Following are some examples with code. ==================== import turtle import random t = turtle.Turtle( shape = "circle" ) t.lt( 90 ) lv = 14 l = 120 s = 30 t.color( 'indigo' ) t.width(lv) t.penup() t.bk(l) t.pendown() t.fd(l) def draw_tree ( l , level ): width = t.width() # save the current pen width t.width(width * 3.0 / 4.0 ) # narrow the pen width l = 3.0 / 4.0 * l #t.color(R,G,B) #provide the RGB numbers t.color(random.random(), random.random(), random.random()) t.lt(s) t.fd(l) if level < lv: draw_tree(l, level + 1 ) t.color(random.random(), random.random(), random.random()) t.bk(l) t.rt( 2 * s) t.fd(l) if level < lv: draw_tree(l, level + 1 ) t.color(random.random(), random.random(), random.random()) t.bk(l) t.lt(s) t.width(width) # restore the previous pen width t.speed( "fastest" ) draw_tree(l, 5 ) turtle.done() ===========
Comments
Post a Comment