ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 16.09.2024

Просмотров: 23

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

}

// Calculate Score

Score = Score + ScoreAdd + DropHeight;

pScore->Caption = AnsiString(Score);

int NextPiece;

do {

NextPiece = (rand() % Piecetype) + 1;

} while (Piece->Kind == NextPiece);

NewPiece(NextPiece);

return;

}bool __fastcall TfrmMain::OnBottom(void) {

if((Piece->Top + 25) > Board->Height - Piece->Height) return(true); // Board Bottom

return(Collision(0));

}

bool __fastcall TfrmMain::Collision(int direction) {

// Direction: 0 = bottom, 1 = Left, 2 = Right, 3 = Rotation

int FirstBlock = ((Piece->Top / 25) * BoardWidth) + (Piece->Left / 25);

switch(direction) {

case 0: FirstBlock += BoardWidth; break;

case 1: FirstBlock--; break;

case 2: FirstBlock++; break;

case 3: FirstBlock = FirstBlock;

}

int Px = Piece->Height / 25;

int Py = Piece->Width / 25;

int Tile;

for(int x = 0; x < Px; x++) for(int y = 0; y < Py; y++) {

Tile = FirstBlock + (x * BoardWidth) + y;

if(Tile < 0) continue;

if(BoardTile[Tile]->Color == BaseColor) continue;

if(Piece->Divs[x * Py + y]->Color == BaseColor) continue;

return(true);

}

return(false);

}

void __fastcall TfrmMain::sbRotateClick(TObject *Sender) {

if(!Piece) return;

sndPlaySound("Rotate.wav", SND_ASYNC | SND_FILENAME);

PieceTimer->Interval = TimeLevel;

Piece->Rotate(0);

if(Collision(3)) { Piece->Rotate(0); Piece->Rotate(0); Piece->Rotate(0); }

if(Piece->Left < 0) Piece->Left = 0;

if(Piece->Left + Piece->Width > Board->Width) Piece->Left = Board->Width - Piece->Width;

return;

}

void __fastcall TfrmMain::btnStartClick(TObject *Sender) {

for(int x = 0; x < MaxTiles; x++) ShowTile(x, clGray, debug);

Score = 0;

pScore->Caption = Score;

PieceCount = 0;

SkillLevel = 1;

TimeLevel = SkillSpd[SkillLevel - 1];

test->Caption = "Level " + AnsiString(SkillLevel);

NewPiece((rand() % Piecetype) + 1);

return;

}

void __fastcall TfrmMain::ShowTile(int x, TColor TileColor, bool Seen) {

if(x < 0) return;

if(Seen) {

BoardTile[x]->Color = TileColor;

BoardTile[x]->BevelInner = bvRaised;

BoardTile[x]->BevelOuter = bvLowered;

BoardTile[x]->BringToFront();

return;

}

BoardTile[x]->Color = BaseColor;

BoardTile[x]->BevelInner = bvNone;

BoardTile[x]->BevelOuter = bvNone;

BoardTile[x]->SendToBack();

return;}void __fastcall TfrmMain::NewPiece(int Type) {

if(xNextPiece == 0) {

Piece = new tPiece(this, Type, BaseColor);

nPiece = new tPiece(this, Type, BaseColor);

xNextPiece = (rand() % Piecetype) + 1;

}delete(Piece);

delete(nPiece);

Piece = new tPiece(this, xNextPiece, BaseColor);

nPiece = new tPiece(this, Type, BaseColor);

xNextPiece = Type;

DropHeight = 0;

PieceTimer->Enabled = true;

PieceTimer->Interval = TimeLevel;

Board->InsertControl(Piece);

Piece->Left = (Board->Width - Piece->Width) / 2;

Piece->Left -= (Piece->Left % 25);

Piece->Top -= Piece->Height;

pNextPiece->InsertControl(nPiece);

nPiece->Left = (pNextPiece->Width - nPiece->Width) / 2;

nPiece->Top = (pNextPiece->Height - nPiece->Height) / 2;;

int count = 0;

for(int x = 0; x < MaxTiles; x++) {

bool show = (BoardTile[x]->Color != BaseColor);

if(show) { BoardTile[x]->BringToFront(); count = 0; }

if(count > BoardWidth) break;

//BoardTile[x]->SendToBack();

}return;

}void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action) {


if(Piece != NULL) delete(Piece);

void __fastcall TfrmMain::btnPauseClick(TObject *Sender) {

if(!Piece) return;

PieceTimer->Enabled = !PieceTimer->Enabled;

if(PieceTimer->Enabled) {

btnPause->Caption = "Pause";

sbRotate->Enabled = true;

sbLeft->Enabled = true;

sbRight->Enabled = true;

sbDrop->Enabled = true;

return;

}btnPause->Caption = "Continue";

sbRotate->Enabled = false;

sbLeft->Enabled = false;

sbRight->Enabled = false;

sbDrop->Enabled = false;return;

}void __fastcall TfrmMain::sbLeftClick(TObject *Sender) {

if(!Piece) return;

DropHeight = 0;

PieceTimer->Interval = TimeLevel;

if(Piece->Left > 0)

if(!Collision(1)) Piece->Left-=25;

return;

}void __fastcall TfrmMain::sbRightClick(TObject *Sender) {

if(!Piece) return;

DropHeight = 0;

PieceTimer->Interval = TimeLevel;

if(Piece->Left < Board->Width - Piece->Width)

if(!Collision(2)) Piece->Left+=25;

return;

}void __fastcall TfrmMain::sbDropClick(TObject *Sender) {

if(!Piece) return;

sndPlaySound("Drop.wav", SND_ASYNC | SND_FILENAME);

DropHeight = (Board->Height - Piece->Top) / 25;

PieceTimer->Interval = 25;

return;

}void __fastcall TfrmMain::Time(TObject *Sender) {

// sndPlaySound("Tick.wav", SND_ASYNC | SND_FILENAME); // Annoying !!!!!

// Determine if piece is on the Bottom

if(OnBottom()) {StickPiece();

return;

} // Lower the piece 1 square or 25 Pixels

Piece->Top+=25;

return;

}void __fastcall TfrmMain::FeatureTestButtonClick(TObject *Sender) {

sndPlaySound("Rotate.wav", SND_ASYNC | SND_FILENAME);

// play(BaseFolder + "\\Rotate.wav")

// AnsiString BaseFolder = ExtractFilePath(Application->ExeName);

// mPlay->FileName = BaseFolder + "\\Rotate.wav"; //specify video file

// mPlay->DeviceType = dtWaveAudio;

// mPlay->Open();

// mPlay->Play();

return;

}void __fastcall TfrmMain::FormKeyUp(TObject *Sender, WORD &Key,

TShiftState Shift)

{if (vubor==0) { sbLeft->Enabled=false; btnPause->Enabled=false;

btnStart->Enabled=false; BitBtn1->Enabled=false;

sbRight->Enabled=false; sbDrop->Enabled=false;

sbRotate->Enabled=false; }

if (Key==VK_LEFT) sbLeftClick(this);

if (Key=='P') btnPauseClick(this);

if (Key=='S') btnStartClick(this);

if (Key==VK_ESCAPE) Close();

if (Key==VK_RIGHT) sbRightClick(this);

if (Key==VK_DOWN) sbDropClick(this);

if (Key==VK_UP) sbRotateClick(this);

if (vubor==1){

sbLeft->Enabled=true; btnPause->Enabled=true;

btnStart->Enabled=true; BitBtn1->Enabled=true;

sbRight->Enabled=true; sbDrop->Enabled=true;

sbRotate->Enabled=true;

}}

void __fastcall TfrmMain::N3Click(TObject *Sender)

{frmMain->Close();

}void __fastcall TfrmMain::N2Click(TObject *Sender)

{Form1->Visible=true;

}void __fastcall TfrmMain::N4Click(TObject *Sender)

{AboutBox->Show();

}void __fastcall TfrmMain::N5Click(TObject *Sender)

{

for(int x = 0; x < MaxTiles; x++) ShowTile(x, clGray, debug);

Score = 0;

pScore->Caption = Score;

PieceCount = 0;

SkillLevel = 1;

TimeLevel = SkillSpd[SkillLevel - 1];

test->Caption = "Level " + AnsiString(SkillLevel);

NewPiece((rand() % Piecetype) + 1);

return;

}

void __fastcall TfrmMain::N6Click(TObject *Sender)

{

frmMain->Close();

}


Додаток б Лістинг модуляPiece

#include <vcl.h>

#pragma hdrstop

#include "Piece.h"

#pragma package(smart_init)

static inline void ValidCtrCheck(tPiece *) {

new tPiece(NULL, 0, clBlack);

return;

}

__fastcall tPiece::tPiece(TComponent* Owner, int PieceType, TColor Background) : TPanel(Owner) {

BevelInner = bvNone;

BevelOuter = bvNone;

BackColor = Background;

Kind = PieceType;

switch (Kind) {

case 1:

case 2: Color = clRed; // 1. Square

Height = 25 * 2; // XX

Width = 25 * 2; // XX

break;

case 3: Width = 25 * 3;

case 4: Color = clBlue; // 2. Line 2-4

Height = 25; // XXxx

if(Kind == 4) Width = 25 * 4;

break;

case 5:

case 6: Color = clFuchsia; // 3. Half Cross

Height = 25 * 2; // 0X2 X1 XXX 0X

Width = 25 * 3; // XXX XX 3X5 XX

break; // X5 4X

case 7:

case 8: Color = clGreen; // 4. Right Hitch 5. Left Hitch

Height = 25 * 2; // 0XX X1 XX2 0X

Width = 25 * 3; // XX5 XX 3XX XX

break; // 4X X5

case 9:

case 10: Color = clTeal; // 6. Right "L" 7. Left "L"

Height = 25 * 3; // XX XXX 0X X12 XX 01X X1 XXX

Width = 25 * 2; // X3 34X 2X XXX 2X XXX X3 X45

break; // X5 XX 4X XX

Default : Color = clWhite; break;

}

Blocks = (Width / 25) * (Height / 25);

for(int x = 0; x < Blocks; x++) {

Divs[x] = new TPanel(this);

Divs[x]->Height = 25;

Divs[x]->Width = 25;

Block(x, true);

this->InsertControl(Divs[x]);

}

switch (Kind) {

case 5:

case 6: Block(0, false); Block(2, false); break;

case 7: Block(0, false); Block(5, false); break;

case 8: Block(2, false); Block(3, false); break;

case 9: Block(3, false); Block(5, false); break;

case 10: Block(2, false); Block(4, false); break;

}

int x = (rand() % 7) + 1; // 1-7

for(int y = 0; y < x; y++) Rotate(0);

}

void __fastcall tPiece::Rotate(int Direction) {

int Temp;

// Direction: 0=Right, 1 = Left

switch (Kind) {

case 1:

case 2: Arrange(); break;

case 3:

case 4: Temp = Height; Height = Width; Width = Temp; Arrange(); break;

case 5:

case 6: Temp = Height; Height = Width; Width = Temp;

if(Divs[2]->Color == BackColor) { Arrange(); Block(1, false); Block(5, false); break; }

if(Divs[1]->Color == BackColor) { Arrange(); Block(3, false); Block(5, false); break; }

if(Divs[3]->Color == BackColor) { Arrange(); Block(0, false); Block(4, false); break; }

if(Divs[4]->Color == BackColor) { Arrange(); Block(0, false); Block(2, false); break; }

case 7: Temp = Height; Height = Width; Width = Temp;

if(Divs[0]->Color == BackColor) { Arrange(); Block(1, false); Block(4, false); break; }


if(Divs[1]->Color == BackColor) { Arrange(); Block(0, false); Block(5, false); break; }

case 8: Temp = Height; Height = Width; Width = Temp;

if(Divs[2]->Color == BackColor) { Arrange(); Block(0, false); Block(5, false); break; }

if(Divs[0]->Color == BackColor) { Arrange(); Block(2, false); Block(3, false); break; }

case 9: Temp = Height; Height = Width; Width = Temp;

if(Divs[5]->Color == BackColor) { Arrange(); Block(3, false); Block(4, false); break; }

if(Divs[4]->Color == BackColor) { Arrange(); Block(0, false); Block(2, false); break; }

if(Divs[0]->Color == BackColor) { Arrange(); Block(1, false); Block(2, false); break; }

if(Divs[1]->Color == BackColor) { Arrange(); Block(3, false); Block(5, false); break; }

case 10: Temp = Height; Height = Width; Width = Temp;

if(Divs[2]->Color == BackColor) { Arrange(); Block(1, false); Block(0, false); break; }

if(Divs[0]->Color == BackColor) { Arrange(); Block(3, false); Block(1, false); break; }

if(Divs[3]->Color == BackColor) { Arrange(); Block(5, false); Block(4, false); break; }

if(Divs[5]->Color == BackColor) { Arrange(); Block(4, false); Block(2, false); break; }

Default : break;

}

return;}

void __fastcall tPiece::Block(int x, bool Seen) {

if(Seen) {

Divs[x]->Color = Color;

Divs[x]->BevelInner = bvRaised;

Divs[x]->BevelOuter = bvLowered;

return; }

Divs[x]->Color = BackColor;

Divs[x]->BevelInner = bvNone;

Divs[x]->BevelOuter = bvNone;

return;

}

void __fastcall tPiece::Arrange(void) {

for(int x = 0; x < Blocks; x++) {

Block(x, true);

div_t N = div(x, (Width / 25));

Divs[x]->Left = N.rem * 25;

Divs[x]->Top = N.quot * 25;

// Divs[x]->Caption = x;

} return;

} this->ReadFromFile(Form1->Memo2);

return; }

this->F=fopen("info.txt", "r");

while(!feof(this->F))

{ fscanf(F,"%s ",&this->temp);


Додаток в Лістинг модуля Unit1

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

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

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

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

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

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

{

Key=0;

}

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

Додаток г Лістинг модуля Unit2

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

#include <vcl.h>

#pragma hdrstop

#include "Unit2.h"

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

#pragma resource "*.dfm"

TAboutBox *AboutBox;

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

__fastcall TAboutBox::TAboutBox(TComponent* AOwner)

: TForm(AOwner)

{

}

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

void __fastcall TAboutBox::OKButtonClick(TObject *Sender)

{

AboutBox->Close();

}

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