Draw Colored Solid Cube using Turtle in Python
Last Updated :
06 Jul, 2020
Turtle is an inbuilt module in Python. It provides:
- Drawing using a screen (cardboard).
- Turtle (pen).
To draw something on the screen, we need to move this turtle (pen) and to move the turtle(pen), there are some functions like the forward(), backward(), etc.
Prerequisite: Turtle Programming Basics
Drawing Colored Solid Cube
In this section, we will discuss how to draw a solid cube.
Approach:
- Import turtle
- Set window screen
- Set color of turtle
- Form a face of cube
- Fill the color
- Repeat step 3, 4 and 5 for two another faces.
Code:
python3
import turtle
pen = turtle.Turtle()
x = 120
def right():
pen.left( 45 )
pen.forward(x)
pen.right( 135 )
pen.forward(x)
pen.right( 45 )
pen.forward(x)
pen.right( 135 )
pen.forward(x)
def left():
pen.left( 45 )
pen.forward(x)
pen.left( 135 )
pen.forward(x)
pen.left( 45 )
pen.forward(x)
pen.left( 135 )
pen.forward(x)
def top():
pen.left( 45 )
pen.forward(x)
pen.right( 90 )
pen.forward(x)
pen.right( 90 )
pen.forward(x)
pen.right( 90 )
pen.forward(x)
pen.right( 135 )
pen.forward(x)
pen.color( "red" )
pen.begin_fill()
right()
pen.end_fill()
pen.color( "blue" )
pen.begin_fill()
left()
pen.end_fill()
pen.color( "green" )
pen.begin_fill()
top()
pen.end_fill()
|
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...