30【WinForm项目】C#模拟交通信号灯|WinForm交通红绿灯

简介: 交通信号灯十字路口通行原理图。

前言

交通信号灯十字路口通行原理图。

实际上归类为四大类:

  • 南北双向直行
  • 东西双向直行
  • 一对黑色粗线的左转
  • 一对红色粗线的左转。

在这里插入图片描述
状态逻辑图如下图所示:

  • 假定通行顺序为:南北直行、南北左转、东西直行、东西左转。
  • 南北行人只在南北车流绿灯和黄灯的时候可以通行,东西人行同理。
  • 假设绿灯25s,黄灯5s,这二者决定红灯的时间。
  • 四大类方向30s切换一次,总共循环为120s。
  • 南北、东西行人通行时间一次循环内分别为30s。

在这里插入图片描述

一、运行效果

  • 点击开始:

进入120s的整个循环中,第一部分南北直行绿灯在25s后变为黄灯持续5s,然后为红灯;第二部分南北左转灯对应变为绿灯并进行倒计时,然后黄灯,红灯;第三部分就是东西直行的绿灯黄灯红灯,第四部分是东西左转的绿黄红切换。

  • 南北向、东西向行人在对应的时间切换绿灯和红灯。
  • 停止按钮,停止计数器的计时。
  • 思路:1秒的定时器计算时间,到了对应的时间阶段,触发界面的状态变化和倒计时。详情见代码。

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、界面设计

控件和命名图片中标出了同系列的第一个,其余的根据方向类推命名。

在这里插入图片描述

三、代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace TrafficLight
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int time = 0;//计时器
        const int MAX_LOOP_TIME = 120;//120s时间为一次循环

        //以下时间段分别为:南北直行绿灯25,南北直行黄灯5,南北左转绿灯25,南北左转黄灯5,东西直行绿灯25,东西直行黄灯5,东西左转绿灯25,东西左转黄灯5
        int[] PassTime={25, 30, 55, 60, 85, 90, 115, 120};
        
        //事件委托的方式触发
        public delegate void delegateTrafficLight(int tm);
        public event delegateTrafficLight EventTrafficLight;


        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Start();

            //Thread threadLightChg = new Thread(LightChange);
            //threadLightChg.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //计时开始,进入循环,120s为一次循环
            if (time < MAX_LOOP_TIME - 1)  //119秒后置为0秒
            {
                time += 1;
                if (EventTrafficLight != null)
                {
                    EventTrafficLight(time);
                }
            }
            else
            {
                time = 0;
                if (EventTrafficLight != null)
                {
                    EventTrafficLight(time);
                }

                txtStoN.Clear();
                txtNtoS.Clear();
                txtWtoE.Clear();
                txtEtoW.Clear();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.EventTrafficLight += new delegateTrafficLight(LightChange);
        }

        public void LightChange(int time)
        {
            if (time < PassTime[0])
            {
                lbStoNStraight.BackColor = Color.Green;
                lbStoNLeft.BackColor = Color.Red;

                lbNtoSStraight.BackColor = Color.Green;
                lbNtoSLeft.BackColor = Color.Red;

                lbWtoEStraight.BackColor = Color.Red;
                lbWtoELeft.BackColor = Color.Red;

                lbEtoWStraight.BackColor = Color.Red;
                lbEtoWLeft.BackColor = Color.Red;

                lbPeopleNS1.BackColor = Color.Green;
                lbPeopleNS2.BackColor = Color.Green;
                lbPeopleNS3.BackColor = Color.Green;
                lbPeopleNS4.BackColor = Color.Green;

                lbPeopleWE1.BackColor = Color.Red;
                lbPeopleWE2.BackColor = Color.Red;
                lbPeopleWE3.BackColor = Color.Red;
                lbPeopleWE4.BackColor = Color.Red;

                //倒计时
                txtStoN.Text = (PassTime[0] - time).ToString();
                txtNtoS.Text = (PassTime[0] - time).ToString();

            }
            else if (time >= PassTime[0] && time < PassTime[1])
            {
                lbStoNStraight.BackColor = Color.Yellow;
                lbStoNLeft.BackColor = Color.Red;

                lbNtoSStraight.BackColor = Color.Yellow;
                lbNtoSLeft.BackColor = Color.Red;

                lbWtoEStraight.BackColor = Color.Red;
                lbWtoELeft.BackColor = Color.Red;

                lbEtoWStraight.BackColor = Color.Red;
                lbEtoWLeft.BackColor = Color.Red;

                lbPeopleNS1.BackColor = Color.Green;
                lbPeopleNS2.BackColor = Color.Green;
                lbPeopleNS3.BackColor = Color.Green;
                lbPeopleNS4.BackColor = Color.Green;

                lbPeopleWE1.BackColor = Color.Red;
                lbPeopleWE2.BackColor = Color.Red;
                lbPeopleWE3.BackColor = Color.Red;
                lbPeopleWE4.BackColor = Color.Red;

                //倒计时
                txtStoN.Text = (PassTime[1] - time - 1).ToString();
                txtNtoS.Text = (PassTime[1] - time - 1).ToString();
            }
            else if (time >= PassTime[1] && time < PassTime[2])
            {
                lbStoNStraight.BackColor = Color.Red;
                lbStoNLeft.BackColor = Color.Green;

                lbNtoSStraight.BackColor = Color.Red;
                lbNtoSLeft.BackColor = Color.Green;

                lbWtoEStraight.BackColor = Color.Red;
                lbWtoELeft.BackColor = Color.Red;

                lbEtoWStraight.BackColor = Color.Red;
                lbEtoWLeft.BackColor = Color.Red;

                lbPeopleNS1.BackColor = Color.Red;
                lbPeopleNS2.BackColor = Color.Red;
                lbPeopleNS3.BackColor = Color.Red;
                lbPeopleNS4.BackColor = Color.Red;

                lbPeopleWE1.BackColor = Color.Red;
                lbPeopleWE2.BackColor = Color.Red;
                lbPeopleWE3.BackColor = Color.Red;
                lbPeopleWE4.BackColor = Color.Red;

                //倒计时
                txtStoN.Text = (PassTime[2] - time - 1).ToString();
                txtNtoS.Text = (PassTime[2] - time - 1).ToString();
            }
            else if (time >= PassTime[2] && time < PassTime[3])
            {
                lbStoNStraight.BackColor = Color.Red;
                lbStoNLeft.BackColor = Color.Yellow;

                lbNtoSStraight.BackColor = Color.Red;
                lbNtoSLeft.BackColor = Color.Yellow;

                lbWtoEStraight.BackColor = Color.Red;
                lbWtoELeft.BackColor = Color.Red;

                lbEtoWStraight.BackColor = Color.Red;
                lbEtoWLeft.BackColor = Color.Red;

                lbPeopleNS1.BackColor = Color.Red;
                lbPeopleNS2.BackColor = Color.Red;
                lbPeopleNS3.BackColor = Color.Red;
                lbPeopleNS4.BackColor = Color.Red;

                lbPeopleWE1.BackColor = Color.Red;
                lbPeopleWE2.BackColor = Color.Red;
                lbPeopleWE3.BackColor = Color.Red;
                lbPeopleWE4.BackColor = Color.Red;

                //倒计时
                txtStoN.Text = (PassTime[3] - time - 1).ToString();
                txtNtoS.Text = (PassTime[3] - time - 1).ToString();
            }
            else if (time >= PassTime[3] && time < PassTime[4])
            {
                lbStoNStraight.BackColor = Color.Red;
                lbStoNLeft.BackColor = Color.Red;

                lbNtoSStraight.BackColor = Color.Red;
                lbNtoSLeft.BackColor = Color.Red;

                lbWtoEStraight.BackColor = Color.Green;
                lbWtoELeft.BackColor = Color.Red;

                lbEtoWStraight.BackColor = Color.Green;
                lbEtoWLeft.BackColor = Color.Red;

                lbPeopleNS1.BackColor = Color.Red;
                lbPeopleNS2.BackColor = Color.Red;
                lbPeopleNS3.BackColor = Color.Red;
                lbPeopleNS4.BackColor = Color.Red;

                lbPeopleWE1.BackColor = Color.Green;
                lbPeopleWE2.BackColor = Color.Green;
                lbPeopleWE3.BackColor = Color.Green;
                lbPeopleWE4.BackColor = Color.Green;

                //倒计时
                txtWtoE.Text = (PassTime[4] - time - 1).ToString();
                txtEtoW.Text = (PassTime[4] - time - 1).ToString();
            }
            else if (time >= PassTime[4] && time < PassTime[5])
            {
                lbStoNStraight.BackColor = Color.Red;
                lbStoNLeft.BackColor = Color.Red;

                lbNtoSStraight.BackColor = Color.Red;
                lbNtoSLeft.BackColor = Color.Red;

                lbWtoEStraight.BackColor = Color.Yellow;
                lbWtoELeft.BackColor = Color.Red;

                lbEtoWStraight.BackColor = Color.Yellow;
                lbEtoWLeft.BackColor = Color.Red;

                lbPeopleNS1.BackColor = Color.Red;
                lbPeopleNS2.BackColor = Color.Red;
                lbPeopleNS3.BackColor = Color.Red;
                lbPeopleNS4.BackColor = Color.Red;

                lbPeopleWE1.BackColor = Color.Green;
                lbPeopleWE2.BackColor = Color.Green;
                lbPeopleWE3.BackColor = Color.Green;
                lbPeopleWE4.BackColor = Color.Green;

                //倒计时
                txtWtoE.Text = (PassTime[5] - time - 1).ToString();
                txtEtoW.Text = (PassTime[5] - time - 1).ToString();
            }
            else if (time >= PassTime[5] && time < PassTime[6])
            {
                lbStoNStraight.BackColor = Color.Red;
                lbStoNLeft.BackColor = Color.Red;

                lbNtoSStraight.BackColor = Color.Red;
                lbNtoSLeft.BackColor = Color.Red;

                lbWtoEStraight.BackColor = Color.Red;
                lbWtoELeft.BackColor = Color.Green;

                lbEtoWStraight.BackColor = Color.Red;
                lbEtoWLeft.BackColor = Color.Green;

                lbPeopleNS1.BackColor = Color.Red;
                lbPeopleNS2.BackColor = Color.Red;
                lbPeopleNS3.BackColor = Color.Red;
                lbPeopleNS4.BackColor = Color.Red;

                lbPeopleWE1.BackColor = Color.Red;
                lbPeopleWE2.BackColor = Color.Red;
                lbPeopleWE3.BackColor = Color.Red;
                lbPeopleWE4.BackColor = Color.Red;

                //倒计时
                txtWtoE.Text = (PassTime[6] - time - 1).ToString();
                txtEtoW.Text = (PassTime[6] - time - 1).ToString();
            }
            else if (time >= PassTime[6])
            {
                lbStoNStraight.BackColor = Color.Red;
                lbStoNLeft.BackColor = Color.Red;

                lbNtoSStraight.BackColor = Color.Red;
                lbNtoSLeft.BackColor = Color.Red;

                lbWtoEStraight.BackColor = Color.Red;
                lbWtoELeft.BackColor = Color.Yellow;

                lbEtoWStraight.BackColor = Color.Red;
                lbEtoWLeft.BackColor = Color.Yellow;

                lbPeopleNS1.BackColor = Color.Red;
                lbPeopleNS2.BackColor = Color.Red;
                lbPeopleNS3.BackColor = Color.Red;
                lbPeopleNS4.BackColor = Color.Red;

                lbPeopleWE1.BackColor = Color.Red;
                lbPeopleWE2.BackColor = Color.Red;
                lbPeopleWE3.BackColor = Color.Red;
                lbPeopleWE4.BackColor = Color.Red;

                //倒计时
                txtWtoE.Text = (MAX_LOOP_TIME - time - 1).ToString();
                txtEtoW.Text = (MAX_LOOP_TIME - time - 1).ToString();
            }
        }
    }

   
}

总结

重在理解交通信号灯的原理。

代码重点:

  • 使用了定时器
  • 使用了委托事件的触发方式
  • 对常量和公共数组的应用
  • 变量命名规范、代码整洁
目录
相关文章
|
2月前
|
传感器 算法 物联网
智能停车解决方案之停车场室内导航系统(二):核心技术与系统架构构建
随着城市化进程的加速,停车难问题日益凸显。本文深入剖析智能停车系统的关键技术,包括停车场电子地图编辑绘制、物联网与传感器技术、大数据与云计算的应用、定位技术及车辆导航路径规划,为读者提供全面的技术解决方案。系统架构分为应用层、业务层、数据层和运行环境,涵盖停车场室内导航、车位占用检测、动态更新、精准导航和路径规划等方面。
179 4
|
7月前
|
存储 算法 编译器
课程设计——基于FPGA的交通红绿灯控制系统(源代码)
课程设计——基于FPGA的交通红绿灯控制系统(源代码)
|
8月前
LabVIEW软件模拟氢燃料电池在车辆中的应用
LabVIEW软件模拟氢燃料电池在车辆中的应用
55 4
|
8月前
|
数据采集 传感器 存储
LabVIEW开发航空航天器风洞的数据采集系统
LabVIEW开发航空航天器风洞的数据采集系统
54 0
|
8月前
|
机器学习/深度学习 运维 数据可视化
LabVIEW开发汽车发动机故障模拟器
LabVIEW开发汽车发动机故障模拟器
43 0
|
8月前
|
监控 安全 Java
采用UWB技术开发的智慧工厂人员定位系统源码【UWB定位基站、卡牌】
采用UWB技术开发的智慧工厂人员定位系统源码【UWB定位基站、卡牌】
161 0
|
安全 自动驾驶 Linux
聊聊身边的嵌入式,交通信号灯
聊聊身边的嵌入式,交通信号灯
|
安全 JavaScript 前端开发
智慧工厂人员定位系统源码,采用UWB高精度定位技术开发
智慧工厂人员定位主要采用UWB高精度定位技术,通过在厂区内布设有限数量定位基站,厂区室内设备上放置好定位标签,实时精确地定位厂区设备标签位置,从而清晰显示所有设备的具体位置信息。方便管理人员进行安全区域管控、资产精细化管理。精度达到10厘米级,精确管控以精益生产,通过合理调度安排,从而提高企业的管理水平和生产效率。
170 0
|
传感器 移动开发 监控
红绿灯设计 | 学习笔记
快速学习红绿灯设计
276 0
|
监控 定位技术 安全
Qt编写安防视频监控系统12-异形布局
一、前言 视频监控系统中,除了常规的1画面、4画面、9画面、16画面以外,还有几个布局比较另类,比如6画面、8画面、13画面,有些通道需要占据不同的行列,4画面、9画面、16画面都是非常对称的布局,行数和列数都一样,而且每个通道占据的面积都一样,初学者都知道怎么处理。
1367 0

热门文章

最新文章