Link to home
Start Free TrialLog in
Avatar of karatekid183
karatekid183

asked on

Need help with tag type delphi game

i got both my character to move around the screen(there is no AI just a 2 pplayer game) i need it so when my image1 goes on image2. label1 says hit and then my score section will say 1
Avatar of NightAwk
NightAwk

Well first of all i'll asume u have a timer, if not then
u'll need one, to make image1 move toward image2.

anyway in the timer, simply detect collision w this fucntion :

function Form1.Hit : Boolean ;
begin
  Hit := False ;

  (* this function check if image1 is overlaping image2 *)
  if (image1.Left+image1.Width >= image2.Left)
  and (image1.Left <= image2.Left+image2.Width)
  and (image1.Top+image1.Height >= image2.Top)
  and (image1.Top <= image2.Top+image2.Height) then
    Hit := True ;
end ;

set the timer interval to 1000 and every timer call, move ur image1 a lil bit more in the direction its going, so u will get a smooth animation. And call Hit eachtime, if it returns true, set ur label and score caption.

this fonction works only check for 4 axis, if u plan
to use diagonals too, then u would have to change the hit function a lil bit i think.

Good luck.
Avatar of karatekid183

ASKER

actually i dont have a time, all i have right now is both of my character are able to move round the screen(by P1 using the arrows and P2 using w,a,s,d) so what do i have to put in my timers and what does form1.hit:boolean mean
There is no need for timer if there's nothing moving without user pressing keys.

Let's make it simple:

If your form's name is Form1, then Replace erroreous line:

function Form1.Hit: Boolean;
with
function TForm1.Hit: Boolean;

Then after the key handling code put these:

if Hit then begin

Label1.Caption := 'Hit';

end else begin

// What exactly must be done when player scores,
// comes here

end;
Where do i put function TForm1.Hit: Boolean??
Where do i put function TForm1.Hit: Boolean??
where do i put TForm1.Hit: Boolean
where do i put TForm1.Hit: Boolean
u put the interface of the function here :


  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    function Hit : boolean ;
  public
    { Public declarations }
  end;

and u put the function itself where ever u like in the unit.

if ur goal is to only detect if the two players are touching each other, then yeah u dont need a timer,
i was thinking you were like usign projectile, like a gun
fighting game, then u would need a timer to make the projectile move. but that not the case so forget about timers.

jsut call hit everytime a player moves his character.

the function should work in theory, u may have to change variables names and all.

good luck.

P.S. maybe learning a lil bit more about delphi and apscal wouldn't hurt you, u know ..  anyone woudl had known that bye creating a Tform1.hit fucntino u would have to put the declaration in the Tform1 class...
u put the interface of the function here :


  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    function Hit : boolean ;
  public
    { Public declarations }
  end;

and u put the function itself where ever u like in the unit.

if ur goal is to only detect if the two players are touching each other, then yeah u dont need a timer,
i was thinking you were like usign projectile, like a gun
fighting game, then u would need a timer to make the projectile move. but that not the case so forget about timers.

jsut call hit everytime a player moves his character.

the function should work in theory, u may have to change variables names and all.

good luck.

P.S. maybe learning a lil bit more about delphi and pascal wouldn't hurt you, u know ..  anyone woudl had known that bye creating a Tform1.hit fucntino u would have to put the declaration in the Tform1 class...
u put the interface of the function here :


  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    function Hit : boolean ;
  public
    { Public declarations }
  end;

and u put the function itself where ever u like in the unit.

if ur goal is to only detect if the two players are touching each other, then yeah u dont need a timer,
i was thinking you were like usign projectile, like a gun
fighting game, then u would need a timer to make the projectile move. but that not the case so forget about timers.

jsut call hit everytime a player moves his character.

the function should work in theory, u may have to change variables names and all.

good luck.

P.S. maybe learning a lil bit more about delphi and pascal wouldn't hurt you, u know ..  anyone would had known that by creating a Tform1.hit function u would have had to put the declaration in the Tform1 class...
ok this is what i put:
 TForm1 = class(TForm)
    Image2: TImage;
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Timer1: TTimer;
    MainMenu1: TMainMenu;
    Ouvrir1: TMenuItem;
    Nouveaux1: TMenuItem;
    Fermer1: TMenuItem;
     
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Fermer1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Déclarations privées}
  public
    { Déclarations publiques}
  end;

when i place private
   function Hit : boolean ;
 public
   { Public declarations }
 end;
 it give me like 10 errows bout procedure FormKeyDown. whats wrong, do i declare hit has a variable? also how would i place walls? BTW IM NOT A NOOB, IM IN 11TH GRADE PROGRAMMING, I KNOW HOW TO USE, FOR, CASE, IF AND OTHER STUFF
ASKER CERTIFIED SOLUTION
Avatar of NightAwk
NightAwk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
karatekid183:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       the answers contains useful information, it would be mistake to delete,
       but the original question is not solved
       to accept xx's answer
       to split points between xx and xx
       to delete this answer and refund points
       PAQ and refund points
       to delete this question and give points to xx
       to delete this question and NOT refund points
       this question don't belong to this topic, otherwise don't contain useful information
       the question to be moved into xxxx topic
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup VolunteerNo comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to accept NightAwk's answer
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup Volunteer