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:
//---------------------------------------------------------------------------
#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();
}
//---------------------------------------------------------------------------