实现一个红绿灯效果,每次红灯亮10秒,黄灯亮3秒,绿灯亮8秒,并在红绿灯右侧显示倒计时。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>交通灯计时器</title>
    </head>
    <body>
        <style>
            /* 交通灯圆圈的样式 */
            span {
                display: inline-block;
                width: 80px;
                height: 80px;
                border-radius: 50%;
                border: 1px solid white;
                opacity: 0.1;
            }

            /* 高亮显示当前亮起的灯 */
            .light{
                opacity: 1;
            }

            /* 红灯的样式 */
            #red{
                background-color: red;
            }

            /* 黄灯的样式 */
            #yellow{
                background-color: yellow;
            }

            /* 绿灯的样式 */
            #green{
                background-color: green;
            }

            /* 计时器显示的样式 */
            #time{
                margin-left: 30px;
                font-size: 70px;
            }
        </style>

        <!-- 交通灯和计时器元素 -->
        <p>
            <span id="red" class="light"></span>
            <span id="yellow"></span>
            <span id="green"></span>
            <b id="time">10</b>
        </p>

        <script>
            // 初始化当前灯和计时器的变量
            var current = 'red';
            var time = 10;

            // 切换亮起的灯并重置计时器的函数
            function changeLight(from, to, timeout){
                // 检查计时器是否在运行
                if (time > 0) {
                    return;
                }

                // 更新当前灯并重置计时器
                current = to;
                time = timeout;

                // 更新样式以反映亮起的灯的变化
                document.getElementById(from).removeAttribute('class', 'light');
                document.getElementById(to).setAttribute('class', 'light');
            }

            // 设置一个定时器来更新交通灯和计时器的显示
            setInterval(function(){
                // 减少计时器的计数
                time--;

                // 检查当前灯并在计时器达到0时切换到下一个灯
                if (current === 'red') {
                    changeLight('red', 'yellow', 3);
                } else if (current === 'yellow') {
                    changeLight('yellow', 'green', 8);
                } else if (current === 'green'){
                    changeLight('green', 'red', 10);
                }

                // 更新计时器显示
                document.getElementById('time').innerText = time;
            }, 1000);
        </script>
    </body>
</html>

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个任务可以通过 Python 中的 Tkinter 模块来实现,下面是一个简单的实现: ```python import tkinter as tk class TrafficLight: def __init__(self, master): self.master = master self.master.title("Traffic Light") self.master.geometry('300x200') self.color = 'red' self.time_left = 10 self.timer_id = None self.create_widgets() self.change_color() def create_widgets(self): self.canvas = tk.Canvas(self.master, width=100, height=250) self.canvas.pack(side=tk.LEFT, padx=20) self.red_light = self.canvas.create_oval(20, 20, 80, 80, fill='red') self.yellow_light = self.canvas.create_oval(20, 100, 80, 160, fill='gray') self.green_light = self.canvas.create_oval(20, 180, 80, 240, fill='gray') self.countdown_label = tk.Label(self.master, text='', font=('Helvetica', 24, 'bold')) self.countdown_label.pack(side=tk.RIGHT, padx=20) def change_color(self): if self.color == 'red': self.canvas.itemconfig(self.red_light, fill='red') self.canvas.itemconfig(self.yellow_light, fill='gray') self.canvas.itemconfig(self.green_light, fill='gray') self.time_left = 10 self.countdown_label.config(fg='red', text=f'{self.time_left}') elif self.color == 'yellow': self.canvas.itemconfig(self.red_light, fill='gray') self.canvas.itemconfig(self.yellow_light, fill='yellow') self.canvas.itemconfig(self.green_light, fill='gray') self.time_left = 3 self.countdown_label.config(fg='black', text=f'{self.time_left}') elif self.color == 'green': self.canvas.itemconfig(self.red_light, fill='gray') self.canvas.itemconfig(self.yellow_light, fill='gray') self.canvas.itemconfig(self.green_light, fill='green') self.time_left = 8 self.countdown_label.config(fg='green', text=f'{self.time_left}') self.timer_id = self.master.after(1000, self.countdown) self.color = {'red': 'yellow', 'yellow': 'green', 'green': 'red'}[self.color] def countdown(self): self.time_left -= 1 self.countdown_label.config(text=f'{self.time_left}') if self.time_left == 0: self.master.after_cancel(self.timer_id) self.change_color() root = tk.Tk() tl = TrafficLight(root) root.mainloop() ``` 这个程序创建了一个 `TrafficLight` 类,它继承了 `tkinter.Frame` 类,并在其中创建了三个圆形 `Canvas` 元素,表示红灯黄灯绿灯。通过不断地改变这三个元素的颜色,来模拟红绿灯的变化。 程序还创建了一个 `countdown` 方法,用于显示红绿灯倒计时,并在倒计时结束后调用 `change_color` 方法,来切换红绿灯的颜色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值