./code/turtles/readme.txt
Here is a small collection of sample Python turtle programs for example and inspiration.
Note: some of these have a rather large screen size, so you may need to scroll around to see what is going on.
You need a Python interpreter set up on your computer to run these.
It can be downloaded for free from www.python.org
These programs can either be copy-pasted into the interactive interpreter,
or saved into a plain-text file with the extension .py, for example:
hex_spiral.py (note the underscore to separate words!)
you can then edit the code in your favorite text editor, and run it from a terminal (or power shell on windows) with the line:
python3 hex_spiral.py
You can of course also run them from an IDE if you have one set up.
./code/turtles/hex_spiral.py
import turtle, math
s = turtle.Screen()
s.screensize(2000,2000)
s.colormode(255)
s.bgcolor(30,60,90)
t = turtle.Turtle()
t.fillcolor((255,0,255)) # on a range of 0-255
t.speed(0) # 1 - 10, but then 0 is faster than 10
t.hideturtle()
t.penup()
n = 400
for i in range(1, n):
t.forward(i*5)
t.right(171)
t.begin_fill()
t.fillcolor(round(125*math.sin(20*i/n)) + 125,
round(125*math.sin(30*i/n)) + 125,
round(125*math.sin(i/n)) + 125)
for j in range(6): # hexagon
t.forward(20)
t.left(60)
t.end_fill()
s.exitonclick()
./code/turtles/color_wheel.py
import turtle, math
s = turtle.Screen()
s.screensize(640,640)
s.colormode(255)
s.bgcolor((16,32,48))
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.penup()
n = 180 # number of colors
def color(i, sat):
b = sat*math.sin(2*math.pi/n*(i)) + 125
r = sat*math.sin(2*math.pi/n*(i+n/3)) + 125
g = sat*math.sin(2*math.pi/n*(i+2*n/3)) + 125
return (round(r),round(g),round(b))
def spiral(i,r):
x = math.cos(2*math.pi/n*i)*r
y = math.sin(2*math.pi/n*i)*r
return (round(x),round(y))
for i in range(1,7):
for j in range(n):
x,y = spiral(j,280-40*i)
t.goto(x,y)
if j%i == 0:
t.pencolor(color(j, 125-20*i))
t.dot(100-8*i)
s.exitonclick()
./code/turtles/fractal.py
import turtle
s = turtle.Screen()
s.screensize(1152, 1024)
s.colormode(255)
s.bgcolor((16,32,48))
t = turtle.Turtle()
t.pensize(1)
t.speed(0)
t.pencolor((230, 100, 250))
t.hideturtle()
t.penup()
t.goto(0, 500)
t.left(60)
t.pendown()
def fractal(length):
if length <= 4:
return
for i in range(3):
fractal(length//2)
t.right(120)
t.forward(length)
fractal(1024)
s.exitonclick()