Share your repls and programming experiences

← Back to all posts
Python Turtle Graphics - Random Spirals
minermaniac447 (172)

Python Turtle Graphics - Random Spirals


Hello! If you're reading this, I can only assume you want to learn about how to make a random spiral generator with Python Turtle Graphics. If I'm wrong, just give your upvote and leave. (I'm kidding, please stay.)
For this tutorial, I'm going to assume you have some basic knowledge of Python (syntax, how to use variables, etc.) and a more in-depth knowledge of Turtle Graphics. If you don't, please at least read the first 4 chapters or so of this resource. If you need to find more about turtle functions (which you likely will), see these docs. Done reading? Let's start.

I - Initialization


To begin, we need to create an import section. We're going to use a comment, shown by starting the line with a #, to detail what each section is for in the future. To import a package, use the following line of code: import z, where z is the package you want to import. In our case, we want to import turtle and random, so you will have 3 lines total:

#Import statements
import turtle
import random

Next, we need to create our turtle in another section. To create a turtle, use y = turtle.Turtle(), where y is the name of the turtle you want to create. For this example, let's use random1.
Finally, in another section, we're going to hide the turtle, because we don't want it showing while we draw our spirals. This will be done with the random.ht() function, which hides the turtle random1. So, now you should have the following:

#Import Statements
import turtle
import random

#Creating the turtle
random1 = turtle.Turtle()

#Hiding the turtles
random1.ht()

II - Random Assignments


Everything from here onward is going in a huge loop - for drawnum in range(z), where z is the number of different spirals you want. So, now there are 4 things to randomly assign to the turtle - rotation angle, line length, color, and number of lines.


Part 1 - Assigning a random color

This is the most involved of all four values. Side note - I'm sure there's a better way to do this, but I'm doing it this way. Deal with it. First, we're going to make an array and call it hexlist. It's going to have 16 values - 0-9 and A-F. Then, we're going to need to create a variable called c1 which just contains "#". Next, we're going to make a loop that loops 6 times using for i in range(6):, and inside the loop we're going to do 2 things. The first is creating another variable and assigning it a random number between 0 and 15 - let's call it j. Then, we're going to append the value of hexlist at j to c1. The loop code should look like this:

for i in range(6):
  j = random.randint(0,15)
  c1 += hexlist[j]

This should assign a random 6-digit hex code to c1, which we will be using to set its color.


Part 2 - Assigning other random variables

To assign the other three variables - a1, d1, and n1 (angle, distance, number of lines), all we really need are their limits. For a1, I'm going to have a lower limit of -359 and an upper limit of 359. That way, it can choose any rotation amount left or right nearly all the way around the circle - but not all the way. d1 will be a random value between 1 and 300 for this example, and n1 will be between 1 and 360. So, these initalizations should look like this:

a1 = random.randint(-359,359)
d1 = random.randint(1,300)
n1 = random.randint(1,360)

Part 3 - How to create segments

Now, if you want it to have 'hooks' off of each line, it's super easy to add. For each seperate hook, just copy the lines used to create c1, a1, and d1, and add an a after the end of the variable name (c1 to c1a). To add another hook, just change the letter after the number. It's that simple!

III - Drawing the spiral


First, create a new section within our loop and set random1's speed to 0. Then, we're going to make a loop that loops n1 times. In this loop, we're going to do a few different things - set random1's color to c1, have it go forward d1, optionally draw more segments (see Part 2), go back to the center and rotate a1 degrees, and repeat.


Part 1 - Setting up, drawing the main line

This section is very easy. First, send random1.speed(0) to make it as fast as possible. Then, to make a loop loop n1 times, we're going to use the following: for x in range(n1):. Then, inside the loop, we're going to set random1's color to c1 and have it go forward by d1. That should look like this:

  #Drawing random1
  random1.speed(0)
  for x in range(n1):
    #Line segment 1
    random1.color(c1)
    random1.fd(d1)

Part 2 - Drawing 'hooks'

This section can be repeated for each 'hook' off of the main line that you want to draw. First, set the color of random1 to c1a. Then, turn left by a1a degrees and go forward d1a pixels. To add another hook after, just recopy these 3 lines, paste them in, and change the a after the number to a b or a c to match the number of hooks created in II Part 3. This is how it should look with just one hook:

    #Line segment 2
    random1.color(c1a)
    random1.lt(a1a)
    random1.fd(d1a)

Part 3 - Resetting to redraw

To reset ourself to redraw that line again which creates a spiral, first we need to pick up the pen and go to the center (0,0). Then, we need to turn right by every value of a1x, where x is the letter corrosponding to a hook. Finally, we need to turn left by a1 degrees and put down the pen. This is what you should have:

    #Going to the next position
    random1.up()
    random1.goto(0,0)
    random1.rt(a1a)
    random1.lt(a1)
    random1.down()

If you compile everything together and press run, you should now have a working program! Congratulations, and have fun messing around with it! If you liked this and want more of a challenge, check out this tutorial. If you want to learn basically everything about LOLCODE, check out this! If you liked this tutorial, feel free to upvote. Thanks!