Computer ScienceChapter 3: Programming Fundamentals (Python)
3.4 Turtle Graphics
Turtle Graphics
Explanation level
Introduction
Python provides a fun and visual way to learn programming through Turtle Graphics. It allows you to draw shapes and pictures by giving commands to a small "turtle" that moves around the screen. Turtle Graphics is installed with Python by default, so you can start using it immediately without installing anything extra.
Definition
Turtle Graphics is a built-in Python library that provides a virtual drawing board where a small pen (called a "turtle") can be moved around to draw lines and shapes using simple commands.
In simple words, Turtle Graphics lets you draw pictures by telling a turtle where to move and when to put its pen down or lift it up.
Main Idea
The turtle graphics is installed with Python and we have just to invoke it and use it. No need to install anything to use it. It provides functions which can be easily used even from the shell prompt. Through these functions we can design basic drawings and two-dimensional geometrical shapes.
It is easy to use and provides an interactive session and motivation for the novice. Nevertheless, turtle provides advanced features which are quite useful for the development of animations, simulations and computer games.
Core Concept
The core concept of Turtle Graphics is:
- A turtle (pen) starts at the center of the screen.
- You give commands like "move forward," "turn left," "turn right," "pick up pen," "put down pen."
- As the turtle moves with the pen down, it draws lines on the screen.
- By combining these simple commands, you can draw complex shapes and pictures.
Detailed Explanation
Getting Started with Turtle Graphics
On the Shell, the first statement needs to be: ```python import turtle ```
This command will invoke the turtle library and all the functions of the said library can now be used.
The very next line is: ```python turtle.Screen() ```
This loads the canvas of the turtle graphics. As soon as a white canvas loads, you will be able to see a small arrow (called a "turtle") which is the pen that will draw our lines and shapes. This pen is called turtle and correspondingly the name turtle graphics arose.
Basic Commands
Resizing the Canvas: ```python turtle.screensize(200, 150, "coral") ``` This resizes the canvas and changes its color. Keep in mind that all measurements that are used in turtle graphics are in terms of pixels.
Drawing a Dot: ```python turtle.dot(10, "Blue") ``` This draws a dot of blue color of size 10 pixels.
Drawing a Circle: ```python turtle.circle(50) ``` This draws a circle with radius 50 pixels. By default, the pen color is "Black."
Changing Pen Color: ```python turtle.pencolor("Blue") ``` This changes the pen color to Blue. Another circle drawn after this will have a blue circumference.
Example — Drawing Circles: ```python import turtle turtle.Screen() turtle.screensize(200, 150, "coral") turtle.dot(10, "Blue") turtle.circle(50) turtle.pencolor("Blue") turtle.circle(-50) ```
Output: A coral-colored canvas with a blue dot and two circles — one black, one blue.
Important Point: Every instruction is executed from the point where the pen is currently located.
Drawing a Triangle
To draw a triangle, you need to play with angles. The drawing of the first line with `forward()` function is easy, as earlier. Thereafter, depending upon what type of triangle you want to draw, you will set the angle and length of line accordingly.
This way, you will be thankful to your school teachers who taught you all the trigonometric formulae and the resultant shape of the triangle is visible on screen.
Example — Drawing a Triangle: ```python turtle.reset() turtle.forward(140) turtle.left(135) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(45) turtle.left(90) ```
- `turtle.reset()` — Clears the canvas
- `turtle.forward(140)` — Draws a line 140 pixels forward
- `turtle.left(135)` — Turns the pen 135 degrees left
- `turtle.forward(100)` — Draws a line 100 pixels forward
- `turtle.left(90)` — Turns 90 degrees left
- `turtle.forward(100)` — Draws another line 100 pixels
- `turtle.left(45)` — Turns 45 degrees left
- `turtle.left(90)` — Turns 90 degrees left
Drawing a Car Shape
To clear up the canvas, `turtle.reset()` is used. Let's use the most basic line drawing functions of `forward()` and `back()`. These functions take distance in pixels — how far you want to draw a line and in which direction.
- `forward()` will draw in the direction of the pen
- `back()` will draw in the reverse direction of the pen
- `forward(100)` — Draws a line 100 pixels forward
- `left(90)` — Turns the pen 90 degrees left
- `right(90)` — Turns the pen 90 degrees right
- `penup()` — Lifts the pen (no drawing when moving)
- `pendown()` — Puts the pen down (drawing when moving)
- `circle(-10)` — Draws a small circle (negative radius draws in opposite direction)
- `dot(5, "Blue")` — Draws a blue dot of size 5
Example — Drawing a Car: ```python turtle.forward(100) turtle.left(90) turtle.forward(25) turtle.left(90) turtle.forward(25) turtle.right(90) turtle.forward(25) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(25) turtle.right(90) turtle.forward(25) turtle.left(90) turtle.forward(25) turtle.pencolor("Blue") turtle.penup() turtle.left(90) turtle.forward(25) turtle.pendown() turtle.circle(-10) turtle.penup() turtle.right(90) turtle.forward(10) turtle.dot(5, "Blue") turtle.back(10) turtle.left(90) turtle.forward(50) turtle.pendown() turtle.circle(-10) turtle.penup() turtle.right(90) turtle.forward(10) turtle.dot(5, "Blue") ```
Output: A basic car shape with two wheels (circles) and blue dots at the center of each wheel.
Key Turtle Graphics Functions
| Function | Purpose |
|---|---|
| `turtle.Screen()` | Creates the canvas |
| `turtle.screensize(w, h, color)` | Resizes and colors the canvas |
| `turtle.forward(distance)` | Moves the pen forward |
| `turtle.back(distance)` | Moves the pen backward |
| `turtle.left(angle)` | Turns the pen left by angle degrees |
| `turtle.right(angle)` | Turns the pen right by angle degrees |
| `turtle.penup()` | Lifts the pen (no drawing) |
| `turtle.pendown()` | Puts the pen down (drawing) |
| `turtle.pencolor(color)` | Changes the pen color |
| `turtle.circle(radius)` | Draws a circle with given radius |
| `turtle.dot(size, color)` | Draws a dot of given size and color |
| `turtle.reset()` | Clears the canvas and resets the turtle |
Intermediate Level
How Turtle Graphics Works
The turtle starts at the center of the canvas, facing east (right). The position is (0, 0) and the direction is 0 degrees.
- Position: Where the turtle is on the canvas (x, y coordinates)
- Direction: Which way the turtle is facing (angle in degrees)
- Pen state: Whether the pen is up (not drawing) or down (drawing)
- `forward()` and `back()` change position (and draw if pen is down)
- `left()` and `right()` change direction
- `penup()` and `pendown()` change pen state
Pixels and Coordinates
All measurements in turtle graphics are in pixels.
- (0, 0) is the center of the canvas
- Positive x is to the right
- Positive y is upward
- Negative x is to the left
- Negative y is downward
When the turtle moves forward, it moves in the direction it is facing. When it moves backward, it moves in the opposite direction.
Angles in Turtle Graphics
Python by default uses degrees for angles (not radians). However, Python also provides the provision for radians if needed.
- `left(90)` — turns 90 degrees to the left (counterclockwise)
- `right(90)` — turns 90 degrees to the right (clockwise)
- 90 degrees = quarter turn
- 180 degrees = half turn
- 360 degrees = full turn
Drawing Shapes — Formula
For a regular polygon with **n sides**: - Exterior angle = 360n
| Shape | Sides | Exterior angle | Command |
|---|---|---|---|
| Triangle | 3 | 120° | `left(120)` |
| Square | 4 | 90° | `left(90)` |
| Pentagon | 5 | 72° | `left(72)` |
| Hexagon | 6 | 60° | `left(60)` |
| Octagon | 8 | 45° | `left(45)` |
Pen Up and Pen Down
The `penup()` and `pendown()` functions are used to move the turtle without drawing.
Example — Drawing Two Separate Lines:
```python import turtle turtle.Screen()
First line turtle.forward(100)
Move without drawing turtle.penup() turtle.forward(50) turtle.pendown()
Second line turtle.forward(100) ```
Output: Two lines of 100 pixels each, separated by a gap of 50 pixels.
Using `turtle.reset()`
- Clears the entire canvas
- Resets the turtle to the center (0, 0)
- Sets the direction to east (0 degrees)
- Sets the pen color to black
- Puts the pen down
This is useful when you want to start a new drawing without closing the window.
Formula / Mathematical Form
For a regular polygon with n sides: ``` Exterior angle = 360n ```
Examples:
| n (sides) | Exterior angle |
|---|---|
| 3 | 120° |
| 4 | 90° |
| 5 | 72° |
| 6 | 60° |
| 8 | 45° |
| 10 | 36° |
For a circle approximation: ``` Repeat 36 times: Forward small distance Turn 10 degrees ```
Solved Example
Example — Draw a Pentagon
Given: A pentagon has 5 sides. Required: Draw a regular pentagon using Turtle Graphics. Formula: Exterior angle = 3605 = 72°
Code: ```python import turtle turtle.Screen()
for i in range(5): turtle.forward(100) turtle.left(72) ```
Explanation: 1. The loop
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Same supplied content as this Notes page (shared canonical source).