Day 11-14: Turtle

Day 11-14: Turtle

June 1-4th: Working with the Turtle module to make some pretty cool stuff.

·

5 min read

For the past few days I've been working through a few quick projects using the turtle model and it's been a ton a fun. Although I find myself more interested in learning about how the back end operates I can't look away from pretty colors and cool shapes. What can I say? It's the artist in me.

The first day I started off with simply learning how to use the functions in the module to make the turtle move in a random path while also leaving a randomly colored trail behind. It turned out really cool. It looks very chaotic but the kind of chaos that you would want to be a part of.

Screen Shot 2022-06-05 at 11.21.15 AM.png

I used the random.randint() function from the random module to generate the RGB colors to be chosen within the random_color() function that I created to package all of the steps nicely. In the code below, I set the directions to have the turtle face north, south, east and west and made it so that the directions were able to be randomly selected while Tim the turtle was taking his walk.

import turtle
from turtle import Turtle, Screen
import random

# making tim walk in random directions with a trail of random colors
tim = Turtle()

screen = Screen()
tim.shape("turtle")
directions = [0, 90, 180, 270]
tim.speed('fastest')
tim.pensize(10)
turtle.colormode(255)


def random_color():
  r = random.randint(0, 255)
  g = random.randint(0, 255)
  b = random.randint(0, 255)
  random_color = (r, g, b)
  return random_color


for num in range(500):
    tim.forward(25)
    tim.color(random_color())
    tim.setheading(random.choice(directions))

Spirograph

Next, I was able to make Tim create a Spirograph. Do you remember those cool Spirograph art sets that we had in the 90s? You could make really pretty shapes with all of those colorful pens. Wow, I am totally ageing myself here but so be it! Proud late 1900's baby here! lol

Anyway, I replaced the bottom for loop above with a method that I created called draw_spirograph(). With that function I was able to make it so that after Tim would draw a circle, the heading, or direction that he is facing would change a few degrees so that he could draw another one that is tilted a bit. I was also able to program Tim to stop drawing circles depending on how big the gap was. The smaller the gap, the more dense (and 3D looking) the Spirograph would look.

def draw_spirograph(sizeofgap):
    for _ in range(int(350 / sizeofgap)):
        tim.color(random_color())
        tim.circle(100)
        tim.setheading(tim.heading() + sizeofgap)


drawspirograph(5)

This is the result of draw_spirograph(5)! Screen Shot 2022-06-05 at 11.25.11 AM.png

Damien Hirst Painting

Have you heard about that guy, Damien Hirst? Some people know him for his (poorly) preserved animal art pieces that sold for millions of dollars. He is also known for his (less crazy) paintings of colored dots. I managed to write a little bit of code that made my turtle, Tim, create his own imitation Damien Hirst painting!

The first thing I did was import the colorgram module. Colorgram is used to extract colors from pictures and make a color palette out of the colors it finds. Cool, right? I thought so too. I created an empty string called rgb_colors and appended the colors (in RGB format) that were found in my reference painting by the extract() method. That created my list of colors that would be randomly selected for the painting later.

import colorgram

rgb_colors = []
colors = colorgram.extract('image.jpg', 30)
for color in colors:
    rgb_colors.append(color.rgb)

print(rgb_colors)

I then renamed rgb_colors to colorlist and inserted that in to the code below.

The hardest part about this project was figuring out how to control Tim's starting point and how to navigate him properly after that. But after making him go completely off the screen a few times and pointing him in the complete opposite direction I managed to direct him in a way that made the most sense for the painting I was trying to recreate.

import turtle as t
import random

tim = t.Turtle()
screen = t.Screen()
t.colormode(255)
tim.speed("fastest")
tim.hideturtle()
tim.penup()

colorlist = [(202, 164, 110), (240, 245, 241), (236, 239, 243), (149, 75, 50), (222, 201, 136), (53, 93, 123),
             (170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35),
             (145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77),
             (183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102), (12, 70, 64),
             (107, 127, 153), (176, 192, 208), (168, 99, 102)]

tim.setheading(225)
tim.forward(300)
tim.setheading(0)
numofdots = 100

Having Tim start with at set heading of 225 pointed him to the bottom left of the screen so I was able to move him 300 paces that way as his starting point. Then, I was able to point him in the direction where he could start drawing his dots from left to right, move up a row, shoot back to the left side and start a new row again until he was done making all of the required dots.

for dotcount in range(1, numofdots + 1):
    tim.dot(20, random.choice(colorlist))
    tim.forward(50)
    if dotcount % 10 == 0:
        tim.setheading(90)
        tim.forward(50)
        tim.setheading(180)
        tim.forward(500)
        tim.setheading(0)

screen.exitonclick()

Here is the final result! Enjoy! Screen Shot 2022-06-05 at 1.03.45 PM.png

I had a great time getting into this but I think I might skip ahead a few lessons because I really would like to get to the web development part and learning about how I can use python to build web applications and secure them soon. Hopefully I can get through that quickly. Until then, see you soon!