GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Sep-2006, 11:53
Cyphas Cyphas is offline
New Member
 
Join Date: Sep 2006
Posts: 1
Cyphas is on a distinguished road

Coding a Bouncing Ball and GoalKeeper


Hi All

I am trying to program a simple game but have a bit of trouble with the coding. The game /program is meant to have a bouncing ball(TShape: cCircle) that bounce and change direction when it collides with surface of the GUI -form

Also it is meant to bounce off a TShape object-in my case a rounded rectangle(goal keeper). And adds a point for every time it doesie every time the goal keeper defends /prevent the ball from scoring. If the ball passes the goal keeper no further point is given and the program terminate.

I am having trouble with the code in blue. Also How do I call a handlerie another button from within codes of another button.. The Play On part...want to call RESTART

See Code below as well as attchment for full program:

Thanks for ur help in advance:

CPP / C++ / C Code:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "drkBandBU.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Interval = 3;

// Moves the ball both horiz & vertical directions
BallXPos = BallXPos + BallXMove;
BallYPos = BallYPos + BallYMove;

//Changes ball direction when it hits the vertical edges
if (((BallYPos <= 0 ) || (BallYPos >= CH - Ball->Height)||
   ((BallYPos >= KeeperYPos - Ball->Height) &&
   (BallXPos >= KeeperXPos - Ball->Width)) ||
   ((BallYPos <= KeeperYPos - Keeper->Height) &&
   (BallXPos >= KeeperXPos - Ball->Width))))
  {
   BallYMove = (-1) * BallYMove;
   if (!BallYPos <= 0)
    {
    Points = Points + 1;
    PointsDisplay->Text = AnsiString(Points);
    }
  }
if ((BallXPos <= 0 )||((BallXPos >= KeeperXPos - Ball->Width) &&
   (BallYPos <= KeeperYPos + Keeper->Height)&&
   (BallYPos >= KeeperYPos) &&
   (BallXPos <= KeeperXPos + Keeper->Width)))
  {
   BallXMove = (-1) * BallXMove;
   if (!BallXPos <= 0)
    {
    Points = Points + 1;
    PointsDisplay->Text = AnsiString(Points);
    }
  }
      
if (BallXPos >= CW - Ball->Width)
     {
      Ball->Hide();
      Timer1->Enabled = false;
      if (Points >= 50)
         EndTxt->Text= "GAME OVER!! -You are truly a Professional." ;
      else
         EndTxt->Text="Sorry, GAME OVER!! You lost - Get some more training.";
      EndPanel->Visible = true;
     }
Ball->Left = BallXPos;
Ball->Top = BallYPos;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
CW = ClientWidth - 1;
CH = ClientHeight - 1;
BallXPos = Ball->Left;
BallYPos = Ball->Top;
KeeperXPos = Keeper->Left;
KeeperYPos = Keeper->Top;
BallXMove = 2;
BallYMove = -2;
Points = 0;
EndPanel->Hide();
BeginPanel->Visible = true;




}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
 Ball->Left = BallXPos;
 Ball->Top = BallYPos;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::MoveUpButtonClick(TObject *Sender)
{
if (Keeper->Top >0)
Keeper->Top = Keeper->Top - 8;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::MoveDownButtonClick(TObject *Sender)
{
if (Keeper->Top < CH - (Keeper->Height))
Keeper->Top = Keeper->Top + 8;
}
//---------------------------------------------------------------------------







void __fastcall TForm1::QuitButtonClick(TObject *Sender)
{
Close();        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ReadyButtonClick(TObject *Sender)
{
EndPanel->Hide();
Timer1->Enabled = true;
BeginPanel->Hide();
}
//---------------------------------------------------------------------------
Attached Files
File Type: zip drkBandBU.zip (22.4 KB, 9 views)
Last edited by LuciWiz : 01-Sep-2006 at 13:11. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-Sep-2006, 13:13
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,037
LuciWiz is just really niceLuciWiz is just really niceLuciWiz is just really niceLuciWiz is just really nice

Re: Coding a Bouncing Ball and GoalKeeper


Quote:
Originally Posted by Cyphas
I am having trouble with the code in blue.

That would be the void __fastcall TForm1::Timer1Timer(TObject *Sender) function. I edited your code and added code tags
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogGID Spam Detector 1.1.0 by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:21.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.