Курсовая работа: Система координат канви
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
<=Lose->Count-1; j ++)
for( i = 0; i
<=Ball->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
<=Ball->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
<=Lose->Count-1; i ++)
{ lz = Lose->Items[i];
lz->Draw; }
for( i = 0; i
<=Ball->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
<=BilliardTable->Lose->Count-1; i ++)
{ lz = BilliardTable->Lose->Items[i];
inLz = sqrt(sqr(x-lz->x)+sqr(y-lz->y))
<== lz->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
<=BilliardTable->Ball->Count-1; j ++)
{ bb = BilliardTable->Ball->Items[j];
if(! bb->exist )
continue;
if(bb->ID == ID )
continue;
d = sqrt(sqr(x-bb->x)+sqr(y-bb->y));
if((d <== R + bb->R) )
{ delta = (R+bb->R - d)/2
+ 1;
ddx = (bb->x-x)/d;
ddy = (bb->y-y)/d;
x = x - ddx*delta;
y = y - ddy*delta;
bb->x = bb->x +
ddx*delta;
bb->y = bb->y +
ddy*delta;
Result = bb;
exit; } } }
//initial
int ballSize = 10; //розмір
куль
int loseSize = ballSize + 5;
int MaxEnergy= 20; // сила
максимального удару
int CueLength = 200;
//довжина кия
float mu = 0.97;
float Step = 0.03; //
переміщення
int PyramidHeight;
//величина піраміди
float MovementLimit; //
переміщення
bool BallsInMove=false;
int Player=0;
float CompAngle;
int CompMove;
float CalculateAngle;
int tick;
bool MustBeHitted;
int Balls;
Страницы: 1, 2, 3, 4, 5 |