编一个立方灯程序
时间: 2023-07-25 16:03:31 浏览: 74
这里给出一个Python实现的立方灯程序,使用了turtle库来绘制立方体。代码如下:
```python
import turtle
def draw_cube(size):
for i in range(4):
turtle.forward(size)
turtle.left(90)
turtle.left(45)
turtle.forward(size/2)
turtle.right(135)
turtle.forward(size)
turtle.right(135)
turtle.forward(size/2)
turtle.left(45)
turtle.backward(size/2)
turtle.right(45)
turtle.forward(size/2)
turtle.right(45)
turtle.forward(size)
turtle.left(45)
turtle.backward(size/2)
turtle.right(45)
turtle.backward(size/2)
turtle.right(45)
turtle.backward(size)
turtle.left(45)
turtle.forward(size/2)
turtle.right(45)
turtle.backward(size/2)
turtle.right(45)
turtle.backward(size)
turtle.left(45)
turtle.backward(size/2)
turtle.right(45)
turtle.backward(size/2)
turtle.right(45)
turtle.backward(size/2)
turtle.speed("fastest")
turtle.hideturtle()
turtle.color("red")
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
draw_cube(100)
turtle.done()
```
运行程序后,会在窗口中绘制一个红色的立方体。你可以尝试修改参数 `size` 来调整立方体的大小。
阅读全文