Recent

Author Topic: move shape with mouse by mousemove  (Read 8441 times)

zkdjeksa

  • New Member
  • *
  • Posts: 10
move shape with mouse by mousemove
« on: November 30, 2018, 01:46:25 pm »
I am now working on a game which requires a square can be moved around by MouseMove.
The coordinate of the square was set in the form.
How can I write the procedure? Besides, how to make the square to move within a certain area and if the square touches the boundary, the game will end?


Code: Pascal  [Select][+][-]
  1. unit unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls,LclType, LclIntf;
  10.  
  11. type
  12.  
  13.   { TForm3 }
  14.  
  15.   TForm3 = class(TForm)
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     lblScore: TLabel;
  19.     Label3: TLabel;
  20.     Shape1: TShape;
  21.     Shape10: TShape;
  22.     Square: TShape;
  23.     endoflevel: TShape;
  24.     Shape2: TShape;
  25.     Shape3: TShape;
  26.     Shape4: TShape;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure SquareMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
  29.       );
  30.   private
  31.       procedure Updatescore;
  32.   public
  33.  
  34.   end;
  35.  
  36. var
  37.   Form3: TForm3;
  38.   Score: integer;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44. procedure TForm3.FormCreate(Sender: TObject);
  45. begin
  46.   Score:=0;
  47.   Updatescore;
  48.  
  49.  
  50. end;
  51.  
  52. procedure TForm3.SquareMouseMove(Sender: TObject; Shift: TShiftState; X,
  53.   Y: Integer);
  54. begin
  55.   Square.top:=
  56.   Square.Left:=
  57. end;
  58.  
  59. Procedure Tform3.Updatescore;
  60. begin
  61.   lblScore.Caption:='Score:'+ IntToStr(Score);
  62. end;
  63.  
  64.  
  65.  
  66. end.
  67.          


taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: move shape with mouse by mousemove
« Reply #1 on: November 30, 2018, 02:47:58 pm »
I am now working on a game which requires a square can be moved around by MouseMove.
The coordinate of the square was set in the form.
How can I write the procedure? Besides, how to make the square to move within a certain area and if the square touches the boundary, the game will end?


Code: Pascal  [Select][+][-]
  1. unit unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls,LclType, LclIntf;
  10.  
  11. type
  12.  
  13.   { TForm3 }
  14.  
  15.   TForm3 = class(TForm)
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     lblScore: TLabel;
  19.     Label3: TLabel;
  20.     Shape1: TShape;
  21.     Shape10: TShape;
  22.     Square: TShape;
  23.     endoflevel: TShape;
  24.     Shape2: TShape;
  25.     Shape3: TShape;
  26.     Shape4: TShape;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure SquareMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
  29.       );
  30.   private
  31.       procedure Updatescore;
  32.   public
  33.  
  34.   end;
  35.  
  36. var
  37.   Form3: TForm3;
  38.   Score: integer;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44. procedure TForm3.FormCreate(Sender: TObject);
  45. begin
  46.   Score:=0;
  47.   Updatescore;
  48.  
  49.  
  50. end;
  51.  
  52. procedure TForm3.SquareMouseMove(Sender: TObject; Shift: TShiftState; X,
  53.   Y: Integer);
  54. begin
  55.   Square.top:=
  56.   Square.Left:=
  57. end;
  58.  
  59. Procedure Tform3.Updatescore;
  60. begin
  61.   lblScore.Caption:='Score:'+ IntToStr(Score);
  62. end;
  63.  
  64.  
  65.  
  66. end.
  67.          
Code: Pascal  [Select][+][-]
  1. unit unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls,LclType, LclIntf;
  10.  
  11. type
  12.  
  13.   { TForm3 }
  14.  
  15.   TForm3 = class(TForm)
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     lblScore: TLabel;
  19.     Label3: TLabel;
  20.     Shape1: TShape;
  21.     Shape10: TShape;
  22.     Square: TShape;
  23.     endoflevel: TShape;
  24.     Shape2: TShape;
  25.     Shape3: TShape;
  26.     Shape4: TShape;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure SquareMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  29.     procedure SquareMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer );
  30.   private
  31.       FMouseDownPos, FLastPosition :TPoint;
  32.       FDragEnabled                         :Boolean;
  33.       procedure Updatescore;
  34.   public
  35.  
  36.   end;
  37.  
  38. var
  39.   Form3: TForm3;
  40.   Score: integer;
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. Procedure TForm1.SquareMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  47. begin
  48.   FMouseDownPos.X := X;
  49.   FMouseDownPos.Y := Y;
  50.   FDragEnabled := True;
  51. end;
  52.  
  53. procedure TForm3.SquareMouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);
  54. begin
  55.   if FDragEnabled then  begin
  56.     Square.Left := Square.Left + (X - FMouseDownPos.X);
  57.     Square.Top := Square.Top + (Y - FMouseDownPos.Y);
  58.   end;
  59. end;
  60.  
  61. Procedure Tform3.Updatescore;
  62. begin
  63.   lblScore.Caption:='Score:'+ IntToStr(Score);
  64. end;
  65.  
  66. end.
  67.  

Typed directly in the browser never tested or compiled if there are problems post I'll probably not be here to help but others will be able to help from here on.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 4608
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: move shape with mouse by mousemove
« Reply #2 on: November 30, 2018, 02:49:50 pm »
You need to use mouse move in the form not in the shape. Because mouse move in shape will limit to shape rectangle area.

And to detect a collision between rectangles see here
https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection#Axis-Aligned_Bounding_Box

You can use two trect to compare.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: move shape with mouse by mousemove
« Reply #3 on: November 30, 2018, 02:53:16 pm »
You need to use mouse move in the form not in the shape. Because mouse move in shape will limit to shape rectangle area.
since the shape is moving under the mouse cursor by the code on the onmousemove event the form will never receive the mouse event. That's a hypothesis but the OP will test it.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 4608
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: move shape with mouse by mousemove
« Reply #4 on: November 30, 2018, 02:54:29 pm »
You need to use mouse move in the form not in the shape. Because mouse move in shape will limit to shape rectangle area.
since the shape is moving under the mouse cursor by the code on the onmousemove event the form will never receive the mouse event. That's a hypothesis but the OP will test it.

Yes it's true I forget that.

Handoko

  • Hero Member
  • *****
  • Posts: 5376
  • My goal: build my own game engine using Lazarus
Re: move shape with mouse by mousemove
« Reply #5 on: November 30, 2018, 03:05:10 pm »
Hello zkdjeksa,
If you want to learn how to write games using Lazarus, I recommend you to read this thread:

http://forum.lazarus.freepascal.org/index.php/topic,38136

The thread is about creating a snake game. It is very long ... boring ... but you will learn many basic things for game programming:

- Game loop
- User input detection
- Using array to represent the the game world
- Collision detection
- How to use TList (to replace array)
- Using buffer for keyboard input
- Using buffer to reduce flickering
« Last Edit: November 30, 2018, 03:14:52 pm by Handoko »

Handoko

  • Hero Member
  • *****
  • Posts: 5376
  • My goal: build my own game engine using Lazarus
Re: move shape with mouse by mousemove
« Reply #6 on: November 30, 2018, 03:08:35 pm »
Also, maybe you'll interested to create a simple pong game:
http://forum.lazarus.freepascal.org/index.php/topic,42439.msg297949.html#msg297949

zkdjeksa

  • New Member
  • *
  • Posts: 10
Re: move shape with mouse by mousemove
« Reply #7 on: November 30, 2018, 03:19:21 pm »
 :D :DThank you guys very much!!!

flowCRANE

  • Hero Member
  • *****
  • Posts: 885
Re: move shape with mouse by mousemove
« Reply #8 on: December 01, 2018, 04:12:52 pm »
A long time ago, for the purpose of a thread on another forum, I wrote a simple application for moving virtual rectangles in a window. The code of this application was written in Delphi 7 and it was quite long, so I decided to refresh it a bit—use generic lists and generally the richer functionality of Free Pascal. Thus, the source of this application is added to the attachments.

The program displays several colored rectangles that you can drag in the window with the left mouse button. To render them, it uses the TPaintBox control. Changes to the position of rectangles while dragging are visible when you drag. The program has protection against moving the rectangle outside the window. If the rectangle touches the edge of the window, the appropriate information is displayed inside rectangle.

I hope that the code of this application will come in handy. Good luck.
« Last Edit: December 01, 2018, 11:19:41 pm by furious programming »
Lazarus 3.6 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on a retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL.

Handoko

  • Hero Member
  • *****
  • Posts: 5376
  • My goal: build my own game engine using Lazarus
Re: move shape with mouse by mousemove
« Reply #9 on: December 01, 2018, 04:48:11 pm »
I'm an old school programmer, I haven't updated my knowledge to use generic lists. I've just downloaded and tried your code. It is neatly written and easy to understand, I can use it to learn generic. Thank you furious programming for sharing the code.

For others who want to learn how to move shape using mouse, here I ever wrote some simple demos:

Drag and drop shape:
http://forum.lazarus.freepascal.org/index.php/topic,38844.msg265130.html#msg265130

Drawing, moving and make connection between shapes:
http://forum.lazarus.freepascal.org/index.php/topic,41036.msg284131.html#msg284131

 

TinyPortal © 2005-2018