Originally Posted by
sewing0109
I'm honestly not sure how to dsetermine the clicked cell.
Well that depends a bit on what components or library you use for drawing your game. So it would be nice if you could share some more information on this.
But based on the fact that in your posts you are mentioning ImageList and StringGrid I asume that you are using Visual components (VCL in delphi or LCL in Lazarus).
So I have written you a short example which will show you how to get mouse relative coordinates above TImage and how to caclulate which cell would be selected. I even add a bit of code to render the gridlines on TImage to make it easier for you to understand.
In order to make this example simply create a new project and put a TImage component on it.
Then you need to create an OnMouseMove event for TImage and copy the code below into it:
Code:
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var Line, Row: Integer;
begin
//Calculate over which row is mouse cursor positioned
//Row width is 20 pixels
Row := X div 20;
//Calculate ower which line is mouse cursor positioned
//Line height is 20 pixels
Line := Y div 20;
//Show output values instead of form caption
Form1.Caption := 'X:'+IntToStr(X)+' Y:'+IntToStr(Y)+' Line:'+IntToStr(Line)+' Row:'+IntToStr(Row);
end;
You also need to create an OnCreate even for main form in order to draw gridlines on TImage. Code for this is below:
Code:
procedure TForm1.FormCreate(Sender: TObject);
var X,Y: Integer;
begin
//Draw vertical lines
for X:=0 to 20 do
begin
Image1.Canvas.MoveTo(X*20,0);
Image1.Canvas.LineTo(X*20,400);
end;
//Drwa horizontal lines
for Y:=0 to 20 do
begin
Image1.Canvas.MoveTo(0,Y*20);
Image1.Canvas.LineTo(400,Y*20);
end;
end;
This example reads mouse relative coordinates above TImage component, calculates over which line/row is mouse cursor positioned and displays that instead of default Form caption.
Bookmarks