📜  在Python中使用 Turtle 绘制彩色立体立方体

📅  最后修改于: 2022-05-13 01:54:49.790000             🧑  作者: Mango

在Python中使用 Turtle 绘制彩色立体立方体

Turtle 是Python中的一个内置模块。它提供:

  1. 使用屏幕(纸板)绘图。
  2. 乌龟(笔)。

要在屏幕上绘制一些东西,我们需要移动这个海龟(笔)并移动海龟(笔),有一些函数,如 forward()、backward() 等。

先决条件: 海龟编程基础

绘制彩色立体立方体

在本节中,我们将讨论如何绘制一个实心立方体。

方法:

代码:

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


输出 :

使用 Turtle 图形的实体立方体