Delphi 与 DirectX 之 DelphiX(8): 第一个简单动画

  • A+
所属分类:[开发技巧]

本例效果图(用 gif 动画演示, 无论如何也出不来 的流畅效果):

代码文件:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
DXDraw1: TDXDraw;
DXImageList1: TDXImageList;
DXTimer1: TDXTimer;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure DXTimer1Timer(Sender: TObject; LagCount: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
var
arr: array of record X,Y,a,b: Integer; end;
PicItem: TPictureCollectionItem;

 

procedure TForm1.Button1Click(Sender: TObject);
begin
if Length(arr) >1 then
SetLength(arr, Length(arr)-1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
SetLength(arr, Length(arr)+1);
arr[High(arr)].X := Random(DXDraw1.Width - PicItem.Width);
arr[High(arr)].Y := Random(DXDraw1.Height - PicItem.Height);
arr[High(arr)].a := 1;
arr[High(arr)].b := 1;
end;

procedure TForm1.DXTimer1Timer(Sender: TObject; LagCount: Integer);
var
i: Integer;
begin
DXDraw1.Surface.Fill(0);
for i := 0 to Length(arr) - 1 do
begin
if arr[i].X > DXDraw1.Width - PicItem.Width then arr[i].a := -1;
if arr[i].Y > DXDraw1.Height - PicItem.Height then arr[i].b := -1;
if arr[i].X = 0 then arr[i].a := 1;
if arr[i].Y = 0 then arr[i].b := 1;
Inc(arr[i].X, arr[i].a);
Inc(arr[i].Y, arr[i].b);
PicItem.Draw(DXDraw1.Surface, arr[i].X, arr[i].Y, 0);
end;
DXDraw1.Flip;
end;

procedure TForm1.FormCreate(Sender: TObject);
const
ImgPath1 = 'C:\Temp\DelphiX.bmp';
begin
DXDraw1.Align := alClient;
DXImageList1.DXDraw := DXDraw1;

DXImageList1.Items.Add;
PicItem := DXImageList1.Items[DXImageList1.Items.Count-1];
PicItem.Picture.LoadFromFile(ImgPath1);

SetLength(arr,1);
arr[0].X := Random(DXDraw1.Width - PicItem.Width);
arr[0].Y := Random(DXDraw1.Height - PicItem.Height);
arr[0].a := 1;
arr[0].b := 1;

DXTimer1.Interval := 10;
end;

end.

窗口文件:

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 278
ClientWidth = 376
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
DesignSize = (
376
278)
PixelsPerInch = 96
TextHeight = 13
object DXDraw1: TDXDraw
Left = 3
Top = -17
Width = 365
Height = 239
AutoInitialize = True
AutoSize = True
Color = clBlack
Display.FixedBitCount = False
Display.FixedRatio = True
Display.FixedSize = True
Options = [doAllowReboot, doWaitVBlank, doCenter, do3D, doDirectX7Mode, doHardware, doSelectDriver]
SurfaceHeight = 239
SurfaceWidth = 365
TabOrder = 2
Traces = <>
end
object Button1: TButton
Left = 293
Top = 245
Width = 25
Height = 25
Anchors = [akRight, akBottom]
Caption = '-'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 324
Top = 245
Width = 25
Height = 25
Anchors = [akRight, akBottom]
Caption = '+'
TabOrder = 1
OnClick = Button2Click
end
object DXImageList1: TDXImageList
Items.ColorTable = {
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
&nbsp
;   0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000}
Items = <>
Left = 328
Top = 152
end
object DXTimer1: TDXTimer
ActiveOnly = True
Enabled = True
Interval = 1000
OnTimer = DXTimer1Timer
Left = 336
Top = 88
end
end
 

8第一个简单动画.rar

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: