Shape Spiral

Shape Spiral


Goal


The goal for this project is to recreate the shape spiral on the left. To do this we will break the project down into some smaller pieces.

1. Make a square

2. Change color of lines

3. Make spiral pattern

4. Modify code to change pattern

Make a square

Making a square is simply a combination of forwards followed by 90 degree left turns. The number of times we would need to repeat those steps is equal to the number of sides or 4.


import turtle as t

for i in range(4):
    t.forward(100)
    t.left(90)

Changing color


RGB Color Codes is a system that can be used to make colors by combining different amounts of the three colors red, green, and blue.

The amount of each color is represented as a number between 0 and 255 with 0 being none and 255 being full.

If we take the color code (255, 0, 255) this color combines

255 red (full red)

0 green (no green)

255 blue (full blue)

What color does this make? That's right, purple!


Randomizing color

Obviously we don't want to have to code each color change by hand. Instead, let's have the computer do the work for us by picking a new random color for each line in the spiral.

To make these random colors we can have the computer pick 3 random numbers between 0 and 255 and then combine them to create a RGB color code!

import turtle as t
from random import randint

    r = randint(0,255)
    g = randint(0,255) 
    b = randint(0,255)
     
    t.colormode(255) 
    t.pencolor(r,g,b)


Making a spiral

In general, a spiral starts at a single point, turns into a curved line, and slowly grows bigger in size and radius. So we would need our square to do the same. The square needs to slowly grow in size while rotating. We can tackle these separately then combine the results.

Growing square

While in a loop we can slowing increase the size of the square by adding a multiple of i, or the current iteration value.


import turtle as t

for i in range(25):
    t.forward(50 + 5*i)
    t.left(90)

On the...

1st loop (i = 0) so t.forward(50 + 0)

2nd loop (i = 1) so t.forward(50 + 5)

3rd loop (i = 3) so t.forward(50 + 15)

.

.

.

25th loop (i = 24) so t.forward(50 + 120)

Rotating the square

Making a square requires each turn to be 90 degrees. By making the turn angle slightly bigger than 90 degrees the result still looks like a square, but creates the appearance of a rotating square.


import turtle as t

for i in range(25):
    t.forward(250)
    t.left(90.991)

Putting the pieces together

import turtle as t

for i in range(150):
    t.forward(50 + 5*i)
    t.left(90.991)


And lastly we add in the random colors


import turtle as t
from random import randint

for i in range(400):
 
    r = randint(0,255)
    g = randint(0,255) 
    b = randint(0,255)
     
    t.colormode(255) 
    t.pencolor(r,g,b)
    t.forward(50 + i)
    t.right(90.991)


Final Program


import turtle as t
from random import randint
t.bgcolor('black')
t.speed(100)

for i in range(400):
 
    r = randint(0,255)
    g = randint(0,255) 
    b = randint(0,255)
     
    t.colormode(255) 
    t.pencolor(r,g,b)
    t.forward(50 + i)
    t.right(90.991)