Agent P, I have been hearing some rumours about a Python Turtle. Please find it and invite it to join O.W.A.C.A.! Aims: For students to gain some experience wth Python's turtle module. Objectives: ALL: Use the turtle module in the interpreter. MOST: Experiment with simple drawings using turtle. SOME: Make simple programs using the turtle module and explore some of its more advanced features.
Have a go at using these basic commands. First we create an instance of the Turtle class. I called it 'a', you can call it what you like. Then we can use the dot notation to invoke the turtle's methods. a.fd(n) -> go forward n pixels. a.lt(n) -> turn left n degrees. a.color(n) -> change the color of the line. a.home() -> go back to 0,0. a.pensize(n) -> change the width of the line. a.up() -> take the pen off the paper.
Have a play with the turtle object. Can you work out how to ... ? -> turn right -> go back -> put the pen down again Mini Tasks In the interpreter: -> draw a triangle -> draw a pentagon -> draw a circle (don't use the built-in method). If you have used Scratch, the turtle module may seem familiar to you. They are both descended from the LOGO programming language.
The center of the turtle's window is 0,0. You can use the “goto” method to move the turtle to a position on the screen by its co-ordinates. The center of the turtle's window is 0,0. You can write words in the turtle window. You can also change the turtle to a turtle shape and stamp an imprint on the screen. If you type the name of your turtle object and a dot, IDLE will show you a list of methods that you could invoke. Have a play and find out what some of them do.
If you succeeded with the triangle and pentagon scripts, you probably noticed that you could generalise the procedure. And a circle is just a polygon with very many sides ....
I asked the program to draw a polygon with 360 sides, all of length 1. Task: For the next activity, we need a function that will draw a quarter of a circle. See if you can work this out for yourself. If you can, I want something that will draw this shape. It is just two quarter-circles facing one another.
Turtle Challenge Once you have the “petal” shape function, you should be able to write a program that draws flowers like this. Choose some nice colours and vary the number of “petals” in the flower until you find a design that you like.
Only if you are stuck ... Here's a possible quarter-circle function. circumference = π * 2 * radius We are going to draw the arc in steps corresponding to one degree. Let's call the length of these “step”. step = (π * 2 * radius) / 360 step = 0.01745 ... For my version, I decided that 0.02 was close enough.
Using fill Just use begin_fill() before you start drawing the shape you want to fill. Then, use end_fill() and the shape will be filled for you.
Task: Have a play with that filled square routine. If you alter the amount of turn you can write a function that produces results similar to these. You'll probably want to use a while loop. How will your program know when it is time to stop?