Messing around with procedural art - Turtle graphics
Let's take a short break from Pygrr to delve back into the world of Turtle graphics.
Turtle graphics: an introduction
Nice, that didn't clear it up at all! To put it simply, Turtle graphics are a type of programming concept where an object (consider it a "pen") is moved around a 2D canvas, drawing wherever it goes! Most people who know Python will be aware of the Turtle module - this is a Pythonic implementation of Turtle graphics (fun fact: it uses Tkinter too, like Pygrr!). Turtle graphics are a key feature of the programming language "Logo".
Uses of Turtle graphics
Turtle graphics are a popular way to introduce programming to kids, and are often used for this!
They are also used as a quick way to implement graphic representation of data when debugging or writing code, as they are super easy to write. They're not the fastest, so they're typically not used in the final build, but they're invaluable when writing code!
Neither of these are what we're talking about today: today is all about procedural art...
Procedural art, also known as Generative art, is art created with aid from a computer. So, you give the computer some data or rules to follow, and through a typically iterative or procedural process, the computer will yield an art piece.
Make sure to remember that you remember that although it is done with the help of a computer, it is still art, and takes a while to do and master - it's not as simple as click a few buttons, contrary to common belief... Well, I don't have the time to become an artist today, so let's just delve right into making art with Turtle graphics in Python!
So, with Python, you can import the Turtle module to get started! I've defined a small function that when ran, will make the turtle's colour fade from black to red, and red to black (the word "turtle" in lowercase refers to the cursor or "pen" moving throughout the screen, and in uppercase it refers to the Python module as a whole).
I've also gone ahead and disabled Turtle's rendering using tracer(0,0), which will now mean I have to call update() to render the stuff onto the canvas - this allows me to make things run faster, avoiding unnecessary calls to update which happen every action the turtle performs by default.
Now, we make an infinite loop that increments local variable i forever, and that breaks when the space key is pressed (I used onkey() and listen() to do this). I i is directly visible by 100, we will update the screen. This may make it look jittery, as every 100 actions we are rendering the screen, which is much less than the standard 30 or so fps, but it speeds things up!
Now, let's to do some art...
We'll start with a simple but chaotic looking one. In the loop, we'll rotate the turtle by 120 degrees plus i degrees divided by 10,000. We divide i as otherwise it gets too big, and the image rendered is waaaay too big to fit on the screen! Then we'll move the turtle forward by i. Remember, whenever the turtle moves, it draws a line behind it!
This gives us this output image:
And here's the code:
import turtle
i = 0
while True:
turtle.right((i ** 2) / 10_000)
turtle.forward(i)
turtle.right(120)
if i % 100 == 0:
turtle.update()
i += 1
We can do different rotations and moving in the loop, and it'll draw different things. Here are some others I made (these are all single-line drawings - the turtle never takes its pen off the canvas):
Now all we need is an art gallery to contact me!
Isaac, over and out...