In this Python tutorial, we are going to learn about Python Turtle 3d shapes. Here we will learn to draw 3d shapes in Python Turtle and also cover different examples related to Turtle 3d shapes. Moreover, we will cover these topics.
- Python turtle 3d shape
- Python turtle 3d cube
- Python turtle 3d cuboid
- Python turtle 3d sphere
- Python turtle 3d cylinder
Python turtle 3d shape
In this section, we will learn about how to draw 3d shapes with the help of a turtle in a python turtle.
3d means three-dimension.Three dimensions such as height, width, and depth of the shape. We can draw different three-dimension(3d) shapes the 3d shapes are cube, cuboid, sphere, and cylinder. These 3d shapes are solid in nature and occupy space.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.bgcolor(“black”) is used to give color to the background of the screen.
- tur.title(“Python Guides”) is used to give the title to the screen.
- turt.color(“blue”) is used to give the color to the turtle.
- turt.goto(50,50) is used to move the turtle in an absolute position.
- turt.forward(100) is used to move the turtle in the forward direction.
- turt.left(90) is used to move the turtle in the left direction.
from turtle import *
import turtle
tur = turtle.Screen()
tur.bgcolor("black")
tur.title("Python Guides")
turt = turtle.Turtle()
turt.color("blue")
tut = turtle.Screen()
for i in range(4):
turt.forward(100)
turt.left(90)
turt.goto(50,50)
for i in range(4):
turt.forward(100)
turt.left(90)
turt.goto(150,50)
turt.goto(100,0)
turt.goto(100,100)
turt.goto(150,150)
turt.goto(50,150)
turt.goto(0,100)
turtle.done()
Output:
After running the above code, we get the following output in which we can see that a three-dimension(3d) shape is drawn on the screen.
Also, check: Python Turtle Grid
Python turtle 3d cube
In this section, we will learn about how to draw a 3d cube with the help of a turtle in a python turtle.
A cube is a solid three-dimension(3d)figure. Three dimension cube is known as a regular hexahedron. It has 6 six square faces, twelve edges, and 8 vertices. All faces of the three-dimensional cube are square in shape. Here we draw 3d cube with the help of a turtle on the screen.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.bgcolor(“cyan”) is used to give background color to the screen.
- tur.title(“Python Guides”) is used to give the title to the window.
- turt.color(“black”) is used to give the color to the turtle.
- turt.forward(100) is used to move the turtle in the forward direction.
- turt.left(90) is used to move the turtle in the left direction.
- turt.goto(50,50) is used to move the turtle at its absolute position.
from turtle import *
import turtle
tur = turtle.Screen()
tur.bgcolor("cyan")
tur.title("Python Guides")
turt = turtle.Turtle()
turt.color("black")
tut = turtle.Screen()
for i in range(4):
turt.forward(100)
turt.left(90)
turt.goto(50,50)
for i in range(4):
turt.forward(100)
turt.left(90)
turt.goto(150,50)
turt.goto(100,0)
turt.goto(100,100)
turt.goto(150,150)
turt.goto(50,150)
turt.goto(0,100)
turtle.done()
Output:
After running the above code, we get the following output in which we can see that a 3d cube is drawn on the screen
Read: Python Turtle Star
Python turtle 3d cuboid
In this section, we will learn about how to draw a 3d cuboid with the help of a turtle in Python turtle.
As we know a cuboid is a solid three-dimension figure it has six rectangular faces and each rectangular face is a right angle to the other. It has 12 edges,8 corners, and vertices.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.bgcolor(“black”) is used to give the black color to background.
- tur.title(“Python Guides”) is used to give the title to the window.
- turt.color(“cyan”) is used to give the color to the pen.
- turt.forward(100) is used to move the turtle in the forward direction.
- turt.left(90) is used to move the turtle in the left direction.
- turt.goto(50,50) is used to move the turtle at its accurate position.
from turtle import *
import turtle
tur = turtle.Screen()
tur.bgcolor("black")
tur.title("Python Guides")
turt = turtle.Turtle()
turt.color("cyan")
tut=turtle.Screen()
for i in range(2):
turt.forward(100)
turt.left(90)
turt.forward(150)
turt.left(90)
turt.goto(50,50)
for i in range(2):
turt.forward(100)
turt.left(90)
turt.forward(150)
turt.left(90)
turt.goto(150,50)
turt.goto(100,0)
turt.goto(100,150)
turt.goto(150,200)
turt.goto(50,200)
turt.goto(0,150)
turtle.done()
Output:
After running the above code, we get the following output in which we can see 3d cuboid is drawn on the screen with the help of a turtle.
Read: Python Turtle Background
Python turtle 3d sphere
In this section, we will learn about how to draw a 3d sphere with the help of a turtle in a python turtle.
The sphere is a round-shaped figure. Its looks like a surface of a ball. Each point of the sphere surface is equidistant from its center The surface area of a sphere is 4×3.14xr^3.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.circle(rad,90) is used to draw the circle.
- screen.setup(500,500) is used to give the size to the screen.
- screen.bgcolor(‘black’) is used to give color to thebackground.
- tur.speed(100) give the speed to the turtle.
- tur.seth(-val) is used to orient the sphere at angle = -val.
- tur.color(col[ind]) is used to give color to the sphere.
- tur.hideturtle() is used for hiding the turtle.
from turtle import *
import turtle as tur
screen = tur.Screen()
def draw(rad):
for i in range(2):
tur.circle(rad,90)
tur.circle(rad//2,90)
screen.setup(500,500)
screen.bgcolor('black')
col=['violet','blue','green','yellow',
'orange','red']
val=10
ind=0
tur.speed(100)
for i in range(36):
tur.seth(-val)
tur.color(col[ind])
if ind==5:
ind=0
else:
ind+=1
draw(80)
val+=10
tur.hideturtle()
tur.done()
Output:
After running the above code, we get the following output in which we can see that a beautiful 3d sphere is drawn on the screen.
Read: Python Turtle Random
Python turtle 3d cylinder
In this section, we will learn about how to draw a 3d cylinder with the help of a turtle in a python turtle.
A three-dimension cylinder is a solid geometrical figure. It has straight parallel sides and curved or oval bases. The center of the bases is joined by a line segment.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle.
- rad, heigh = [50, 200] is used to give radius and height in pixel to the circle.
- shape(‘square’) is used to give the shape of a square.
- fillcolor(‘cyan’) is used to fill the color inside the shape.
- shape(‘circle’) is used to give the shape of a circle.
- backward(heigh/2) is used to move the turtle in the backward direction.
- pencolor(‘cyan’) is used to give the color to the pen.
from turtle import *
import turtle
cursor_size = 20
rad, heigh = [50, 200]
shape('square')
shapesize(rad*2 / cursor_size, heigh / cursor_size)
fillcolor('cyan')
stamp()
shape('circle')
shapesize(stretch_len=rad / cursor_size)
backward(heigh/2)
stamp()
forward(5)
pencolor('cyan')
stamp()
forward(heigh - 5)
color('black')
stamp()
done()
Output:
After running the above code, we get the following output in which we can see that a beautiful 3d cylinder with curve and oval base joint with line segment is drawn on the screen.
You may also like to read the following articles on Python Turtle.
- Python Turtle Pen + Examples
- Python Turtle Nested Loop
- Python Turtle Draw Line
- Python Turtle Get Position
So, in this tutorial, we discussed Python Turtle 3d shapes and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle 3d shape
- Python turtle 3d cube
- Python turtle 3d cuboid
- Python turtle 3d sphere
- Python turtle 3d cylinder
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.