在Python中使用Turtle绘制彩色实体立方体
Turtle是Python中的一个内建模块。它提供了
1.使用屏幕(纸板)绘图。
2.Turtle(笔)。
为了在屏幕上画东西,我们需要移动这个Turtle(笔),为了移动这个Turtle(笔),有一些函数,如forward(), backward(),等等。
绘制彩色实心立方体
在本节中,我们将讨论如何绘制一个实体立方体。
步骤:
1.进口Turtle
2.设置窗口屏幕
3.设置Turtle的颜色
4.形成立方体的一个面
5.填充颜色
6.重复步骤3、4和5,再做两个面。
代码:
# 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()
输出 :