Drawing using the Python Turtle
The Python Turtle can be used to draw a number of interesting shapes. The general method is to:
- Create a turtle
- Set its line colour, width, drawing speed, the length of the first line to be drawn to a value, such as 2 pixels, and the angle the turtle is to turn through after each line segment is drawn (e.g. 90 degrees)
- Set its pen to the pen up position
- Go to the point where the drawing is to start
- Put the pen down
- Draw a line of a set length from the starting point towards the right
- Turn the turtle through the previously set angle
- Add a few pixels to the length of the next line to be drawn
- Repeat the line drawing, turning and line lengthening as many times as is wanted
If the turning angle is 90 degrees then the shape drawn will be a squiral. Here is some code that will do that.
import turtle wn = turtle.Screen() wn.bgcolor(0, 0, 0) t = turtle.Turtle() t.speed(0) t.penup() t.pensize(1) t.pencolor(255, 0, 0) number_of_lines = 100 line_length = 2 line_length_increase = 2 turn_angle = 90 t.pendown() for _ in range(number_of_lines): t.forward(line_length) t.left(turn_angle) line_length += line_length_increase t.penup()
To draw more interesting shapes use an angle other than 90 degrees. The program used in this repl does that by choosing a random angle. The line colour and number of lines drawn are also randomised.