]
},
{
"cell_type": "markdown",
"id": "8778c0a1",
"metadata": {},
"source": [
"## 0.导入库模块"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f20eb002",
"metadata": {},
"outputs": [],
"source": [
"#import turtle #导入turtle函图库,其方法前面一定要添加库名turtle\n",
"from turtle import * #导入turtle库,其方法前面一定不能添加库名\n",
"from random import * #导入random库,其方法前面一定不能添加库名\n",
"import time #导入time库,其方法前面一定要添加库名time"
]
},
{
"cell_type": "markdown",
"id": "461adf10",
"metadata": {},
"source": [
"## 1.绘制单端数码管的间隙"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "80436157",
"metadata": {},
"outputs": [],
"source": [
"def drawGap():\n",
" penup() #设置画笔为飞行模式\n",
" fd(5) #画笔前进5像素\n",
" "
]
},
{
"cell_type": "markdown",
"id": "a9ec82f0",
"metadata": {},
"source": [
"## 2.绘制单端数码管"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "78f10980",
"metadata": {},
"outputs": [],
"source": [
"def drawLine(draw):\n",
" drawGap() #调用间隙函数(画间隙)\n",
" if draw:\n",
" pendown() #设置如果函数参数draw为真时画笔为工作模式\n",
" else:\n",
" penup() #否则画笔仍未飞行模式\n",
" fd(20) #画笔向前行进20像素\n",
" drawGap() #第2次调用间隙函数\n",
" right(90) #将画笔向下(前进向右)旋转90度\n",
" "
]
},
{
"cell_type": "markdown",
"id": "b7628b13",
"metadata": {},
"source": [
"## 3.绘制单个数字数码管"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9922911e",
"metadata": {},
"outputs": [],
"source": [
"def drawDigit(digit):\n",
" #画第1段数码管\n",
" pencolor(random(),random(),random()) #用随机函数设置画笔的颜色\n",
" drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False) #用if...else drawLine(False)\n",
" #用if...else表达式画第1段数码管\\\n",
" #画第2段数码管\n",
" pencolor(random(),random(),random()) #用随机函数设置画笔的颜色\n",
" drawLine(True) if digit in [2] else drawLine(False)\n",
" \n",
" #画第3段数码管\n",
" pencolor(random(),random(),random()) #用随机函数设置画笔的颜色\n",
" drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)\n",
" \n",
" #画第4段数码管\n",
" pencolor(random(),random(),random()) #用随机函数设置画笔的颜色\n",
" drawLine(True) if digit in [0,2,4,5,6,8,9] else drawLine(False)\n",
" \n",
" left(90) #将画笔向上(前进向左)旋转90度\n",
" #画第5段数码管\n",
" pencolor(random(),random(),random()) #用随机函数设置画笔的颜色\n",
" drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)\n",
" \n",
" #画第6段数码管\n",
" pencolor(random(),random(),random()) #用随机函数设置画笔的颜色\n",
" drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)\n",
" \n",
" #画第7段数码管\n",
" pencolor(random(),random(),random()) #用随机函数设置画笔的颜色\n",
" drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)\n",
" \n",
" left(180) #将画笔反向(旋转180度)\n",
" penup() #将画笔设置飞行模式\n",
" fd(20) #将画笔前进20像素 "
]
},
{
"cell_type": "markdown",
"id": "f2e4b0af",
"metadata": {},
"source": [
案例分析:绘制七段电子数码管时间
最新推荐文章于 2025-07-22 12:55:31 发布