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

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

代码文件:

unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, DXDraws, StdCtrls, DXClass;
type
 TForm1 = class(TForm)
  DXDraw1: TDXDraw;
  DXImageList1: TDXImageList;
  DXTimer1: TDXTimer;
  Button1: TButton;
  Button2: TButton;
  procedure FormCreate(Sender: TObject);
  procedure DXTimer1Timer(Sender: TObject; LagCount: Integer);
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
var
 arr: array of record X,Y,a,b: Integer; end;
 PicItem: TPictureCollectionItem;
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;
 Button1.Caption := '-';
 Button2.Caption := '+';
 DXTimer1.Interval := 10;
end;
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;
end.

时间: 2024-08-22 14:47:52

Delphi与DirectX之DelphiX(8) 第一个简单动画的相关文章

Delphi与DirectX之DelphiX(87): TDIB.DrawTransparent、DrawTranslucent、Dra

Delphi与DirectX之DelphiX(87): TDIB.DrawTransparent.DrawTranslucent.DrawAlpha(); TDIB.DrawTransparent: 按指定的透明色透明; TDIB.DrawTranslucent: 先按指定的透明色透明, 然后再半透明; DIB.DrawAlpha: 先按指定的透明色透明, 再按指定的透明度透明. 本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages,

Delphi与DirectX之DelphiX(17):TPictureCollectionItem.PatternWidth、Patte

Delphi与DirectX之DelphiX(17):TPictureCollectionItem.PatternWidth.PatternHeight 本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DXDraws, StdCtrls; type TForm1 = class(TForm) DXD

Delphi与DirectX之DelphiX(7)

Delphi与DirectX 之DelphiX(7): 给TDXImageList加载图片的最后方案 本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DXDraws, StdCtrls; type TForm1 = class(TForm) DXDraw1: TDXDraw; DXImageList

Delphi与DirectX之DelphiX(6)

Delphi与DirectX之DelphiX(6): 让TDXImageList和常规VCL交互使用 本例测试了两个问题: 1.其他 VCL 加载的图片能否给 TDXImageList 使用; 2.TDXImageList 中的图片能否给其他 VCL 使用. 例子中先用 TPicture 加载了图片, 然后给 TDXImageList; 然后把图片绘制在了窗体上, 而非 TDXDraw 中. 继续了解点 TDXImageList: TDXImageList 控件只有两个属性: DXDraw 和

Delphi与DirectX之DelphiX(1):安装测试

测试 Demo 下载: files.cnblogs.com/del/DelphiX_1.rar (在 Delphi 2007 和 2009 下均编译通过) 其实按照这里(www.cnblogs.com/del/archive/2008/06/13/1219324.html)的介绍, 比下载来的快, 也不需要下载. DirectX, 微软很久的技术了; 从 Windows Vista 开始, DirectX 已经是微软操作系统的界面基础了. 在 Delphi 下使用 DirectX 比较流行的有下

Delphi与DirectX之DelphiX(3):初识TDXDraw

在 DelphiX 中, 所能看到的一般都是画在 TDXDraw. TDXDraw 默认两个绘图表面: TDXDraw.Surface 和 TDXDraw.Primary; 一般先在后台(Surface)绘图, 然后再用 TDXDraw.Flip 命令切换前后台, 从而看到绘制效果. TDXDraw.Flip 会置换 Surface 和 Primary 两个对象, 所以我们尽可以只在 Surface 上绘图; 由于 DirectX 的工作机制, 让这个切换速度异常地快(只是切换一个指针), 从而

Delphi与DirectX之DelphiX(24):TDirectDrawSurface.Blt()、BltFast();

这两个函数可不是一般的麻烦, 特别是Blt(); 暂时搞不彻底, 这是初步尝试的例子: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DXDraws, StdCtrls, DirectX; type TForm1 = class(TForm) DXDraw1: TDXDraw; Button1: TButton

Delphi与DirectX之DelphiX(91): TDIB.DrawMono();

本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DIB, StdCtrls; type TForm1 = class(TForm) DXPaintBox1: TDXPaintBox; Button1: TButton; procedure Button1Click(Sender: TObject);

Delphi与DirectX之DelphiX(90): TDIB.DrawMorphed();

本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DIB, StdCtrls; type TForm1 = class(TForm) DXPaintBox1: TDXPaintBox; Button1: TButton; Button2: TButton; Button3: TButton; proc