In this project, we are generate several Julia Sets by varying the constant c in the normal Julia Set function. Turtle will be too slow to do this work. Instead we are going to use the PIL library. Please check out Julia Set Animation project for source code.
Tag Archives: colorsys
Rainbow Colored Fully Connected 48-gon
We have project that draws fully connected 24-gon. Use colorsys library to gradually change the hue of lines as they get longer.
Colored Pentaflake Fractal (Source Code)
Based on petaflake fractal, color it with colorsys library.
Source Code:
import turtle
import math
import colorsys
screen = turtle.Screen()
screen.title('Pentaflake Fractal Colored - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)
turtle.fillcolor('dark cyan')
def pentagon(x,y,r,direction): #x,y is the center
turtle.up()
turtle.goto(x,y)
turtle.seth(direction)
turtle.fd(r)
turtle.left(126)
turtle.down()
c = colorsys.hsv_to_rgb((x+1000)/2000,1,0.7)
turtle.color(c)
turtle.begin_fill()
for _ in range(5):
turtle.fd(2*r*math.sin(math.radians(36)))
turtle.left(72)
turtle.end_fill()
def pentaflake(x,y,r,direction,n):
if n==0:
pentagon(x,y,r,direction)
return
r2 = r/(1+2*math.cos(math.radians(36)))
d = 2*r2*math.cos(math.radians(36))
for _ in range(5):
x2,y2 = x+d*math.cos(math.radians(direction)),y+d*math.sin(math.radians(direction))
pentaflake(x2,y2,r2,direction,n-1)
direction += 72
pentaflake(x,y,r2,direction+180,n-1)
pentaflake(0,0,1000,90,6)
screen.update()
Colored Spiral of Spirals with Python Turtle (Source Code)
Just for fun, draw a colored version of spiral of spirals using the colorsys library.
Source Code: (This may a few minutes)
import turtle
import colorsys
screen = turtle.Screen()
screen.title('Colored Spiral of Spirals Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)
def draw_spiral(x,y,length,direction):
L = length
c = 0
color=colorsys.hsv_to_rgb((y+900)/1800,1,0.8)
turtle.color(color)
while length>1 or c<20:
if length>2:
draw_spiral(x,y,length*0.255,160+direction)
turtle.up()
turtle.seth(direction)
turtle.goto(x,y)
if length <= 2:
turtle.down()
turtle.fd(length)
x,y = turtle.xcor(), turtle.ycor()
length *= 0.93
direction += 20
c += 1
LL=300
draw_spiral(500,-500,LL,90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")
Colored Square Spiral in a Square (Source Code)
In a previous project, we draw a 5 degree square spiral in a square. Use the colosys library to fill the square with gradually changing hues. Also, reduce the tilting degree from 5 degrees to 0.1 degree to see smooth change in color. If you use recursion make sure you increase the maximum recursion depth with sys.setrecursionlimit() function.
Source Code:
import turtle
import math
import colorsys
import sys
screen = turtle.Screen()
screen.title('5 Degree Square Spiral in a Square - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()
sys.setrecursionlimit(10000)
def draw_square(x,y,direction,length):
turtle.up()
turtle.goto(x,y)
turtle.seth(direction)
turtle.back(length/2)
turtle.left(90)
turtle.back(length/2)
turtle.seth(direction)
turtle.down()
c = colorsys.hsv_to_rgb(0.9*length/L,1,1)
turtle.fillcolor(c)
c = colorsys.hsv_to_rgb(0.9*length/L,1,1)
turtle.pencolor(c)
turtle.begin_fill()
for _ in range(4):
turtle.fd(length)
turtle.left(90)
turtle.end_fill()
def square_spiral(x,y,direction,length):
if length < 5: return
draw_square(x,y,direction,length)
square_spiral(x,y,direction+alpha,length/(math.sin(math.radians(alpha)) + math.cos(math.radians(alpha))))
alpha=0.1
L=1600
square_spiral(0,0,0,L)
screen.update()
Colored Nested Circle of Squares with Python and Turtle
Improve the nested circle of squares by filling the squares with different colors. You may use Python’s colorsys library.
Chase the Cycler Simulation (with Source Code)
In a previous project called massive chasing game, you simulated a game where you created 100 turtles and each turtle chase the next one and the last turtle chase the first one.
In this project, instead of making the last turtle chase the first one, make it move on a circle forever. It will create an interesting animation shown above.
Source Code:
import turtle
import random
import colorsys
screen = turtle.Screen()
screen.tracer(0,0)
screen.setup(1000,1000)
screen.title('Chase the Cycler - PythonTurtle.Academy')
turtle.hideturtle()
n=200
chasers = []
for i in range(n):
chasers.append(turtle.Turtle())
h = 0
for i in range(n):
c = colorsys.hsv_to_rgb(h,1,0.8)
h += 1/n
chasers[i].color(c)
chasers[i].up()
chasers[i].goto(random.uniform(-500,500), random.uniform(-500,500))
chasers[n-1].goto(0,-300)
chasers[n-1].shape('circle')
chasers[n-1].shapesize(1)
def chase():
for i in range(n-1):
angle = chasers[i].towards(chasers[i+1])
chasers[i].seth(angle)
chasers[n-1].left(2)
chasers[n-1].fd(10)
for i in range(n-1):
chasers[i].fd(10)
screen.update()
screen.ontimer(chase,1000//20)
chase()
Random Filled Circles
Generate a list of random numbers as the radii of circles. Sort these numbers in reverse order and draw these circles with colors from hue value 0 to 1.
Game of Snake with Python Turtle
Develop a Game of Snake with Python Turtle with multiple difficulty levels.
You may need to use features or libraries: List, Random, Keyboard Event, Timer Event, Colorsys.
Colored Apollonian Gasket (Full) with Python Turtle
Color the Apollonian Gasket.