turtle·库数码管显示当前日期且要求每个数字颜色不一样
时间: 2024-02-12 10:09:26 浏览: 73
好的,使用turtle库可以绘制出数码管显示当前日期,并且设置每个数字的颜色不一样。
以下是代码实现:
```python
import turtle
import datetime
# 定义数字0-9的笔画
pens = [[(0, 0), (0, 100), (60, 100), (60, 0), (0, 0)], # 0
[(30, 0), (30, 100)], # 1
[(0, 0), (60, 0), (60, 50), (0, 50), (0, 100), (60, 100)], # 2
[(0, 0), (60, 0), (60, 50), (0, 50), (60, 50), (60, 100), (0, 100)], # 3
[(0, 0), (0, 50), (60, 50), (60, 0), (60, 100)], # 4
[(60, 0), (0, 0), (0, 50), (60, 50), (60, 100), (0, 100)], # 5
[(60, 0), (0, 0), (0, 100), (60, 100), (60, 50), (0, 50)], # 6
[(0, 0), (60, 0), (60, 100)], # 7
[(0, 0), (60, 0), (60, 100), (0, 100), (0, 0), (60, 50)]] # 8
# 定义数字颜色
colors = ['red', 'dark orange', 'gold', 'green', 'blue', 'purple', 'deep pink']
# 获取当前日期
today = datetime.datetime.now().strftime('%Y-%m-%d')
# 创建画布
turtle.setup(800, 200)
turtle.penup()
turtle.goto(-350, 0)
# 绘制每个数字
for i, num in enumerate(today):
if num == '-':
turtle.write('-', font=('Arial', 36, 'bold'))
turtle.fd(40)
continue
turtle.pendown()
turtle.pencolor(colors[i % len(colors)])
for j in range(len(pens[int(num)])):
x, y = pens[int(num)][j]
turtle.goto(x + i * 80, y)
turtle.penup()
turtle.done()
```
运行代码后,可以看到绘制出来的数码管显示当前日期,每个数字的颜色都不一样。
阅读全文