Python Turtle is something that evolved from Logo programming language, invented in 1966 by Wally Feurzig. With the aid of Object Oriented Programming approach, we can create an impressive set of animations easily. The following animation was created by Python Turtle; the code is at the bottom of this tutorial. The RequirementsBefore using Python Turtle for animations, please take the following steps to install the environment:
The best way to learn the Python turtle is running set of codes, from the simplest to the more advanced gradually, rather than making an effort to understand the simulator fully at first. This is the approach adopted in this tutorial. The documentation of Python turtle is here. Here are some of the turtle methods; they direct the turtle what to do: InstructionsMeaningturtle.forward(number)Move forwardturtle.back(number)Move backwardturtle.right(angle)Turn clockwiseturtle.left(angle)Turn anti-clockwiseturtle.pencolor(colour string)Drawing colourturtle.pensize(number)Choosing size of pen nibturtle.circle(radius)Drawing a circleturtle.speed(number)Choosing speed - 1 to 10turtle.write(message,font)Writing on the screenturtle.ht()Hiding the turtleturtle.setpos(x,y)Changing the position of turtleDrawing a right-angleThe following animation shows the turtle at work in producing a right-angle on the screen: This is the code for the above animation: import turtle # importing the module # sign indicates the comments in Python scripts. Drawing a SquareThe following animation shows how the turtle draws a square on the screen: This is the code for the above animation: import turtle # importing the module Although, the above code produces a square, it is not good programming practice due to repetition of the code, which could have been tackled by a simple loop. Therefore, the code can be revised to produce the same shape with efficiency as follows: import turtle # importing the module The for loop makes the drawing of the square much easier. It role is really important when we create other polygons. Recommended BooksIf you are learning computer science at GCSE, here is a set of books for you: they are revision guides, yet they cover every single topic, while giving ample information to grasp the concepts in an innovative way; the books show a clear path to follow, something that the bulky text books fail to do; they are good even for a sixth former. Drawing PolygonsIn order to change the above code to draw polygons, we need to take into account some concepts in geometry. As you can see, with each turn, the turtle moves through a certain angle, specified by right(angle). This is the exterior angle of the polygon, which is the same as 360/n, where n is the number of sides. With for and while loops, the following code produces a set of polygons on screen - from a triangle to a decagon. The code for the above animation is as follows: import turtle # importing the module If you want to practise it interactively, here is the code: Drawing Letter EPlease note how the image is centred on the screen, with turtle.setpos() method. Drawing CirclesHere is the code for adding two numbers and drawing a circle: import turtle # importing the module The following animation shows how the two numbers are taken in as two inputs and later answer is given out: As you can see, the starting point of the circle is the centre of the screen by default, which is not the centre of the circle. In order to get round this problem, we have to set the position by code as follows: import turtle # importing the module Ad: No more sore throat suffering with this: Drawing Concentric CirclesHere is the code for drawing concentric circles with the aid of loops: import turtle # importing the module The following animation shows how the animation works: Changing ColoursThe colour of the pen can be changed in many different ways; here are two ways:
If you use the second method, red, green and blue can be any integer between 1 - 255. However, before that, turtle.colormode(1) or turtle.colormode(255) must be declared in the code. Here is the code: import turtle # importing the module The following animation shows the output - with random colours, of course. Advanced Animations - turtle in its habitat!The following code creates a turtle that moves around on a beach - leaving behind a certain regular pattern! import turtle The following animation shows the iteration that leads to a countdown, based on the user input: Advanced Animations - diverging turtlesIn this animation, turtles leave their foot print on the beach: this is achieved by turtle.stamp() method along with turtle.penup(). This is the code: import turtle The animation is as follows: Advanced Animations - colourful starWith the aid of simple geometry, a star can be drawn by Python Turtle. This is the code: import turtle This is the animation that produces the star: Advanced Animations - a clock dialIn the following animation, Python Turtle is used to construct a clock dial - with numbers, 1 to 12, surrounded by a circle. This is the code: import turtle This is the animation at work: Advanced Animations - multi-coloured flowerThe following code produces a flower in different colours. The colours are provided with the aid of a list.The code is as follows: trtl=turtle.Turtle() This is the animation: As you can see, the effect is produced by turtle.seth() function - in turning the direction of turtle. Drawing Multiple SquaresIn this animation, predetermined number of squares are drawn with the aid of a function. The function has two parameters - length of the square and the colour. Then, using a for loop, we can draw the number of squares we need. You can change the length and colour in order to practise interactively. |