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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output :

Solid Cube Using Turtle Graphics

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.