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();
            }
        }
    }

   
}

总结

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

代码重点:

  • 使用了定时器
  • 使用了委托事件的触发方式
  • 对常量和公共数组的应用
  • 变量命名规范、代码整洁
目录
相关文章
|
5月前
|
存储 算法 编译器
课程设计——基于FPGA的交通红绿灯控制系统(源代码)
课程设计——基于FPGA的交通红绿灯控制系统(源代码)
|
6月前
|
数据采集 传感器 存储
LabVIEW开发航空航天器风洞的数据采集系统
LabVIEW开发航空航天器风洞的数据采集系统
41 0
|
6月前
|
传感器 算法 机器人
植保机器人环境感知地理定位与导航
植保机器人环境感知地理定位与导航
67 4
|
算法 新能源 Python
多旋翼物流无人机节能轨迹规划(Python代码实现)
多旋翼物流无人机节能轨迹规划(Python代码实现)
206 0
|
6月前
|
监控 安全 Java
采用UWB技术开发的智慧工厂人员定位系统源码【UWB定位基站、卡牌】
采用UWB技术开发的智慧工厂人员定位系统源码【UWB定位基站、卡牌】
135 0
|
安全 调度
城市轨道交通供电系统简介及功能实现(Matlab代码实现)
城市轨道交通供电系统简介及功能实现(Matlab代码实现)
115 0
|
监控 安全 Java
采用UWB定位技术开发的智慧工厂人员定位系统源码
技术架构:Java+ vue+ spring boot 系统概述: 智慧工厂人员定位系统源码,采用UWB定位技术,通过在厂区内布设一定数量的定位基站,实时精确地定位员工、车辆、物品上微标签位置,零延时地将人、车、物的位置信息显示在工厂控制中心,进行安全区域管控、人员在岗监控、车辆实时轨迹监控。UWB定位技术精度可达到厘米级,借助摄像头可实现视频联动功能,实时查看工厂内各个员工的实时面面。
126 0
采用UWB定位技术开发的智慧工厂人员定位系统源码
|
安全 JavaScript 前端开发
智慧工厂人员定位系统源码,采用UWB高精度定位技术开发
智慧工厂人员定位主要采用UWB高精度定位技术,通过在厂区内布设有限数量定位基站,厂区室内设备上放置好定位标签,实时精确地定位厂区设备标签位置,从而清晰显示所有设备的具体位置信息。方便管理人员进行安全区域管控、资产精细化管理。精度达到10厘米级,精确管控以精益生产,通过合理调度安排,从而提高企业的管理水平和生产效率。
155 0
|
智慧交通
智慧交通day02-车流量检测实现05:小车匀速案例
智慧交通day02-车流量检测实现05:小车匀速案例
99 0
|
机器学习/深度学习 传感器 开发框架
【控制】车辆自主导航控制系统附matlab代码
【控制】车辆自主导航控制系统附matlab代码