Turtle Fun Fuzz Ball (Python)

Updated vegaseat 3 Tallied Votes 288 Views Share

A little fun drawing with the Python module turtle. Well commented to help the beginner.

TrustyTony commented: I do not like the unnecessary and confusing () around single values. -3
''' turtle_fuzz_ball1.py
draw a fuzz ball with Python module turtle
see also:
http://docs.python.org/2/library/turtle.html
tested with Python27 and Python33  by  vegaseat  23jan2013
'''

import turtle as tu
import random as rd

def fuzz_ball(angle):
    # pen to center of screen
    tu.up()
    tu.goto(0, 0)
    tu.down()
    # pick an (r,g,b) random color
    # values 0.0 to > 1.0
    r = rd.random()
    g = rd.random()
    b = rd.random()
    # set the color
    tu.color(r,g,b)
    # point in this direction
    tu.setheading(angle)
    num_segments = 3
    count = 0
    for k in range(num_segments):
        # pick random radius and extent of the circle's arc
        radius = rd.randrange(80, 30, -10)
        extent = rd.randrange(120, 30, -10)
        # alternate via count
        if count == 1:
            radius *= -1
            count = 0
            tu.circle(radius, extent)
        else:
            count += 1
            tu.circle(radius, extent)


# delete any turtle drawings from the screen
# re-center the turtle and set variables to the default values
tu.reset()

# for instant drawing use 
#tu.tracer(False)
# for animated drawing use
tu.speed('fastest')

# width of pen
tu.width(3)

# loop through 360 degrees
for angle in range(0, 360):
    fuzz_ball(angle)

# keep showing until window corner x is clicked
tu.done()