首页 > 编程语言 >Delphi 经典游戏程序设计40例 的学习 例32 人畜无害的猎点

Delphi 经典游戏程序设计40例 的学习 例32 人畜无害的猎点

时间:2022-10-08 20:11:04浏览次数:54  
标签:Canvas Pi TR2 32 Delphi 猎点 X2 Y2 Pixels

 

 

unit R32;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TRei32 = class(TForm)
    Button1: TButton;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    procedure WhiteDot;
  public
    { Public declarations }
  end;
const
  Cdot = 6;                      //猎捕时的移动单位,点数
  Crad = 9 * Pi / 180;           //猎捕时的方向变更单位,弧度
var
  Rei32: TRei32;
  MakeBmap : TBitmap;
  Col : TColor;
  RectD : TRect;
  St,R1,R2 : Byte;   // R1 白点旋转半径,R2 黄点旋转半径
  CHeight,CWidth : Word;
  X1,Y1,X2,Y2 : Integer;
  TR1,DR1,TR2,DR2,QTR,TX,TY,TDR : Extended; //TR1,TR2 当前弧度,
                                        //DR1,DR2 当前弧度变化量
                             // tx,ty 计算仰角 轴差,TDR 目标所在方向


implementation

{$R *.dfm}

procedure TRei32.FormCreate(Sender: TObject);
begin
  CHeight := Rei32.ClientHeight;  //设置操作区域大小
  CWidth := 570;

  MakeBmap := TBitmap.Create;
  MakeBmap.Width := CWidth;
  MakeBmap.Height := CHeight;
  
end;

procedure TRei32.Timer1Timer(Sender: TObject);
begin
  with Rei32 do
    case St of
      0: begin     //模式0
        RectD := Rect(0,0,CWidth,CHeight);   //刷黑
        Canvas.Brush.Color := clBlack;
        Canvas.FillRect(RectD);
        MakeBmap.Canvas.Brush.Color := clBlack;
        MakeBmap.Canvas.FillRect(RectD);

        X1 := CWidth div 2;     //初始 参数,
        Y1 := 40;
        R1 := 187;
        TR1 := 270 * Pi / 180;          //TR1,TR2 当前弧度
        DR1 := 2 * Pi / 180;             //DR1,DR2 当前弧度变化量
        X2 := CWidth div 2;
        Y2 := 117;
        R2 := 100;
        TR2 := 270 * Pi / 180;
        DR2 := 359 * Pi / 180;
        St := 1;                  //进入模式1
      end;
        //pi的理解,表示一个圆,0~360和其相乘
      1: begin
        WhiteDot;

        Canvas.Pixels[X2,Y2] := clBlack;       //如此可以看到轨迹
        Canvas.Pixels[X2,Y2 + 1] := clBlack;
        Canvas.Pixels[X2 + 1,Y2] := clBlack;
        Canvas.Pixels[X2 + 1,Y2 + 1] := clBlack;
        QTR := TR2;
        TR2 := TR2 + DR2;
        if TR2 >= 2 * Pi then  //原来是这里判断错了,DR2,TR2
          TR2 := TR2 - 2 * Pi;
        X2 := X2 + Round(R2 * (Cos(TR2) - Cos(QTR)));
        Y2 := Y2 + Round(R2 * (Sin(TR2) - Sin(QTR)));
        Canvas.Pixels[X2,Y2] := clYellow;
        Canvas.Pixels[X2,Y2 + 1] := clYellow;
        Canvas.Pixels[X2 + 1,Y2] := clYellow;
        Canvas.Pixels[X2 + 1,Y2 + 1] := clYellow;
      end;

      2: begin             //模式2 ,黄点追捕白点
        WhiteDot;


        TX := X1 - X2;
        TY := Y1 - Y2;
        if TX = 0 then
          TDR := Pi / 2              //TDR 目标所在方向
        else
          TDR := ArcTan(TY / TX);

        if ((TX <= 0) and (TY <= 0)) or ((TX < 0) and (TY > 0)) then
          TDR := TDR + Pi;

        if TDR < 0 then
          TDR := TDR + 2 * Pi;

        TDR := TDR - TR2;

        if TDR < 0 then
          TDR := TDR + 2 * Pi;

        if TDR <> 0 then
        begin
          if TDR <= Pi then        // < 和<= 程序有区别 ?
            TR2 := TR2 + Crad
          else
            TR2 := TR2 + (2 * Pi - Crad);

          if TR2 >= 2 * Pi then
            TR2 := TR2 - 2 * Pi;
        end;

        if X2 < CWidth - 1 then
        begin
          Canvas.Pixels[X2,Y2] := clBlue;   // 显示蓝色轨迹,并在MAKEBMAP中保存
          Canvas.Pixels[X2,Y2 + 1] := clBlue;
          Canvas.Pixels[X2 + 1,Y2] := clBlue;
          Canvas.Pixels[X2 + 1,Y2 + 1] := clBlue;
          MakeBmap.Canvas.Pixels[X2,Y2] := clBlue;
          MakeBmap.Canvas.Pixels[X2,Y2 + 1] := clBlue;
          MakeBmap.Canvas.Pixels[X2 + 1,Y2] := clBlue;
          MakeBmap.Canvas.Pixels[X2 + 1,Y2 + 1] := clBlue;
        end;

        X2 := X2 + Round(Cdot * Cos(TR2));
        Y2 := Y2 + Round(Cdot * Sin(TR2));

        if X2 < CWidth - 1 then
        begin
          Canvas.Pixels[X2,Y2] := clYellow;   // 显示黄色,猎豹点
          Canvas.Pixels[X2,Y2 + 1] := clYellow;
          Canvas.Pixels[X2 + 1,Y2] := clYellow;
          Canvas.Pixels[X2 + 1,Y2 + 1] := clYellow;
        end;

        if (Abs(X1 - X2) <= 3) and (Abs(Y1 - Y2) <= 3)  then
        begin
          St := 3;
          Canvas.Pixels[X1,Y1] := clBlack;       //刷黑  消去
          Canvas.Pixels[X1,Y1 + 1] := clBlack;
          Canvas.Pixels[X1 + 1,Y1] := clBlack;
          Canvas.Pixels[X1 + 1,Y1 + 1] := clBlack;

        end;

      end;

      3: begin
        if Col = clRed then
         Col := clYellow
        else
         Col := clRed;
        Canvas.Pixels[X2,Y2] := Col;
        Canvas.Pixels[X2,Y2 + 1] := Col;
        Canvas.Pixels[X2 + 1,Y2] := Col;
        Canvas.Pixels[X2 + 1,Y2 + 1] := Col;
      end;

    end;
end;

procedure TRei32.WhiteDot;
begin
  with Rei32 do
   Canvas.Pixels[X1,Y1] := clBlack;       //刷黑  消去
   Canvas.Pixels[X1,Y1 + 1] := clBlack;
   Canvas.Pixels[X1 + 1,Y1] := clBlack;
   Canvas.Pixels[X1 + 1,Y1 + 1] := clBlack;
   QTR := TR1;                            //计算刷白 显示
   TR1 := TR1 + DR1;
   if TR1 >= 2 * Pi then
     TR1 := TR1 - 2 * Pi;
   X1 := X1 + Round(R1 * (Cos(TR1) - Cos(QTR)));    //画圆的计算公式
   Y1 := Y1 + Round(R1 * (Sin(TR1) - Sin(QTR)));
   Canvas.Pixels[X1,Y1] := clWhite;
   Canvas.Pixels[X1,Y1 + 1] := clWhite;
   Canvas.Pixels[X1 + 1,Y1] := clWhite;
   Canvas.Pixels[X1 + 1,Y1 + 1] := clWhite;

   if Random(50) = 0 then
     if DR1 = 2 * Pi / 180 then
       DR1 := 358 * Pi / 180
     else
       DR1 := 2 * Pi / 180;

end;

procedure TRei32.Button1Click(Sender: TObject);
begin
  if St = 1 then
    St := 2
  else
    St := 0;
end;

procedure TRei32.FormPaint(Sender: TObject);
begin
  Rei32.Canvas.CopyMode := cmSrcCopy;
  Rei32.Canvas.Draw(0,0,MakeBmap);
end;

procedure TRei32.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MakeBmap.Free;
end;

end.

程序结构和上例一样

还是三角函数的运用

标签:Canvas,Pi,TR2,32,Delphi,猎点,X2,Y2,Pixels
From: https://www.cnblogs.com/D7mir/p/16770066.html

相关文章