Witam.
Potrzebuje pomocy przy przerobieniu gry z jednego na dwoch graczy.

  • pileczka powinna przechodzic na dole i u gory co bedzie oznaczalo zdobycie punktu.

Program wyglada nastepujaco:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "PPong.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource ".dfm"
TForm1
Form1;
int x, y; //wspolrzedne pilki
int dx, dy; //przyrost wspolrzednych
int r; //promin pilki
TColor cBall; //color pilki
TColor cBack; //color tla
int wp, hp; //rozmir pola dla ping-pongu

// zmienne dla sterowania gra
int rd; // 0 - stop / 1 - wlewo 2 - wprawo
int rx1, rx2; // wspolrzedne X paska dla odbicia
int ry; // wpolrzedna Y paska
int rdx; // krok przesowania paska
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
r = 5; // polka
x = 10; y = 10; // polozenie startowe pilki
dx = 1;
dy = 1;
cBall = (TColor)RGB(217, 217, 25); // color pilki
cBack = (TColor)RGB(33, 94, 33); // color tla
Form1->Color = cBack;
wp = Form1->ClientWidth;
hp = Form1->ClientHeight;
// sterowanie paskiem
rd = 0; // pasek stoi na miejscy
rx1=100;
rx2=155;
ry = Form1->ClientHeight - 20; // polozenie pionowe pasku
rdx = 2; // krok ruchu paska

// ustawenia zegara
Timer1->Interval = 10;
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (rd != 0 )
// trzymamy klawisz
return ;
switch (Key)
{
case VK_LEFT : // wlewo
rd = 2; break;
case VK_RIGHT: // wprawo
rd = 1; break;
}

}
//---------------------------------------------------------------------------
void fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
rd = 0;
}
//---------------------------------------------------------------------------
void
fastcall TForm1::FormPaint(TObject Sender)
{
// rysujeme pasek
Form1->Canvas->Pen->Color = clRed;
Form1->Canvas->Rectangle(rx1, ry, rx2, ry+1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject
Sender)
{
// scieranie pilki
Form1->Canvas->Pen->Color = cBack;
Form1->Canvas->Ellipse(x,y,x+r,y+r);
// nowe wspolrzedne pilki
if ( dx > 0 )
{
// pilka leci wprawo
if ((x + dx + r) >wp) dx = -dx;
}
// ....wlewo
if ((x + dx - r) < 0 ) dx = -dx;

if ( dy > 0 )
{
// .... wdol
if ((x >= rx1) && (x <= rx2 -r) && (y == ry - r - 1))
// trafienie w pasek
dy = - dy;
else
if (y + dy + r > Form1->ClientHeight ) dy = -dy;
}
else
{
// .... dogory
if ((x >= rx1) && (x <= rx2) && (y >= ry - r) && (y <= ry + r))
{
// Pilka odbila sie od dolnej sciany i trafila w pasek z tylu
// Rysujemy ponownie pasek
Form1->Canvas->Pen->Color = clRed;
Form1->Canvas->Rectangle(rx1, ry, rx2, ry+1);
}
if (y + dy - r < 0) dy = -dy;
}
x+= dx;
y+= dy;
// rysowanie pilki w nowym punkcie
Form1->Canvas->Pen->Color = cBall;
Form1->Canvas->Ellipse(x,y,x+r,y+r);

// ruch paska
if (rd != 0)
{
// ncisniety i utrymywany klawisz "wlewo" lub "wprawo"

if (rd == 1)
{
// wprawo
if (rx2 < wp)
{
// rysujem ponownie pasek
Form1->Canvas->Pen->Color = cBack;
Form1->Canvas->Rectangle(rx1,ry,rx1+rdx,ry + 1);
Form1->Canvas->Pen->Color = clRed;
Form1->Canvas->Rectangle(rx2,ry,rx2+rdx,ry+1);
rx1+= rdx;
rx2+= rdx;
}
}
else
// wlewo
if (rx1 > 1 )
{
Form1->Canvas->Pen->Color = cBack;
Form1->Canvas->Rectangle(rx2,ry,rx2-rdx,ry+1);
Form1->Canvas->Pen->Color = clRed;
Form1->Canvas->Rectangle(rx1 - rdx, ry, rx1 +rdx, ry+1);
rx1-=rdx;
rx2-=rdx;
}
}

}

//---------------------------------------------------------------------------

[color=darkblue][size=9][i][b]Złączono Posta[/b]: 02.06.2007 (Sob) 9:47[/i][/size][/color]
do tego sa jeszcze 2 pliczki:

//---------------------------------------------------------------------------

#ifndef PPongH
#define PPongH
//---------------------------------------------------------------------------
#include <classes.hpp>
#include <controls.hpp>
#include <stdctrls.hpp>
#include <forms.hpp>
#include <extctrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
published: // IDE-managed Components
TTimer *Timer1;
void
fastcall FormKeyDown(TObject Sender, WORD &Key,
TShiftState Shift);
void __fastcall FormKeyUp(TObject
Sender, WORD &Key,
TShiftState Shift);
void fastcall FormPaint(TObject *Sender);
void
fastcall Timer1Timer(TObject Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent
Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

[b]i [/b]

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("PPong.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
//---------------------------------------------------------------------------