How to draw different shapes with a turtle in Python
The graphics system is a great way for beginners to conquer both Python and 2D painting. Here's how to draw different shapes with a turtle in Python.
Python has many modules that provide functionality for a wide variety of tasks. One of them is the turtle module, which is often used to teach programming to beginners.
You can use the turtle module to draw graphics such as lines or shapes on a canvas. Initially, you can draw one or more turtles on the canvas. They represent points that you can move around. When moving around, turtles draw straight lines from one point to another.
You can draw different shapes using this method, including squares, triangles, and circles.
How to use turtle to draw shapes
You can use Python to create many practical programs, such as color palettes. When drawing turtles in Python, first you need a canvas, and a turtle object to draw the shapes.
1. Create a new file named shapes.py .
2. At the top of the file, import the turtle module:
import turtle
3. Use the turtle module to create a new window. It will be like a canvas to draw shapes on:
window = turtle.Screen() window.bgcolor("white")
4. Create a new turtle object, choose its shape and color:
turtle_1 = turtle.Turtle() turtle_1.shape("turtle") turtle_1.color("red")
5. At the bottom of the file, use the exitonclick() function to keep the canvas open. This canvas will only close when you click on it. Make sure you keep this line as the last command in the program. Otherwise, you will get an error.
window.exitonclick()
6. Open a command prompt or terminal, and navigate to the directory where the Python script is saved. For example, if you save a Python file on the desktop of your Windows PC, your command will look similar to this:
cd C:UsersSharlDesktop
7. Run the Python command to open the canvas turtle. By default, the turtle's initial orientation and facing right:
How to draw squares and rectangles
Draw a square or rectangle with a turtle on the canvas. Use the forward() and backward() functions to move the turtle around. You can also use the left() or right() function to change the direction the turtle is facing.
1. Since the turtle is already facing the original direction, move forward 100 pixels to draw the first line of the square:
turtle_1.forward(100)
2. Turn left 90 degrees to face down. Move forward again to draw the second side of the square:
turtle_1.right(90) turtle_1.forward(100)
3. Continue to rotate to the right 90 degrees to draw the remaining two edges. To keep the square shape, make sure the sides are 100 pixels in size:
turtle_1.right(90) turtle_1.forward(100) turtle_1.right(90) turtle_1.forward(100)
4. To change the shape to a rectangle, edit the code to make the two opposite sides longer:
turtle_1.forward(100) turtle_1.right(90) turtle_1.forward(200) turtle_1.right(90) turtle_1.forward(100) turtle_1.right(90) turtle_1.forward(200)
5. Run the Python command again using a command line or terminal to open the canvas and view the rectangle:
How to draw triangles
Draw a triangle using the forward(), backward(), left(), or right() function. Since you drew a square in the starting position of the canvas, draw a triangle on another part of the canvas.
1. Create a new turtle object. You can change the shape to use an arrow instead of a turtle :
turtle_2 = turtle.Turtle() turtle_2.shape("arrow") turtle_2.color("blue")
2. Use the penup() function to lift the turtle object off the canvas, so that it no longer draws a straight line:
turtle_2.penup()
3. Move the object to another point on the canvas. Canvas uses a Cartesian plane for coordinates, so its starting point is at 0,0. Coordinates -250, 200 point towards the top left corner of the canvas.
turtle_2.goto(-250, 200)
4. Use the pendown() function to place the turtle object back on the canvas, then the turtle can draw the lines again:
turtle_2.pendown()
5. Draw 3 sides of the triangle by rotating left 120 degrees 3 times. One line at a time to create the side of the triangle:
turtle_2.forward(100) turtle_2.left(120) turtle_2.forward(100) turtle_2.left(120) turtle_2.forward(100) turtle_2.left(120)
6. Alternatively, create a triangle with a for-loop:
for i in range(3): turtle_2.forward(100) turtle_2.left(120)
7. Run the Python command again using a command line or terminal to open the canvas and view the triangle:
How to draw a circle
Use the circle() function to draw a circle. You can select the diameter of the circle by entering a value as an argument to this function.
1. Create another new turtle object to draw the circle, set its color and shape properties:
turtle_3 = turtle.Turtle() turtle_3.shape("circle") turtle_3.color("green")
2. Move the turtle to another part of the canvas. Use the penup() function to prevent an object from drawing a line as it moves. After moving the turtle, put the pen down:
turtle_3.penup() turtle_3.goto(-100, 50) turtle_3.pendown()
3. Use the circle() function to draw a circle. Enter the diameter of the circle into the function.
turtle_3.circle(50)
4. Run the Python command again using the command line or terminal to open the canvas and see the circle:
How to draw shapes like hexagons and octagons
To draw shapes like hexagons and octagons, you can use a for-loop similar to the function used in the triangle drawing example. You can choose the same number of sides and equal side lengths.
1. Create a new turtle object, set shape and color properties:
turtle_4 = turtle.Turtle() turtle_4.shape("turtle") turtle_4.color("purple")
2. Move the turtle object to another location on the canvas:
turtle_4.penup() turtle_4.goto(150,200) turtle_4.pendown()
3. In a hexagon, at the intersection of each side, the angle at that point is 60 degrees. After drawing a line, move 60 degrees to the right to draw the next line. Do this for all 6 sides of the hexagon:
for i in range(6): turtle_4.forward(100) turtle_4.right(60)
4. You can draw another type of shape with equal sides by dividing the number of sides by 360 to calculate the value of the angle. For an octagon, each angle between the sides is 45 degrees. For the octagon, rotate each side to the right at a 45 degree angle and draw a straight line:
for i in range(8): turtle_4.forward(100) turtle_4.right(45)
5. Run the Python command again using a command line or terminal to open the canvas and view the hexagon or octagon. You can also view the full source code for all shapes on Github.
Above is how to draw different shapes with turtle in Python . Hope the article is useful to you.
You should read it
- More than 100 Python exercises have solutions (sample code)
- Bookmark 5 best Python programming learning websites
- For in Python loop
- Manage files and folders in Python
- Multiple choice quiz about Python - Part 3
- 5 choose the best Python IDE for you
- What is Python? Why choose Python?
- Module time in Python
- Python data type: string, number, list, tuple, set and dictionary
- How to install Python on Windows, macOS, Linux
- How to set up Python to program on WSL
- Multiple choice quiz about Python - Part 4
Maybe you are interested
This program will help you become invisible before the webcam lens is recording live Brain research explains the difference in the impact of technology on men and women 18 'extremely creative' inventions are available only in Japan 'Eyes glued' to the screen before going to bed is not harmful to the mental health of the bar, teenager! Changing these 10 ways of speaking will help you advance like 'windy kites'. Can ants survive if they fall from the roof of the building?