一、按键监测程序代码:
import turtle
import time
#绘制圆,等待2秒,自动清除
def cs():
t.fd(200)
t.circle(50)
time.sleep(2)
#t.clear()
t.reset()
#入口程序代码
def main():
#定义全局变量
global t
t=turtle.Turtle()
#键盘输入监测
t.screen.onkey(cs,“space”)
t.screen.listen()
if name==“main”:
main()
二、自助红绿灯程序代码:
‘’’
红绿灯程序 版本3.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,“normal”))
time.sleep(1)
#light.pencolor(ys)
#light.write(i,align=“center”,font=(“黑体”,50,“bold”))
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,ys):
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)
hld_d("red",bj,light)
#自助红绿灯时序调用函数
def hld_zz():
lb=[[-200,200,35,5,5,"red"],
[-200,200,35,7,5,"green"]]
hld_sx(lb[0][0],lb[0][1],lb[0][2],lb[0][3],lb[0][4],lb[0][5])
hld_sx(lb[1][0],lb[1][1],lb[1][2],lb[1][3],lb[1][4],lb[1][5])
def main():
p_bj=35 #红绿灯的半径
p_sj=0 #红绿灯显示时长
p_ts=0 #红绿灯提示时长
try:
p_x=-200 #红绿灯位置x坐标
p_y=200 #红绿灯位置y坐标
p_ys="red"
hld_sx(p_x,p_y,p_bj,p_sj,p_ts,p_ys)
except Exception as e:
print(e)
f=turtle.Turtle()
f.hideturtle()
f.screen.onkey(hld_zz,"space")
f.screen.listen()
if name==“main”:
main()