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
May be interested
- How to draw broken lines in Wordto draw broken lines in word users need to perform indirectly through drawing straight lines.
- Instructions on how to draw diagrams in Wordthe use of drawing models in word content will help readers more generalize and better understand the content, as well as increase the content of the content.
- What is a mangrove?what is a mangrove? this article will provide you with the information you need to know about mangroves!
- Swallowing nearly 1,000 coins, Thai turtles died of blood infectionsthe doctors had surgery to remove nearly 1,000 coins from the turtle's body, but it was still unable to survive because of a blood infection.
- Manipulate shapes in PowerPoint 2016shapes are a great way to make presentations more interesting. powerpoint gives users a lot of different shapes to choose from and they can be customized to suit each person's individual needs.
- How to include shapes in PowerPointcombining shapes in powerpoint creates more unique shapes in the slide.
- How to draw arrows in Wordthe following article shows how to draw arrows in word to help you create diagrams quickly. how to draw diagrams and simple cubes in word.
- Instructions on how to draw organizational structure diagram in Wordto draw an organization chart in word you can use one of two ways: using smartart and using the shapes tool. using smartart will help you draw an organization chart faster than using the shapes tool.
- More than 100 Python exercises have solutions (sample code)more than 100 python code examples are shared by guy zhiwehu on github, however, the solution of this series is written on the old python version. following tipsmake.com will be vietnameseized and edited to suit python 3.x to help you learn and practice python.
- How to draw mind maps using PowerPointhow to draw mind maps using powerpoint. similar to drawing a mind map with word, with powerpoint you can also use tools to draw shapes and lines to be able to draw a simple mind map.