Advertisement

Mutant Rainbow ( Python Project )

 Mutant Rainbow

You can program Python’s turtle to draw all sorts of patterns and designs. But watch out! Looks like the turtle in this project has gone a bit wild— you wouldn’t see rainbows like this in the sky!



What happens

The program will ask you to choose the length and thickness of the line that the turtle paints. The turtle then scurries around the screen until you stop the program, painting colored lines as it goes. The type of pattern it makes will change, depending on the length and thickness of the lines.

How it works

Every pattern in this project is different because the program tells the turtle to face a random new direction before painting each line. The color for each line is also chosen at random from a list of possible colors you’ve coded. So you can never predict exactly what the turtle will do!

Mutant Rainbow flowchart

The program uses an infinite loop that continues to paint colored lines for as long as the program is running. Only when you close the window will the turtle stop its crazy wanderings.


Getting started

Start by setting up and saving a new file, importing the modules that the program will need, and making a couple of useful functions to get user input.

1     Create new files

Open IDLE and create a new file. Save it as “rainbow.py”.

2     Add the modules

Type these two lines at the top of your file to import the Turtle module and the random module. Remember to use import turtle as t, so that you don’t have to type the word “turtle” every time you want to use a function from the Turtle module. You can just call it t.

3     Assign line length

Next make a function that will let the user decide whether the turtle paints long, medium, or short lines. You won’t use it until Step 4, but this will get the program ready for when you need it. Type this bit of code beneath the code in Step 1.

4     Define thickness

In this step, you’ll create a function that will let the user choose whether the turtle paints superthick, thick, or thin lines. Like the get_line_length() function, you won’t use it until Step 5. Type the code shown here, under the code you added in Step 3.

5     Use the functions

Now that you’ve built the two functions, you can use them to get the user’s choices for line length and width. Type these lines at the end of your code, then save your work.

6     Test the program

Run the code to see the new functions in action in the shell. They’ll ask you to select the length and width of the lines.

Summon the turtle!

It’s time to write the code that will create a graphics window and bring in the turtle to do the drawing.

7     Open a window

Type the lines shown here under the code you added in Step 5. This code defines the background color of the window, the shape, color, and speed of the turtle, and the width of the pen the turtle will use to draw lines.

8     Run the project

If you run the code once more, a window will appear after you’ve entered the line sizes in the shell window. You will now see the turtle. Take a good look at it, because it won’t be sitting still for too long!


9     Keep inside the limits!

To stop the turtle from straying, let’s set a boundary 100 steps in from the edges of the window. Create this function to check whether or not the turtle is inside the boundary. Type the code shown here under the code in Step 4 and above the code in Step 5.


Move that turtle!

Now you’re ready to write the function that gets your turtle moving. The last bit of the code will be a while loop that sets the turtle off drawing mutant rainbows!

10    Mutant line

Add this code below the code you typed in Step 9, and above the code you typed in Step 5. This function makes the turtle turn and move forward in a new direction, drawing a single line of random color as it goes. Your main program will use it over and over again to draw mutant rainbows. If the turtle strays beyond the limits you set in Step 9, this function will bring it back.

How it works

The code calls the inside_window() function to see if the turtle is within the window limits. If it is, the turtle turns right by a random amount between 0 degrees (doesn’t turn at all) and 180 degrees (faces the opposite direction), then moves off again. If it has gone too far from the limit, it moves backward.

Go, Turtle, Go!

Finally, add the code that will actually start your turtle drawing. Type these two lines right at the bottom of your code, under the commands you added in Step 7. Then save and run the code to see your first mutant rainbow!

Post a Comment

0 Comments