Advertisement

Kaleido-spiral (Python Project )

 Kaleido-spiral

In the same way that simple lines of code can form a good program, simple shapes can form a complex picture. By combining shapes and colors through code, Kaleido-spiral will help you create a masterpiece of digital art that’s worthy of an art gallery!




What happens

Python’s turtle draws circles on the screen, one after another. Each time a circle is drawn, the turtle changes the position, angle, color, and size of the next circle it draws. A pattern gradually emerges.

How it works

In this project, you’ll use the turtle module and a clever looping technique to layer circles on top of each other in a spiral pattern. Every time a circle is drawn, the program slightly increases the parameters of the circle-drawing code. Each new circle is different from the last one drawn, making the pattern more interesting.

Kaleido-spiral flowchart

The program sets some values that stay the same throughout, such as the turtle’s speed, and then starts looping. The loop chooses a new pen color, draws a circle, turns and moves the turtle, and then repeats itself. It stops when you quit the program.



Get drawing!

The first thing you’ll draw on the screen is a simple circle. Next you’ll repeat this circle, but with a slight change. Finally, you’ll tweak the code to make the pattern more colorful and interesting.

1     Create a new file

Open IDLE and create a new file. Save it as “kaleido-spiral.py”.

2     Import turtle

First you need to import the turtle module. This will be the main module you use. Type this line at the top of the program.

3     Set up the turtle

The code shown here calls functions in the turtle module to set the background color, as well as the turtle’s speed and size.

4     Choose the pen color, draw a circle

Next set the color of the turtle’s trail and test the code by drawing a circle. Add these two lines to the end of your code and run the program.

5     Draw more circles

You should now see a single circle, but we need lots more. Here comes the clever bit. Put the commands to draw a red circle inside a function, but add a line so that the function calls itself. This trick, known as recursion, makes the function repeat. Remember, functions need to be defined before they’re used, so you’ll need to move the function above the line where it’s called.

6     Test your code

Run the program. You would see the turtle drawing the same circle over and over again. Don’t worry—you’ll fix that in the next step.

7     Change the color, increase the size

To create more exciting patterns, make these changes to the code to increase the size of the circle and change its color. This code uses the cycle() function, which takes a list of values as its parameter and returns a special type of list that you can cycle through endlessly using the next() function. Run the code again.

8     Improve the pattern

Now that you’ve changed the color and size of the circle, you can try a few more things to improve the pattern. Let's give it a zany twist by changing the angle and position at which each circle is drawn. Make the changes highlighted in the code below, then run the program and see what happens.


CODE


import turtle
from itertools import cycle
colors = cycle(['red', 'orange', 'yellow', \
'green', 'blue', 'purple'])


def draw_circle(size, angle, shift):
    turtle.pencolor(next(colors))
    turtle.circle(size)
    turtle.right(angle)
    turtle.forward(shift)
    draw_circle(size + 5, angle + 1, shift +
1)


turtle.bgcolor('black')
turtle.speed('fast')
turtle.pensize(4)
draw_circle(30, 0, 1)


Post a Comment

0 Comments