Система координат канви

Курсовой проект - Компьютеры, программирование

Другие курсовые по предмету Компьютеры, программирование

Скачать Бесплатно!
Для того чтобы скачать эту работу.
1. Пожалуйста введите слова с картинки:

2. И нажмите на эту кнопку.
закрыть



+)

{ 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) )

{ 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;

int ballsIn;

TForm1 *Form1;

TCue *Cue=new TCue;

TBall *Ball=new TBall;

TBilliardTable *BilliardTable= new TBilliardTable;

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{}

void __fastcall TForm1::StopAll(){

{ int i;

TBall *b = new TBall;

{ for( i =0; iCount-1; i ++)

{ //b=BilliardTable->Ball->Items[i];

b->dx = 0;

b->dy = 0; }

Cue->Visible = True; } }}

void ComputerMove(){

Cue->Visible = True;

CompAngle = CalculateAngle;

if(CompAngle > Cue->Angle)

CompMove = 1;

else CompMove = -1;}

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,

TShiftState Shift)

{{ int cur;

TBall *b= new TBall;

TBilliardTable *BilliardTable= new TBilliardTable;

{ if(BallsInMove) exit;

if(! Cue->Visible ) exit;

if(Player == 1 ) exit;// нельзя управлять во время хода компьютера!

// cur = Cue->ToBall->ID;

if(Key == VK_DOWN)

Cue->Angle = Cue->Angle - Step;

if(Key == VK_UP)

Cue->Angle = Cue->Angle + Step;

if(Key == VK_RIGHT )

{ cur = (cur + 1) % Ball->count;

//b = Ball->Items[cur];

}while( b->exists);

if(Key == VK_LEFT )

do{ cur = (cur - 1 + Ball->count) % Ball->count;

// b = Ball->Items[cur];

}while( b->exists);

if(Key == 32 ) if (Cue->Visible) Cue->Hit();

if(Key == 13 ) ComputerMove();

if (Key = 13) StopAll();

// Cue->ToBall = Ball->Items[cur]; } }}

void __fastcall TForm1::Timer1Timer(TObject *Sender)

{{ int cur, i, num;

TBall *b, *b2 = new TBall;

boolean allstopped;

TLose *lz = new TLose;

float d;

char* Mess;

{ tick++;

6.28do">/* while( Cue.Angle > 6.28 do

Cue.Angle := Cue.angle - 6.28;

while( Cue.Angle < 0 do

Cue.Angle := Cue.angle + 6.28;

*/ if (CompMove != 0 && Player == 1)

{ Cue->Angle = Cue->Angle + Step*CompMove;

if(abs(CompAngle-Cue->Angle) <= Step)

{ CompMove = 0;

Cue->Angle = CompAngle;

MustBeHitted = True; } }

if(Player == 0 ) MustBeHitted = False;

if(MustBeHitted )

if(Cue->energy > MaxEnergy/2 )

{ Cue->Hit();

MustBeHitted = False; } for(int i =0; i count-1; i++)

{ // b = Ball->Items[i];

if(!b->exists ) continue;

if(b->InLose(num) )

{ b->Stop();

b->exists = False;

Balls--;

ballsIn++;

//lz = BilliardTable->Lose->Items[num];

lz->ballsInside++;

//PlayerN[Player]->balls++;

if(Balls == 1 )

{ GameStatus->gsGameOver();

Timer1->Enabled = False; }

cur = Cue->ToBall->ID;

do{

cur = (cur + 1) % Ball->Count;

b = Ball->Items[cur];

}while( b->exist);

Cue->ToBall = b; }

b->dx = b->dx*mu;

b->dy = b->dy*mu;

if((b->x+b->dx > BilliardTable->Right-b->R)

|| (b->x+b->dx R)

b->dx = -b->dx * mu;

b->x = b->x + b->dx;

if((b->y+b->dy > BilliardTable->Bottom-b->R)

|| (b->y+b->dy R)

b->dy = -b->dy * mu;

b->y = b->y + b->dy;

b2 = b->collisedWith;

b->outFrom(b2);

d = sqrt(sqr(b->dx)+sqr(b->dy));

if(d < MovementLimit )

{ b->Stop; }

Cue->energy = Trunc(MaxEnergy/2*cos(tick/5)+MaxEnergy/2)+1; }

allstopped = True;

for( i = 0; i Count - 1; i ++)

{ b = Ball->Items[i];

allstopped = (b->dx == 0) && (b->dy == 0) && allstopped; }

if(allstopped && MovedLater )

{ Cue->visible = True;

if(ballsIn == 0

then Player = 1 - Player;

if(Player == 1 ) ComputerMove;

ballsIn = 0; }

MovedLater = !allstopped;

BallsInMove = !allStopped;

Draw; } }}

void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)

{ if(BallsInMove ) exit;

if(Key in ["i", "I"] )

{ ShowID = ! ShowID; }

if(Player == 1 ) exit;// нельзя управлять во время хода компьютера!

if(Key in ["h", "H"] then ComputerMove;// help me!}

void __fastcall TForm1::FormDestroy(TObject *Sender)

Create(ExtractFilePath(ParamStr(0))+"\settings.ini");">{ INI = TIniFile->Create(ExtractFilePath(ParamStr(0))+"\settings.ini");

INI->Writeint ("Phisics", "ballSize", ballSize);

INI->Writeint ("Phisics", "PocketSize", loseSize);

INI->Writeint ("Phisics", "MaxEnergy", MaxEnergy);

INI->Writeint ("Phisics", "CueLength", CueLength);

INI->Writeint ("Phisics", "PyramidHeight", PyramidHeight);

INI->WriteFloat("Phisics", "Friction", mu);

INI->WriteFloat("Phisics", "AngleStep", Step);

INI->WriteFloat("Phisics", "MovementLimit", MovementLimit);

INI->Writeint ("Phisics", "TimeInterval", Timer1->Interval);}

void __fastcall TForm1::Button1Click(TObject *Sender)

{ int i, j;

pBall b;

pLose luza;

boolean unique, inrect;

randomize;

Player = 0;

CompMove = 0;

Timer1->Enabled = True;

with Image1->Canvas do

{ Brush->Color = clWhite;

Pen->Color = clBlack;

Rectangle(0, 0, Width, Height); }

BilliardTable->Ball->Clear;

BilliardTable->Lose->Clear;

balls = -1;

new(b);

inc(balls);

b->x = BilliardTable->Width / 4 + BilliardTable->Left;

b->y = BilliardTable->Height / 2 + BilliardTable->Top;

b->R = ballSize;//Random(20)+10;

b->col = clLtGray;//Random(clWhite);

b->dx = Random*2-1;

b->dy = Random*2-1;

b->ID = 0;

b->exist = True;

BilliardTable->Ball->Add(b);

PlayerN[0]->balls = 0;

PlayerN[1]->balls = 0;

GameStatus = gsGame;

loses = -1;

for( j = 0; j <=1; j ++)

for( i = -1; i <=1; i ++)

{ new(luza);

inc(loses);

luza->x = BilliardTable->Left + (BilliardTable->Width / 2)*(i+1);

luza->y = BilliardTable->Top + (BilliardTable->Height / 2)*(j*2);

luza->R = loseSize;

if(abs(i)==1 )

{ luza->x = trunc(luza->x - i*luza->R*sqrt(2)/2);

luza->y = trunc(luza->y -(j*2-1)*luza->R*sqrt(2)/2); }

luza->ballsInside = 0;

BilliardTable->Lose->Add(luza); }

for( j = 1; j <=PyramidHeight; j ++)

for( i = 1; i <=j; i ++)

{ new(b);

inc(balls);

b->R = ballSize; //Random(20)+10;

b->col = clLtGray; //Random(clWhite);//clLtGray;

b->dx = Random*2-1;

b->dy = Random*2-1;

b->ID = balls;

b->exist = True;

if(j % 2 != 0

then b->y = -((j-1) / 2)*2*b->R+(i-1)*2*b->R + H / 2

else b->y = -((j-1) / 2)*2*b->R+(i-1)*2*b->R - b->R + H / 2;

b->y = b->y + dh;

b->x = (j-1)*2*b->R + 3 * BilliardTable->Width / 4 + dw + LoseSize;

BilliardTable->Ball->Add(b); }

inc(Balls);

BilliardTable->Cue->ToBall = BilliardTable->Ball->Items[0];

BilliardTable->Cue->angle = 180*Pi/180;

BilliardTable->Cue->visible = False;

StopAll;

void __fastcall TForm1::FormCreate(TObject *Sender)

Create(ExtractFilePath(ParamStr(0))+"\settings.ini");">{ INI = TIniFile->Create(ExtractFilePath(ParamStr(0))+"\settings.ini");

ballSize = INI->Readint ("Phisics", "ballSize", 10);

loseSize = INI->Readint ("Phisics", "PocketSize", ballSize + 5);

MaxEnergy = INI->Readint ("Phisics", "MaxEnergy", 20);

CueLength = INI-

s