gpt4 book ai didi

delphi - 如何测试形状和面板是否在同一位置

转载 作者:行者123 更新时间:2023-12-03 18:11:03 25 4
gpt4 key购买 nike

这个想法是你必须拍摄面板。因此面板将被设置到屏幕顶部的随机位置,然后向下移动到屏幕底部。您必须在面板到达底部之前用形状拍摄面板。但是我不知道如何测试创建的形状是否在面板的位置以重置面板。目前这是我的代码,但 if 测试为假。

unit Unit1;

interface

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

const
MaxRays=100;
RayStep=8;
type
TForm1 = class(TForm)
Panel1: TPanel;
Timer1: TTimer;
Timer2: TTimer;
Button1: TButton;
Shape1: TShape;
Timer3: TTimer;
Image1: TImage;
procedure Timer2Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure Timer3Timer(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
Rays:array[0..MaxRays-1] of TShape;

public
procedure StartPanelAnimation1;
procedure DoPanelAnimationStep1;
function PanelAnimationComplete1: Boolean;
{ Public declarations }
end;

var
Form1: TForm1;

implementation
var key : char;
{$R *.dfm}

{ TForm1 }



{ TForm1 }

procedure TForm1.DoPanelAnimationStep1;
begin
Panel1.Top := Panel1.Top+1;
end;

function TForm1.PanelAnimationComplete1: Boolean;
begin
Result := Panel1.Top=512;
end;

procedure TForm1.StartPanelAnimation1;
begin
Panel1.Top := 0;
Timer1.Interval := 1;
Timer1.Enabled := True;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
DoPanelAnimationStep1;
if PanelAnimationComplete1 then
StartPanelAnimation1;
if (shape1.Top < panel1.Top) and (shape1.Left < panel1.Left+104) and (shape1.Left > panel1.Left) then
begin
startpanelanimation1;
sleep(10);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
button1.Hide;
key := 'a';
timer2.Enabled := true;
StartPanelAnimation1;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
shape1.Visible := false;
timer2.Enabled := false;
end;

procedure TForm1.FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
image1.Left := image1.Left-10;
end;

procedure TForm1.FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
image1.Left := image1.Left+10;
end;

procedure TForm1.Timer3Timer(Sender: TObject);
var
i:integer;
begin
for i:=0 to MaxRays-1 do
if Rays[i]<>nil then
begin
Rays[i].Top:=Rays[i].Top-RayStep;
if Rays[i].Top<0 then FreeAndNil(Rays[i]);
end;
end;


procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i:integer;
begin
i:=0;
while (i<MaxRays) and (Rays[i]<>nil) do inc(i);
if i<MaxRays then
begin
Rays[i]:=TShape.Create(Self);
Rays[i].Shape:=stEllipse;
Rays[i].Pen.Color:=clRed;
Rays[i].Pen.Style:=psSolid;
Rays[i].Brush.Color:=clYellow;
Rays[i].Brush.Style:=bsSolid;
Rays[i].SetBounds(X-4,Y-20,9,41);
Rays[i].Parent:=Self;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to MaxRays-1 do Rays[i]:=nil;
end;

end.

我已经尝试过@NGLN 所说的但是当我单击鼠标按钮时形状移动 10 个像素然后停止,当它停止时正常向下移动的面板现在在屏幕顶部疯狂移动更改其左侧位置但顶部位置保持为 0。

这是新代码

unit Unit1;

interface


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

const
MaxRays=100;
RayStep=8;
type
TForm1 = class(TForm)
Panel1: TPanel;
Timer1: TTimer;
Timer2: TTimer;
Button1: TButton;
Shape1: TShape;
Timer3: TTimer;
Image1: TImage;
Timer4: TTimer;
procedure Timer2Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure Timer3Timer(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
Rays:array[0..MaxRays-1] of TShape;
public
procedure StartPanelAnimation1;
procedure DoPanelAnimationStep1;
function PanelAnimationComplete1: Boolean;
function EllipticShapeIntersectsPanel(Shape: TShape; Panel: TPanel): Boolean;
{ Public declarations }
end;

var
Form1: TForm1;

implementation
var key : char;
{$R *.dfm}

{ TForm1 }



{ TForm1 }

procedure TForm1.DoPanelAnimationStep1;
begin
Panel1.Top := Panel1.Top+1;
end;

function TForm1.PanelAnimationComplete1: Boolean;
begin
Result := Panel1.Top=512;
end;

procedure TForm1.StartPanelAnimation1;
var left : integer;
begin
Panel1.Top := 0;
randomize;
left := random(clientwidth-105);
panel1.Left := left;
Timer1.Interval := 1;
Timer1.Enabled := True;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
var I: Integer;
begin
DoPanelAnimationStep1;
if PanelAnimationComplete1 then
StartPanelAnimation1;
I := 0;
while (Rays[I] <> nil) and (I < MaxRays) do
begin
if EllipticShapeIntersectsPanel(Rays[I], Panel1) then
Inc(I);
startpanelanimation1;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
button1.Hide;
key := 'a';
timer2.Enabled := true;
StartPanelAnimation1;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
shape1.Visible := false;
timer2.Enabled := false;
end;

procedure TForm1.FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
image1.Left := image1.Left-10;
end;

procedure TForm1.FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
image1.Left := image1.Left+10;
end;


procedure TForm1.Timer3Timer(Sender: TObject);
var
i:integer;
begin
for i:=0 to MaxRays-1 do
if Rays[i]<>nil then
begin
Rays[i].Top:=Rays[i].Top-RayStep;
if Rays[i].Top<0 then FreeAndNil(Rays[i]);
end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i:integer;
left : integer;
top : integer;
begin
i:=0;
while (i<MaxRays) and (Rays[i]<>nil) do i:= i+10;
if i<MaxRays then
begin
Rays[i]:=TShape.Create(Self);
Rays[i].Shape:=strectangle;;
Rays[i].Pen.Color:=clRed;
Rays[i].Pen.Style:=psSolid;
Rays[i].Brush.Color:=clred;
Rays[i].Brush.Style:=bsSolid;
left := image1.Left+38;
top := image1.Top-30;
Rays[i].SetBounds(left,top,9,33);
Rays[i].Parent:=Self;
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Screen.Cursor:=crNone;
end;

function TForm1.EllipticShapeIntersectsPanel(Shape: TShape;
Panel: TPanel): Boolean;
var
ShapeRgn: HRGN;
begin
with Shape.BoundsRect do
ShapeRgn := CreateEllipticRgn(Left, Top, Right, Bottom);
try
Result := RectInRegion(ShapeRgn, Panel.BoundsRect);
finally
DeleteObject(ShapeRgn);
end;
end;

end.

最佳答案

因为你的形状是椭圆形的,创建一个临时区域并确定与矩形的交集 RectInRegion :

function EllipticShapeIntersectsPanel(Shape: TShape; Panel: TPanel): Boolean;
var
ShapeRgn: HRGN;
begin
with Shape.BoundsRect do
ShapeRgn := CreateEllipticRgn(Left, Top, Right, Bottom);
try
Result := RectInRegion(ShapeRgn, Panel.BoundsRect);
finally
DeleteObject(ShapeRgn);
end;
end;

(如果形状是rectangular,那么可以用Darthman的套路。)

现在将阵列中的每条光线馈送到此例程:

procedure TForm1.Timer2Timer(Sender: TObject);
var
I: Integer;
begin
...
I := 0;
while (Rays[I] <> nil) and (I < MaxRays) do
begin
if EllipticShapeIntersectsPanel(Rays[I], Panel1) then
// Do what you want to do
Inc(I);
end;
end;

关于delphi - 如何测试形状和面板是否在同一位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com