Draw Cube and Cuboid in Python using Turtle
Last Updated :
22 Jun, 2020
Prerequisite: Turtle Programming Basics
Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move the turtle, there are some functions i.e forward(), backward(), etc.
Drawing Cube
Following steps are used:
- First draw the front square
- Move to back square through one bottom left side
- Draw the back square
- Draw the remaining side as shown in code.
Below is the implementation.
Python3
import turtle
tut = turtle.Screen()
tut.bgcolor( "green" )
tut.title( "Turtle" )
my_pen = turtle.Turtle()
my_pen.color( "orange" )
tut = turtle.Screen()
for i in range ( 4 ):
my_pen.forward( 100 )
my_pen.left( 90 )
my_pen.goto( 50 , 50 )
for i in range ( 4 ):
my_pen.forward( 100 )
my_pen.left( 90 )
my_pen.goto( 150 , 50 )
my_pen.goto( 100 , 0 )
my_pen.goto( 100 , 100 )
my_pen.goto( 150 , 150 )
my_pen.goto( 50 , 150 )
my_pen.goto( 0 , 100 )
|
Output :
Drawing Cuboid
Following steps are used:
- First draw the front rectangle
- Move to back rectangle through one bottom left side
- Draw the back rectangle
- Draw the remaining side as shown in code.
Below is the implementation.
Python3
import turtle
tut = turtle.Screen()
tut.bgcolor( "green" )
tut.title( "Turtle" )
my_pen = turtle.Turtle()
my_pen.color( "orange" )
tut = turtle.Screen()
for i in range ( 2 ):
my_pen.forward( 100 )
my_pen.left( 90 )
my_pen.forward( 150 )
my_pen.left( 90 )
my_pen.goto( 50 , 50 )
for i in range ( 2 ):
my_pen.forward( 100 )
my_pen.left( 90 )
my_pen.forward( 150 )
my_pen.left( 90 )
my_pen.goto( 150 , 50 )
my_pen.goto( 100 , 0 )
my_pen.goto( 100 , 150 )
my_pen.goto( 150 , 200 )
my_pen.goto( 50 , 200 )
my_pen.goto( 0 , 150 )
|
Output :
Share your thoughts in the comments
Please Login to comment...