两个方法,你参考一下:
来自: muhx, 时间: 2004-07-12 4:20:15, ID: 2707426
利用两个Image比较耗费资源,感觉还是利用PaintBox或者Image画比较好,我有个差不多的例子,算法比你要求的简单一些,主要没有随即移动的算法,应该对你有帮助
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Timer1: TTimer;
PaintBox2: TPaintBox;
procedure Timer1Timer(Sender: TObject);
procedure PaintBox2Paint(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
fLeft: Integer;
fRight: Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if fLeft <= PaintBox2.Width then
Inc(fLeft)
else
fLeft := -20;
PaintBox2.Repaint;
Application.ProcessMessages;
end;
procedure TForm1.PaintBox2Paint(Sender: TObject);
var
aRect: TRect;
i: Integer;
begin
PaintBox2.Canvas.Brush.Color := clAqua;
aRect.Left := 0;
aRect.Right := PaintBox2.Width;
aRect.Top := 0;
aRect.Bottom := PaintBox2.Height;
PaintBox2.Canvas.FillRect(aRect);
for i := 0 to 4 do
begin
PaintBox2.Canvas.Brush.Color := clBlue;
aRect.Left := fLeft + i * 4;
fRight := aRect.Left + 3;
aRect.Right := fRight;
aRect.Top := 2;
aRect.Bottom := PaintBox2.Height-2;
PaintBox2.Canvas.Rectangle(aRect);
end;
PaintBox2.Canvas.Pen.Color := clBlack;
PaintBox2.Canvas.MoveTo(0,0);
PaintBox2.Canvas.LineTo(PaintBox2.Width-1,0);
PaintBox2.Canvas.LineTo(PaintBox2.Width-1,PaintBox2.Height-1);
PaintBox2.Canvas.LineTo(0,PaintBox2.Height-1);
PaintBox2.Canvas.LineTo(0,0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
fLeft := 0;
fRight := 30;
DoubleBuffered :=True;
end;
end.
来自: weichao9999, 时间: 2004-07-12 7:42:40, ID: 2707438
给你个例子,改造一下就可以了,不闪烁
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Lb_V: TLabel;
UpDown1: TUpDown;
Label3: TLabel;
Lb_A: TLabel;
UpDown2: TUpDown;
BitBtn1: TBitBtn;
Label2: TLabel;
Label4: TLabel;
Timer1: TTimer;
Image1: TImage;
Label5: TLabel;
CheckBox1: TCheckBox;
Label6: TLabel;
Lb_X: TLabel;
Lb_Y: TLabel;
Lb_S: TLabel;
BitBtn2: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
procedure UpDown2Click(Sender: TObject; Button: TUDBtnType);
procedure BitBtn1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
vo,alpha,times,x,y:Integer;
const
rate=50;
g=9.8;
tr=0.01;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
vo:=13;
alpha:=70;
times:=0;
Lb_V.Caption:=IntToStr(vo);
Lb_A.Caption:=IntToStr(alpha);
Lb_X.Caption:='';
Lb_Y.Caption:='';
Lb_S.Caption:='';
Image1.Canvas.Brush.Color:=clBlack;
Image1.Canvas.FillRect(Rect(0,0,Image1.Width,Image1.Height));
end;
procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then
begin
vo:=vo+1;
Lb_V.Caption:=IntToStr(vo);
end
else
begin
vo:=vo-1;
if vo<0 then
begin
vo:=0;
end;
Lb_V.Caption:=IntToStr(vo);
end;
end;
procedure TForm1.UpDown2Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then
begin
alpha:=alpha+1;
if alpha>=90 then alpha:=90;
Lb_A.Caption:=IntToStr(alpha);
end
else
begin
alpha:=alpha-1;
if alpha<0 then
begin
alpha:=0;
end;
Lb_A.Caption:=IntToStr(alpha);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Lb_S.Caption:='抛射状态中';
Timer1.Enabled:=True;
x:=0;
y:=Image1.Height;
times:=0;
Image1.Canvas.Brush.Color:=clBlack;
Image1.Canvas.FillRect(Rect(0,0,Image1.Width,Image1.Height));
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
bita,t:Single;
i:integer;
begin
times:=times+1;
bita:=3.1416/180;
t:=times*tr;
if CheckBox1.Checked then
begin
x:=Trunc(vo*Cos(alpha*bita)*t*rate);
y:=Trunc(-(vo*Sin(alpha*bita)*t-0.5*g*t*t)*rate+Panel1.Height);
Image1.Canvas.Pen.Color:=clRed;
Image1.Canvas.Pen.Width:=2;
Image1.Canvas.Ellipse(x-1,y-1,x+1,y+1);
end
else
begin
Image1.Canvas.Brush.Color:=clBlack;
Image1.Canvas.FillRect(Rect(X-15,y-15,x+15,y+15));
x:=Trunc(vo*Cos(alpha*bita)*t*rate);
y:=Trunc(-(vo*Sin(alpha*bita)*t-0.5*g*t*t)*rate+Panel1.Height);
with Image1.Canvas do
begin
for i:=0 to 13 do
begin
Pen.Width:=3;
Pen.Color:=RGB(18*i,0,0);
Ellipse(x-15+i,y-15+i,x+15-i,y+15-i);
end;
Pen.Color:=clWhite;
Brush.Color:=RGB(255,255,255);
Ellipse(x-1,y-1,x+1,y+1);
end;
end;
if y>Image1.Width then BitBtn1Click(self);
Lb_X.Caption:=IntToStr(x div rate);
Lb_Y.Caption:=IntTOStr(Trunc(vo*Sin(alpha*bita)*t-0.5*g*t*t));
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Lb_S.Caption:='暂停抛射';
Timer1.Enabled:=False;
end;
end.