Система координат канви
Курсовой проект - Компьютеры, программирование
Другие курсовые по предмету Компьютеры, программирование
t Width, Height, Left, Right, Top, Bottom;
TList* Ball;
TList* Lose;
TCue Cue;
void Draw();};
float GetAngToXY( pBall b ;float hitX, hitY){
float dx, dy, d;
dx = b->x - hitX;
dy = b->y - hitY;
d = sqrt(dx*dx+dy*dy);
if(dy>0)
Result = arccos(dx/d);
else Result = -arccos(dx/d); }
void TBall::outFrom(pBall:b){
{ AB,
aa, bb,
V1, V2,
aPrXx, aPrXy,
aPrYx, aPrYy,
aPrX, aPrY,
bPrXx, bPrXy,
bPrYx, bPrYy,
bPrX, bPrY,
alfaA, betaA, gammaA,
Extended alfaB, betaB, gammaB;
if(b == NULL ) exit;
AB = sqrt(sqr(x-b->x)+sqr(y-b->y));
V1 = sqrt(dx*dx+dy*dy);
V2 = sqrt(b->dx*b->dx+b->dy*b->dy);
aPrXx = 0;
aPrXy = 0;
bPrXx = 0;
bPrXy = 0;
////////-ball #1-
if(V1>0 ) {
if((b->y-y)>0
alfaA = arccos((b->x-x)/AB);
else alfaA = -arccos((b->x-x)/AB);
if(dy>0
then gammaA = arccos(dx/V1)
else gammaA = -arccos(dx/V1);
betaA = gammaA-alfaA;
aPrX = V1*cos(betaA);
aPrY = V1*sin(betaA);
aPrXx = aPrX*cos(alfaA);
aPrXy = aPrX*sin(alfaA);
aPrYx = aPrY*sin(alfaA);
aPrYy = aPrY*cos(alfaA); }
//////////////-ball #2-
if(V2>0 ) {
if((y-b->y)>0
then alfaB = arccos((x-b->x)/AB); //=alfaA+pi
else alfaB = -arccos((x-b->x)/AB);
if(b->dy>0)
then gammaB = arccos(b->dx/V2);
else gammaB = -arccos(b->dx/V2);
betaB = gammaB-alfaB;
bPrX = V2*cos(betaB);
bPrY = V2*sin(betaB);
bPrXx = bPrX*cos(alfaB);
bPrXy = bPrX*sin(alfaB);
bPrYx = bPrY*sin(alfaB);
bPrYy = bPrY*cos(alfaB); }
dx = ((dx - 2*aPrXx) + bPrXx)*mu; // = mu*(bPrXx - aPrXx)
dy = ((dy - 2*aPrXy) + bPrXy)*mu; // = mu*(bPrXy - aPrXy)
b->dx = ((b->dx - 2*bPrXx) + aPrXx)*mu; // = mu*(aPrXx - bPrXx)
b->dy = ((b->dy - 2*bPrXy) + aPrXy)*mu; // = mu*(aPrXy - bPrXy)}
void InitSound(){
pcm->wFormatTag = WAVE_FORMAT_PCM;
pcm->nChannels = 1;
pcm->nSamplesPerSec = 44100;
pcm->nAvgBytesPerSec = 2*44100;
pcm->nBlockAlign = 2;
pcm->wBitsPerSample = 16;
pcm->cbSize = 0;
WaveOut = 0;
open_status = waveOutOpen(&WaveOut, 0, &pcm, Form1->Handle,
0, callback_Window)}
float CalCulateAngle(){
{ int i, j;
pLose ToLz, lz;
pBall nearestBall, Bl, b;
float hitX, hitY;
minAng, a2Lz, minD,
float dx, dy, a, minDist, d;
minDist = 1.7e+308;
minD = minDist;
with BilliardTable do
{ for( j = 0; j Count-1; j ++)
for( i = 0; i Count-1; i ++)
{ lz = Lose->Items[j];
b = Ball->Items[i];
if(! b->exist ) continue;
d = sqrt(sqr(b->x-lz->x)+sqr(b->y-lz->y));
if(d < minDist )
{ minDist = d;
ToLz = lz;
Bl = b; } }
if((Bl == NULL) ) exit;
dx = Bl->x - ToLz->x;
dy = Bl->y - ToLz->y;
d = sqrt(dx*dx+dy*dy);
if((dy)>0
a2Lz = arccos(dx/d);
else a2Lz = -arccos(dx/d);
hitX = Bl->x + cos(a2Lz)*Bl->R;
hitY = Bl->y + sin(a2Lz)*Bl->R;
minAng = 1.7e+308;
for( i = 0; i Count-1; i ++)
{ b = Ball->Items[i];
if((b->ID == Bl->ID) || (not b->exist)
continue;
a = GetAngToXY(b, hitX, hitY);
if(abs(a2Lz-a) < minAng )
{ minAng = abs(a2Lz-a);
nearestBall = b;
Result = a; } }
for( i := 0 to Ball.Count-1 do
begin
b := Ball.Items[i];
if (b.ID = Bl.ID) or (not b.exist)
continue;
d := sqrt(sqr(b.x-Bl.x)+sqr(b.y-Bl.y));
if d < minD then
begin
minD := d;
nearestBall := b;
end;
end;
dx := Bl.x - ToLz.x;
dy := Bl.y - ToLz.y;
d := sqrt(dx*dx+dy*dy);
if (dy)>0
a2Lz := arccos(dx/d)
else a2Lz := -arccos(dx/d);
hitX := Bl.x + cos(a2Lz)*Bl.R;
hitY := Bl.y + sin(a2Lz)*Bl.R;
dx := nearestBall.x - hitX;
dy := nearestBall.y - hitY;
d := sqrt(dx*dx+dy*dy);
if (dy)>0
then a := arccos(dx/d)
else a := -arccos(dx/d);
Result := a;}
void ComputerMove(){
Cue->visible = True;
CompAngle = CalculateAngle;
if(CompAngle > Cue->angle)
CompMove = 1;
else CompMove = -1;}
void TBilliardTable::Draw(){
{ int i;
pBall *b;
pLose *lz;
char* WhoIsIt;
Canvas->Brush->Color = clBlack;
Canvas->Pen->Color = clBlack;
Canvas->Rectangle(0,0,Width, Height);
Canvas->Brush->Color = $336699;
Canvas->Pen->Color = clYellow;
Canvas->Rectangle(BilliardTable->Left - LoseSize, BilliardTable->Top - LoseSize,
BilliardTable->Right + LoseSize, BilliardTable->Bottom + LoseSize);
Canvas->Brush->Color = clGreen;
Canvas->Rectangle(BilliardTable->Left, BilliardTable->Top, BilliardTable->Right, BilliardTable->Bottom);
Canvas->Pen->Color = clYellow;
Canvas->Pen->Color = clBlack;
Canvas->Ellipse(BilliardTable->Left + (3 * BilliardTable->Width / 4)-2,
BilliardTable->Top + (BilliardTable->Height / 2)-2,
BilliardTable->Left + (3 * BilliardTable->Width / 4)+2,
BilliardTable->Top + (BilliardTable->Height / 2)+2);
Canvas->Brush->Color = $336699;
Canvas->Font->Color = clYellow;
Canvas->Font->Style = [];
if(Player == 0
then WhoIsIt = "Игрок";
if(Player == 1
then WhoIsIt = "Компьютер";
Canvas->TextOut(BilliardTable->Left+30, dh + 1,
"Ход: "+WhoIsIt+"а");
Canvas->TextOut(BilliardTable->Left+30, BilliardTable->Bottom,
"В лузах:"+IntToStr(InLoses));
if(Player == 0 ) Canvas->Font->Style = [fsBold];
Canvas->TextOut(BilliardTable->Right-150, BilliardTable->Bottom,
"Игрок:"+IntToStr(PlayerN[0]->balls));
Canvas->Font->Style = [];
if(Player == 1 ) Canvas->Font->Style = [fsBold];
Canvas->TextOut(BilliardTable->Right-150, dh + 1,
"Компьютер:"+IntToStr(PlayerN[1]->balls));
Canvas->Brush->Color = clBlack;
Canvas->Font->Color = clYellow;
Canvas->Font->Style = [fsBold];
Canvas->Font->Style = [];
for( i = 0; i Count-1; i ++)
{ lz = Lose->Items[i];
lz->Draw; }
for( i = 0; i Count-1; i ++)
{ b = Ball->Items[i];
if(b->exist ) b->Draw; }
if(Cue->visible ) Cue->Draw; }
gsGameOver:
{ }
case } // case;break;; }
void TLose::Draw()
{ with Form1->Image1->Canvas do
{ Brush->Color = clBlack;
Pen->Color = clYellow;
Ellipse(Trunc(x-r), Trunc(y-r),
Trunc(x+r), Trunc(y+r));
Font->Color = clWhite;
TextOut(x-4,y-8, IntToStr(ballsInside)); } }
void TBall::Draw()
{ with Form1->Image1->Canvas do
{ Brush->Color = col;
Pen->Color = col;//clBlack;
Ellipse(Trunc(x-r), Trunc(y-r),
Trunc(x+r), Trunc(y+r));
Brush->Color = clWhite;
Pen->Color = clWhite;
Ellipse(Trunc(x-r*sqrt(2)/2*0.5-2), Trunc(y-r*sqrt(2)/2*0.5-2),
Trunc(x-r*sqrt(2)/2*0.5+2), Trunc(y-r*sqrt(2)/2*0.5+2));
Brush->Color = col;
Font->Color = clWhite - col;
if(ShowID )
{ TextOut(Trunc(x-4), Trunc(y-8), IntToStr(ID)); }
Refresh; } }
void TCue->Draw()
{ int x1, y1, x2, y2, x3, y3;
Brush->Color = clYellow;
Pen->Color = clYellow;
Pen->Width = 4;
x1 = Trunc(ToBall->x+cos(Angle)*(ToBall->R+energy));
y1 = Trunc(ToBall->y+sin(Angle)*(ToBall->R+energy));
x2 = Trunc(ToBall->x+cos(Angle)*(ToBall->R+CueLength+energy));
y2 = Trunc(ToBall->y+sin(Angle)*(ToBall->R+CueLength+energy));
x3 = Trunc(ToBall->x-cos(Angle)*1000);
y3 = Trunc(ToBall->y-sin(Angle)*1000);
MoveTo(x1, y1);
LineTo(x2, y2);
Pen->Width = 1;
Brush->Color = clWhite;
Pen->Color = clWhite;
Ellipse(x1-2,y1-2,x1+2,y1+2);
if(ShowLine )
{ Pen->Style = psDash;
MoveTo(x1, y1);
LineTo(x3, y3);
Pen->Style = psSolid; } } }
void TBall->Stop;
{ dx = 0;
dy = 0;
stopped = true; }
bool TBall::InLose(){
int Number;
int i;
pLose lz;
boolean inLz;
Result = False;
if(! exist ) exit;
for( i = 0; i Count-1; i ++)
{ lz = BilliardTable->Lose->Items[i];
inLz = sqrt(sqr(x-lz->x)+sqr(y-lz->y)) R;
if(inLz )
{ Number = i;
Result = True;
inc(InLoses);
Exit; } } }
pBall TBall->CollisedWith();
{ int j;
pBall bb;
real d, delta, ddx, ddy;
Result = NULL;
if(! exist ) exit;
for( j = 0; j Count-1; j +