Thursday, July 4, 2024
HomeLanguagesPythonDraw Colored Solid Cube using Turtle in Python

Draw Colored Solid Cube using Turtle in Python

Turtle is an inbuilt module in Python. It provides: 

  1. Drawing using a screen (cardboard).
  2. 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: 

  1. Import turtle
  2. Set window screen
  3. Set color of turtle
  4. Form a face of cube
  5. Fill the color
  6. Repeat step 3, 4 and 5 for two another faces.

Code:

python3




# Draw color-filled solid cube in turtle
  
# Import turtle package
import turtle
  
# Creating turtle pen
pen = turtle.Turtle()
  
# Size of the box
x = 120
  
# Drawing the right side of the cube
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)
  
# Drawing the left side of the cube
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)
  
# Drawing the top side of the cube
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)
  
  
# Set the fill color to 
# red for the right side
pen.color("red")
  
# Start filling the color
pen.begin_fill()
right()
  
# Ending the filling of the color
pen.end_fill()
  
# Set the fill color to 
# blue for the left side
pen.color("blue")
  
# Start filling the color
pen.begin_fill()
left()
  
# Ending the filling of the color
pen.end_fill()
  
# Set the fill color to 
#green for the top side
pen.color("green")
  
# Start filling the color
pen.begin_fill()
top()
  
# Ending the filling of the color
pen.end_fill()


Output :

Solid Cube Using Turtle Graphics

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments