程序代码——红绿灯系列
一、用不同的turtle库导入模式绘制五角星
import turtle
import turtle as p02
from turtle import *
def wjx01©:
p01=turtle.Turtle()
p01.pensize(3)
p01.pencolor("#996633")
p01.up()
p01.goto(-200,0)
p01.down()
for i in range(5):
p01.fd(c)
p01.rt(144)
def wjx02©:
p02.pensize(3)
p02.pencolor("#669933")
p02.up()
p02.goto(0,0)
p02.down()
for i in range(5):
p02.fd(c)
p02.rt(144)
def wjx03©:
pensize(3)
pencolor("#663399")
up()
goto(200,0)
down()
for i in range(5):
fd(c)
rt(144)
def main():
wjx01(100)
wjx02(150)
wjx03(200)
if name==“main”:
main()
视频讲解:https://haokan.baidu.com/v?vid=9300960371709141077
二、两种导入turtle库模式绘制圆弧的差别
import turtle
import turtle as p
def h(e):
#定义六只画笔
p01=turtle.Turtle()
p02=turtle.Turtle()
p03=turtle.Turtle()
p04=turtle.Turtle()
p05=turtle.Turtle()
p06=turtle.Turtle()
'''
p01=p
p02=p
p03=p
p04=p
p05=p
p06=p
'''
#设置画笔一的粗细与颜色
p01.pensize(3)
p01.pencolor("#cc2222")
#画笔之间角度位60
p01.rt(60*0)
p02.rt(60*1)
p03.rt(60*2)
p04.rt(60*3)
p05.rt(60*4)
p06.rt(60*5)
#用六只画笔绘制弧线
for i in range(1,6):
p01.circle(e*i,e)
p02.circle(e*i,e)
p03.circle(e*i,e)
p04.circle(e*i,e)
p05.circle(e*i,e)
p06.circle(e*i,e)
def main():
h(70)
if name==“main”:
main()
视频讲解:https://haokan.baidu.com/v?vid=10333390783012956712
三、红绿灯程序
‘’’
红绿灯程序 版本1.0
‘’’
import turtle
import time
#红绿灯变亮
def hld_d(ys,bj,light):
light.color(ys,ys)
light.begin_fill()
light.circle(bj)
light.end_fill()
#时序函数
def hld_sx(x,y,bj,sj):
light=turtle.Turtle()
light.hideturtle()
light.screen.delay(0)
light.up()
light.goto(x,y)
light.down()
ys=""
while True:
ys=(ys=="red") and "green" or "red"
hld_d(ys,bj,light)
time.sleep(sj)
def main():
p_x=-200
p_y=200
p_bj=35
p_sj=1
hld_sx(p_x,p_y,p_bj,p_sj)
if name==“main”:
main()
视频讲解:https://haokan.baidu.com/v?vid=13849329374516502460
四、带数字显示的红绿灯程序
‘’’
红绿灯程序 版本2.0
带数字显示的红绿灯程序
‘’’
import turtle
import time
#倒计时函数
def hld_sz(n,ys,bj,light):
for i in range(n,0,-1):
light.pencolor(“yellow”)
light.write(i,align=“center”,font=(“黑体”,50,“bold”))
time.sleep(1)
hld_d(ys,bj,light)
#红绿灯变亮
def hld_d(ys,bj,light):
light.color(ys,ys)
light.begin_fill()
light.circle(bj)
light.end_fill()
#时序函数
def hld_sx(x,y,bj,sj,ts):
light=turtle.Turtle()
light.hideturtle()
light.screen.delay(0)
light.up()
light.goto(x,y)
light.down()
ys=""
while True:
ys=(ys=="red") and "green" or "red"
hld_d(ys,bj,light)
time.sleep(sj-ts)
hld_sz(ts,ys,bj,light)
def main():
p_x=-200
p_y=200
p_bj=35
p_sj=10
p_ts=8
try:
hld_sx(p_x,p_y,p_bj,p_sj,p_ts)
except Exception as e:
print(e)
if name==“main”:
main()
视频讲解:https://haokan.baidu.com/v?vid=9517312250675085069