图象移动(50分)

  • 主题发起人 主题发起人 shenzhou_nz
  • 开始时间 开始时间
S

shenzhou_nz

Unregistered / Unconfirmed
GUEST, unregistred user!
要在paintbox或者image上绘制简单的几何图形,诸如矩形和三角形或它们的组合体,要求各个图形能根据给定的速度和方向运动。怎么才能实现图形的运动和方向的改变,请指教,谢谢!!!
 
1、关于运行可以调整图形坐标实现;
2、关于运行速度可以通过调整坐标变化步长以及Timer的Interval属性来实现;
3、关于运动方向可以通过判断图形边界坐标是否到达Image控件边界来实现,若到达边界则改换方向,否则继续沿原轨迹运动。
 
才50分算了,要移动比较麻烦得必须做成图层得样式不然比较麻烦,但作成图层透明又比较麻烦这个不是简单得问题,分数太小了
 
我是新手,没分可送啊,请帮忙解答一下,分不在多嘛
 
可能没说清楚
图形是画上去的,有方向性,如果运动方向改变的话,图形也要相应旋转。
 
两个方法,你参考一下:

来自: 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.
 
试了weiliu的方法,是在paintbox里画四个矩形,具有一齐移动的效果。
但如果是不同的图形呢,并且是矩形和三角的组合形,又该如何以不同速度向不同方向运动?请赐教!
 
呵呵
不同得对象怎么移动 看到得只是重绘而已,效果回很难看得
 
这种情况最好定义几个类,图像以及每个图像对应的方向是类里面的成员,速度,方向都可以制定,速度其实就是位置的偏移量,可以分为x向速度和y向速度,然后设置timer的时间间距即可实现不同的速率了,而方向的转换其实就是边界的判断罢了
 
方向的改变就是边界的判断?
请具体解释一下
 
边界是指你的所定义可移动区域的大小
当对象到达边界,即碰撞,则根据当前方向计算下一个方向
而方向的计算就很简单,无非就是x速度和y速度的正向增长或负向增长

当然,上面是考虑碰撞情况下的
如果不考虑这些边界因素,如主题所述,你只要改变x速度和y速度即可实现方向的改变
 
我是要将图片进行旋转,比如说一个长方形,当运动方向为正东时,是东西长,南北宽;
而当运动方向为正北时,是东西宽,南北长。应该是坐标变换的问题,请大侠不吝赐教!
 
凌霄图像批处理专家是一款图片批处理工具,它集批量图片格式转换、批量图片修改处理、批量重命名、批量调整尺寸、批量晒图导出、创建EXE文件、创建PDF文件于一体的图片批处理系统。凌霄图像批处理专家提供将近30种图片修改处理脚本命令(如调整亮度等)供您任意组合使用!她还可以让您轻松的将任意多的图像(图片)文件生成一个EXE或PDF文件,并提供多达150种的显示效果,方便您欣赏自己的图片,并轻易实现与家人、朋友分享!她支持多达11种(BMP, JPG,PNG, EXIF, GIF, WMF,ICO, TGA, PCX,TIFF, PPM )的基本图片格式输出。如果加上与他们等价的格式,则支持的打开图片类型超过30种。而且所有这些功能,您都可以在Windows资源管理器中通过右键菜单轻松使用。
下载地址:www.flyingspace.com
 
后退
顶部