Delphi 经典游戏程序设计40例 的学习 例29 残留的轨迹是钟摆线

2022/9/14 14:16:13

本文主要是介绍Delphi 经典游戏程序设计40例 的学习 例29 残留的轨迹是钟摆线,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

unit R29;

interface

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

type
  TRei29 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    ScrollBar1: TScrollBar;
    ScrollBar2: TScrollBar;
    ScrollBar3: TScrollBar;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Image1: TImage;
    Timer1: TTimer;
    Edit3: TEdit;
    ScrollBar4: TScrollBar;
    Edit4: TEdit;
    Label4: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure ScrollBar2Change(Sender: TObject);
    procedure ScrollBar3Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ScrollBar4Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Rei29: TRei29;
  St : Byte;        //按钮控制,设置状态控制指示用变量
  WX,WY : Byte;
  X,Y,DX,DY,DD,CY: Integer;   //X,Y 坐标用变量,DX,DY 坐标增量(变化量)用变量,
  XTR,YTR,QTR,DR : Extended;          //DR  弧度的变化量,
  Col : TColor;
  RectD : TRect;



implementation

{$R *.dfm}

procedure TRei29.FormCreate(Sender: TObject);
begin   //设置初始值,
  St := 0 ;
  DD := 4;
  WX := 140;
  WY := 60;
  CY := 1;
  DR := Pi * DD / 180;
  ScrollBar1.Position := DD;
  ScrollBar2.Position := WX;
  ScrollBar3.Position := WY;
  ScrollBar4.Position := CY;
end;

procedure TRei29.Timer1Timer(Sender: TObject);
begin
  case St of
    0: begin
      RectD := Rect(0,0,Image1.Width,Image1.Height);
      Image1.Canvas.Brush.Color := clBlack;
      Image1.Canvas.FillRect(RectD);   //刷黑
      Edit1.Text := ' ' + IntToStr(DD) + '°'; //显示参数
      Edit2.Text := ' ' + IntToStr(WX * 2);
      Edit3.Text := ' ' + IntToStr(WY * 2);
      Edit4.Text := ' ' + IntToStr(CY);
      if Random(2) = 0 then        //随机选择X 起点
      begin
        X := Image1.Width div 2 + WX;
        XTR := Pi / 2;
      end
      else begin
        X := Image1.Width div 2 - WX;
        XTR := Pi / 2;
      end;

      Y := 0;                  //设置Y 起点
      YTR := -Pi / 2;
      St := 1;
    end;

    1: begin
      QTR := XTR;                          //计算公式
      XTR := XTR + DR;
      DX := Round(WX * (Sin(XTR) - Sin(QTR)));
      QTR := YTR;
      YTR := YTR + DR * 2;
      DY := Round(WY * (Sin(YTR) - Sin(QTR)));
      X := X + DX;                  //取得 X,Y 坐标
      Y := Y + DY + CY;

      if DX >= 0 then
        Col := clWhite
      else
        Col := clRed;
      Image1.Canvas.Pixels[X,Y] := Col;         //4点显示
      Image1.Canvas.Pixels[X + 1,Y] := Col;
      Image1.Canvas.Pixels[X,Y + 1] := Col;
      Image1.Canvas.Pixels[X + 1,Y + 1] := Col;
      if (Y > Image1.Width  + WY) or (Y < -WY) then
        St := 2;


    end;
  end;
end;

procedure TRei29.ScrollBar1Change(Sender: TObject);
begin
  DD := ScrollBar1.Position;
  Edit1.Text := ' ' + IntToStr(DD) + '°';
  DR := 2 * Pi * DD / 180;                      //应该去掉这行,否则在绘图中会即时的改变
end;                                            //似乎又不行,在状态1中,W,DY值都被在计算
                                                // 所以这里只有同步重新计算DR值
procedure TRei29.ScrollBar2Change(Sender: TObject);
begin
   WX := ScrollBar2.Position;
   Edit2.Text := ' ' + IntToStr(WX * 2);
end;

procedure TRei29.ScrollBar3Change(Sender: TObject);
begin
  WY := ScrollBar3.Position;
  Edit3.Text := ' ' + IntToStr(WY * 2);

end;

procedure TRei29.ScrollBar4Change(Sender: TObject);
begin
  CY := ScrollBar4.Position;
  Edit4.Text := ' ' + IntToStr(CY);

end;

procedure TRei29.Button1Click(Sender: TObject);
begin
  St := 0;
end;



end.

1,程序界面和结构和上例一样,多了一个滚动条来设置参数

2,钟摆线的计算公式看不明白,只是套用。

3,参数设置会让钟摆线变形,变成其他图案了。



这篇关于Delphi 经典游戏程序设计40例 的学习 例29 残留的轨迹是钟摆线的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程