首页 > 代码库 > 红绿灯模拟

红绿灯模拟

  1 unit TrafficLight3;  2   3 interface  4   5 uses  6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  7   Dialogs, StdCtrls, ExtCtrls;  8   9 type 10   TForm1 = class(TForm) 11     Shape1: TShape; 12     Shape2: TShape; 13     Shape3: TShape; 14     Panel1: TPanel; 15     Label1: TLabel; 16     Timer4: TTimer; 17     Label2: TLabel; 18     procedure Timer4Timer(Sender: TObject); 19   private 20     { Private declarations } 21   public 22     { Public declarations } 23   end; 24  25 var 26   Form1: TForm1; 27  28 implementation 29  30 {$R *.dfm} 31 { 32 procedure TForm1.Timer1Timer(Sender: TObject); 33 begin 34   if label1.Caption<>‘1‘ then 35   label1.Caption:=IntToStr(StrToInt(Label1.Caption)-1) 36   else 37   begin 38     Shape1.Brush.Color:=clSilver; 39     Shape3.Brush.Color:=clSilver; 40     Shape2.Brush.Color:=clYellow; 41     Timer2.Enabled:=True; 42     Timer1.Enabled:=False; 43     Timer3.Enabled:=False; 44     Label1.Caption:=‘4‘; 45  46   end; 47 end; 48  49  50 procedure TForm1.Timer2Timer(Sender: TObject); 51 begin 52      if label1.Caption<>‘1‘ then 53   label1.Caption:=IntToStr(StrToInt(Label1.Caption)-1) 54   else 55   begin 56     Shape2.Brush.Color:=clSilver; 57     Shape3.Brush.Color:=clSilver; 58     Shape1.Brush.Color:=clRed; 59     Timer1.Enabled:=True; 60     Timer2.Enabled:=False; 61     Timer3.Enabled:=False; 62     Label1.Caption:=‘5‘; 63  64   end; 65 end; 66  67 procedure TForm1.Timer3Timer(Sender: TObject); 68 begin 69    if label1.Caption<>‘1‘ then 70   label1.Caption:=IntToStr(StrToInt(Label1.Caption)-1) 71   else 72   begin 73     Shape1.Brush.Color:=clSilver; 74     Shape2.Brush.Color:=clSilver; 75     Shape3.Brush.Color:=clGreen; 76     Timer3.Enabled:=True; 77     Timer2.Enabled:=False; 78     Timer1.Enabled:=False; 79     Label1.Caption:=‘6‘; 80  81   end; 82 end; 83  84 } 85  86  87 var 88     n:Integer=0; 89 procedure TForm1.Timer4Timer(Sender: TObject); 90 begin 91   n:=n+1; 92   Label2.Caption:=IntToStr(n); 93   if n<=10  then 94  95  begin 96   97     Shape2.Brush.Color:=clSilver; 98     Shape3.Brush.Color:=clSilver; 99     Shape1.Brush.Color:=clGreen;100    if label1.Caption<>1 then101      label1.Caption:=IntToStr(StrToInt(Label1.Caption)-1)102    else103    begin104   105     Label1.Caption:=5;106    end;107     end;108 109  if (n>5) and (n<=10) then110 111  begin112     if n mod 2=0 then113       begin114       Shape1.Brush.Color:=clGreen;115       Shape3.Brush.Color:=clSilver;116       Shape2.Brush.Color:=clBlack;117     end118    else119    begin120 121 122     Shape1.Brush.Color:=clGreen;123     Shape3.Brush.Color:=clSilver;124     Shape2.Brush.Color:=clYellow;125        end;126    if label1.Caption<>1 then127      label1.Caption:=IntToStr(StrToInt(Label1.Caption)-1)128     else129      begin130        Label1.Caption:=10;131       end;132 133   end;134 135   if (n>10) and (n<=20) then136  begin137     Shape2.Brush.Color:=clSilver;138     Shape1.Brush.Color:=clSilver;139     Shape3.Brush.Color:=clRed;140    if label1.Caption<>1 then141   label1.Caption:=IntToStr(StrToInt(Label1.Caption)-1)142   else143   begin144 145     Label1.Caption:=10;146     n:=0;147   end;148 149  end;150 end;151 152 end.

 

 

红绿灯模拟