红绿灯倒计时

<script type="text/javascript">
	var lamp={
		red: {
			obj: document.getElementById('red'),
			timeout:30,
			style:['red','gray','gray'],
			next:'green'
		},
		yellow: {
			obj: document.getElementById('yellow'),
			timeout:5,
			style:['gray','yellow','gray'],
			next:'red'
		},
		green: {
			obj: document.getElementById('green'),
			timeout:35,
			style:['gray','grat','green'],
			next:'yellow'
		},
		changeStyle(style){
			this.red.obj.className=style[0];
			this.yellow.obj.className=style[1];
			this.green.obj.className=style[2];
		}
	};

	var count={
		obj: document.getElementById('count'),
		change:function(num){
			this.obj.innerHTML=(num<10)?('0'+num):num;
		}
	};

	var now=lamp.green;
	var timeout=now.timeout;
	lamp.changeStyle(now.style);
	count.change(timeout);

	setInterval(function() {
		if(--timeout<=0){
			now=lamp[now.next];
			timeout=now.timeout;
			lamp.changeStyle(now.style);
		}
		count.change(timeout);
	},1000);
</script>
以下是一个红绿灯倒计时的Python程序: ```python import turtle import time # 红绿灯变亮 def hld_d(ys, bj, light): light.color(ys, ys) light.begin_fill() light.circle(bj) light.end_fill() # 红绿灯倒计时 def countdown(t): for i in range(t, 0, -1): print(i) time.sleep(1) # 初始化窗口和画笔 win = turtle.Screen() win.title("红绿灯倒计时") win.bgcolor("black") pen = turtle.Turtle() pen.hideturtle() pen.speed(0) # 画红灯 pen.penup() pen.goto(-50, 50) pen.pendown() hld_d("red", 50, pen) # 画黄灯 pen.penup() pen.goto(-50, 0) pen.pendown() ld_d("yellow", 50, pen) # 画绿灯 pen.penup() pen.goto(-50, -50) pen.pendown() hld_d("green", 50, pen) # 红绿灯倒计时 while True: pen.clear() pen.write("3", align="center", font=("Courier", 80, "normal")) countdown(3) pen.clear() hld_d("red", 50, pen) pen.write("2", align="center", font=("Courier", 80, "normal")) countdown(2) pen.clear() hld_d("yellow", 50, pen) pen.write("1", align="center", font=("Courier", 80, "normal")) countdown(1) pen.clear() hld_d("green", 50, pen) pen.write("GO!", align="center", font=("Courier", 80, "normal")) time.sleep(2) turtle.done() ``` 这个程序使用了turtle库来绘制红绿灯,同时使用了time库来实现倒计时功能。程序中定义了两个函数,hld_d用于绘制红绿灯,countdown用于实现倒计时。在主程序中,先绘制了红绿灯,然后进入一个无限循环,每次循环都会清除画布,显示倒计时数字,然后调用countdown函数实现倒计时,最后显示红绿灯的颜色和GO!字样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值