Skip to content

Instantly share code, notes, and snippets.

@realmayus
Created May 18, 2020 15:30
Show Gist options
  • Save realmayus/b943cb9312e736875542b602127d43d5 to your computer and use it in GitHub Desktop.
Save realmayus/b943cb9312e736875542b602127d43d5 to your computer and use it in GitHub Desktop.
Informatik Wochenplan: Ampelschaltung
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Timer: TTimer;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure TimerTimer(Sender: TObject);
private
public
end;
var
Form1: TForm1;
currentColor: Integer; { derzeitiger Zustand }
trafficLightsEnabled: Boolean; { ist ampelschaltung derzeit aktiv? }
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
if(trafficLightsEnabled = True) then
begin {Stop the traffic lights}
write('Timer stopped');
Timer.Enabled:=False;
Button1.Caption:='Ampel starten';
trafficLightsEnabled := False;
Shape1.Brush.Color:=clBlack;
Shape2.Brush.Color:=clBlack;
Shape3.Brush.Color:=clBlack;
currentColor := 0;
Timer.Interval:= 500;
end
else
begin {Start traffic lights}
write('Timer started');
Timer.Enabled:=True;
trafficLightsEnabled := True;
Button1.Caption:='Ampel stoppen';
end;
end;
{ Funktion zum Initialisieren der Variablen (wird aufgerufen, wenn das Fenster gezeigt wird) }
procedure TForm1.FormShow(Sender: TObject);
begin
writeln('Initializing Window');
currentColor := 0;
trafficLightsEnabled:=False;
end;
procedure TForm1.TimerTimer(Sender: TObject);
begin
writeln('Beginning new cycle, current state: ' + IntToStr(currentColor));
if(currentColor = 0) then
begin
currentColor := 1;
Shape1.Brush.Color:=clRed;
Shape2.Brush.Color:=clBlack;
Shape3.Brush.Color:=clBlack; {these lights -> off}
Timer.Interval:=12500;
end
else if(currentColor = 1) then
begin
currentColor := 2;
Shape2.Brush.Color:=clYellow;
Timer.Interval:=1500;
end
else if(currentColor = 2) then
begin
Shape1.Brush.Color:=clBlack;
Shape2.Brush.Color:=clBlack; {these lights -> off}
Shape3.Brush.Color:=clLime;
currentColor := 3;
Timer.Interval:=12000;
end
else if(currentColor = 3) then
begin
Shape1.Brush.Color:=clBlack;
Shape2.Brush.Color:=clYellow;
Shape3.Brush.Color:=clBlack;
currentColor := 0;
Timer.Interval:=3000;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment