⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ucar.pas

📁 智能交通系统演示delphi源码,适合初学者看的例子
💻 PAS
字号:
unit uCar;

interface

uses
  SysUtils, Classes, Controls, ExtCtrls, uLight, uPort, Dialogs, Types;

type
  TCarDirection = (cdLeft, cdRight, cdDown, cdUp);
  TCarState = (csStop, csForward, csLeftTurn, csRightTurn);
  TCar = class(TImage)
  private
    { Private declarations }
    FDirection: TCarDirection;
    FState: TCarState;
    FHasAckInsected: boolean;
    FHasAckLeave: boolean;
  protected
    { Protected declarations }
  public
    { Public declarations }
    procedure GoForward;
    procedure TurnRight;
    procedure TurnLeft;
    procedure Move;
    procedure Stop;
    function HasInsected(ALight: TLight): boolean;
    function IsToPort(APort: TPort): boolean;
  published
    { Published declarations }
    property Direction: TCarDirection read FDirection write FDirection;
    property State: TCarState read FState;
    property HasAckInsected: boolean read FHasAckInsected write FHasAckInsected;
    property HasAckLeave: boolean read FHasAckLeave write FHasAckLeave;
  end;

procedure Register;

implementation
uses GFunc;

const
  CARSPEED = 2;
  COMINSTANCE = 1; { ODO : change }

procedure TCar.GoForward;
begin
  FState := csForward;
end;

procedure TCar.TurnRight;
begin
  //ShowMessage('right'); { TODO : add }
  FState := csRightTurn;
  if Direction = cdLeft then
    Direction := cdUp
  else if Direction = cdRight then
    Direction := cdDown
  else if Direction = cdUp then
    Direction := cdRight
  else if Direction = cdDown then
    Direction := cdLeft;

end;

procedure TCar.TurnLeft;
begin
  //ShowMessage('left');{ TODO : add }
  FState := csLeftTurn;
  if Direction = cdLeft then
    Direction := cdDown
  else if Direction = cdRight then
    Direction := cdUp
  else if Direction = cdUp then
    Direction := cdLeft
  else if Direction = cdDown then
    Direction := cdRight
end;

procedure TCar.Move;
begin
  if State = csForward then
    if Direction = cdLeft then
      Left := Left - CARSPEED
      //Dec(Left, CARSPEED)
    else if Direction = cdRight then
      Left := Left + CARSPEED
      //Inc(Left, CARSPEED)
    else if Direction = cdUp then
      Top := Top - CARSPEED
      //Dec(Top, CARSPEED)
    else if Direction = cdDown then
      Top := Top + CARSPEED
      //Inc(Top, CARSPEED)
  else if State = csLeftTurn then
    if Direction = cdLeft then
    begin
      //Dec(Left, CARSPEED);
      //Inc(Top, CARSPEED);
      Left := Left - CARSPEED;
      Top := Top + CARSPEED;
    end
    else if Direction = cdRight then
    begin
      Left := Left + CARSPEED;
      Top := Top - CARSPEED;
      //Inc(Left, CARSPEED);
      //Dec(Top, CARSPEED);
    end
    else if Direction = cdUp then
    begin
      Left := Left - CARSPEED;
      Top := Top - CARSPEED;
      //Dec(Left, CARSPEED);
      //Dec(Top, CARSPEED);
    end
    else if Direction = cdDown then
    begin
      Left := Left + CARSPEED;
      Top := Top + CARSPEED;
      //Inc(Left, CARSPEED);
      //Inc(Top, CARSPEED);
    end
  else if State = csRightTurn then
    if Direction = cdLeft then
      Top := Top - CARSPEED
      //Dec(Top, CARSPEED)
    else if Direction = cdRight then
      Top := Top + CARSPEED
      //Inc(Top, CARSPEED)
    else if Direction = cdUp then
      Left := Left + CARSPEED
      //Inc(Left, CARSPEED)
    else if Direction = cdDown then
      Left := Left - CARSPEED;
      //Dec(Left, CARSPEED)
end;

procedure TCar.Stop;
begin

end;

function TCar.HasInsected(ALight: TLight): boolean;
begin
  {result := false;
  if ((Direction=cdLeft) or (Direction=cdRight)) and
     IsInRect(BoundsRect, ALight.BoundsRect) then
    result := true
  else if IsInRect(BoundsRect, ALight.BoundsRect) then
    result := true;    }
    
  result := IsInRect(BoundsRect, ALight.BoundsRect);
end;

function TCar.IsToPort(APort: TPort): boolean;
begin
  result := false;

  if (((Direction=cdLeft) and (APort.Direction=pdLeft)) or
    ((Direction=cdRight) and (APort.Direction=pdRight)))  and
     IsInRect(BoundsRect, APort.BoundsRect) then
    result := true
  else if (((Direction=cdUp) and (APort.Direction=pdUp))
    or ((Direction= cdDown) and (APort.Direction=pdDown))) and
    IsInRect(BoundsRect, APort.BoundsRect) then
    result := true;      
end;


procedure Register;
begin
  RegisterComponents('Transport', [TCar]);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -