在c#中画三个圆分别代表红绿灯,然后间隔一定时间三个灯色轮流换,(换句话说模拟信号灯,该怎么编写代码
答案:1 悬赏:50 手机版
解决时间 2021-11-28 04:23
- 提问者网友:椧運幽默
- 2021-11-27 17:51
在c#中画三个圆分别代表红绿灯,然后间隔一定时间三个灯色轮流换,(换句话说模拟信号灯,该怎么编写代码
最佳答案
- 五星知识达人网友:时间的尘埃
- 2021-11-27 18:28
Graphics g1 = this.CreateGraphics();
var Rec1 = new Rectangle(new Point(30, 30), new Size(150, 150));
int time = 0, before = 0, flag = 0;
System.Timers.Timer t = new System.Timers.Timer(1000);
t.Elapsed += (s, e1) =>
{
int left = time - before;
switch (flag)
{
case 0:
if (left == 15)//red turn green,10s
{
flag = 2;
before = time;
}
g1.FillEllipse(Brushes.Red, Rec1);
break;
case 1:
if (left == 3)//yellow turn red,3s
{
flag = 0;
before = time;
}
g1.FillEllipse(Brushes.Yellow, Rec1);
break;
case 2:
if (left == 10)//green turn yellow,10s
{
flag = 1;
before = time;
}
g1.FillEllipse(Brushes.Green, Rec1);
break;
default:
break;
}
time++;
button1.BeginInvoke(new Action(() =>
{
button1.Text = time.ToString();
}));
};
t.Start();
var Rec1 = new Rectangle(new Point(30, 30), new Size(150, 150));
int time = 0, before = 0, flag = 0;
System.Timers.Timer t = new System.Timers.Timer(1000);
t.Elapsed += (s, e1) =>
{
int left = time - before;
switch (flag)
{
case 0:
if (left == 15)//red turn green,10s
{
flag = 2;
before = time;
}
g1.FillEllipse(Brushes.Red, Rec1);
break;
case 1:
if (left == 3)//yellow turn red,3s
{
flag = 0;
before = time;
}
g1.FillEllipse(Brushes.Yellow, Rec1);
break;
case 2:
if (left == 10)//green turn yellow,10s
{
flag = 1;
before = time;
}
g1.FillEllipse(Brushes.Green, Rec1);
break;
default:
break;
}
time++;
button1.BeginInvoke(new Action(() =>
{
button1.Text = time.ToString();
}));
};
t.Start();
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯