//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"
#include <stdlib.h>
#include <math.h>
#include <mmsystem.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TTetrisForm *TetrisForm;
//---------------------------------------------------------------------------
__fastcall TTetrisForm::TTetrisForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TTetrisForm::FormCreate(TObject *Sender)
{
Bevel3->Top= Tp-FieldWidth;
Bevel3->Left= Lft-FieldWidth;
Bevel3->Width= GlassWidth*NormalWidth+FieldWidth*2;
Bevel3->Height= GlassHeight*NormalHeight+FieldWidth*2;
Image->Top=Tp;
Image->Left=Lft;
Image->Width=GlassWidth*NormalWidth;
Image->Height=GlassHeight*NormalHeight;
BGImage->Width=Image->Width;
BGImage->Height=Image->Height;
ClientWidth= Bevel3->Width+FieldWidth*3+Button1->Width;
ClientHeight= Bevel3->Height+FieldWidth*2;
Button1->Left = Bevel3->Width+FieldWidth*2;
Button2->Left = Button1->Left;
Button3->Left = Button1->Left;
Button5->Left = Button1->Left;
Button4->Left = Button1->Left;
Label1->Left= Bevel3->Width+FieldWidth*2;
Label2->Left= Label1->Left;
Bevel1->Left= Label1->Left;
Bevel1->Width= Button1->Width;
Bevel2->Left= Label1->Left;
Bevel2->Width= Button1->Width;
Label3->Left = Bevel1->Left+FieldWidth;
Label4->Left = Bevel1->Left+FieldWidth;
Bevel4->Top = Button5->Top+Button5->Height+4;
Bevel4->Left = Button5->Left;
Bevel4->Height = SmallHeight*4+4;
Bevel4->Width =SmallWidth*4+8;
NextImage->Top = Button5->Top+Button5->Height+6;
NextImage->Left = Button5->Left+4;
NextImage->Height = SmallHeight*4;
NextImage->Width =SmallWidth*4;
randomize();
ReadStyle();
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
ClearSmallBlock(i,j);
ReadIni();
LoadImage(ImageFileName);
isPlaying=false;
Timer1->Enabled=false;
MediaPlayer1->FileName=ExtractFilePath(Application->ExeName)
+"\\"+"BGSound.mid";
MediaPlayer1->Open();
ConnectJoy();
}
//---------------------------------------------------------------------------
void __fastcall TTetrisForm::DrawBlock(int X, int Y, TColor mColor)
{
int X1,Y1,X2,Y2;
X1 = X*NormalWidth;
X2 = X1+NormalWidth;
Y1 = Y*NormalHeight;
Y2 = Y1+NormalHeight;
Image->Canvas->Brush->Color=mColor;
Image->Canvas->FillRect(Rect(X1+1,Y1+1,X2-1,Y2-1));
Image->Canvas->Pen->Color = clGray;
Image->Canvas->MoveTo(X1,Y1);
Image->Canvas->LineTo(X1,Y2-1);
Image->Canvas->LineTo(X2-1,Y2-1);
Image->Canvas->Pen->Color = clWhite;
Image->Canvas->LineTo(X2-1,Y1);
Image->Canvas->LineTo(X1,Y1);
}
void __fastcall TTetrisForm::ClearBlock(int X, int Y)
{
int X1,Y1,X2,Y2;
X1 = X*NormalWidth;
X2 = X1+NormalWidth;
Y1 = Y*NormalHeight;
Y2 = Y1+NormalHeight;
Image->Canvas->CopyRect(Rect(X1,Y1,X2,Y2),
BGImage->Canvas,Rect(X1,Y1,X2,Y2));
}
void __fastcall TTetrisForm::Button1Click(TObject *Sender)
{
Start();
Button1->Enabled=false;
Button2->Enabled=true;
Button3->Enabled=true;
N2->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TTetrisForm::DrawSmallBlock(int X, int Y, TColor mColor)
{
int X1,X2,Y1,Y2;
X1 = X*SmallWidth;
X2 = X1+SmallWidth;
Y1 = Y*SmallHeight;
Y2 = Y1+SmallHeight;
NextImage->Canvas->Brush->Color = mColor;
NextImage->Canvas->FillRect(Rect(X1+1,Y1+1,X2-1,Y2-1));
NextImage->Canvas->Pen->Color = clGray;
NextImage->Canvas->MoveTo(X1,Y1);
NextImage->Canvas->LineTo(X1,Y2-1);
NextImage->Canvas->LineTo(X2-1,Y2-1);
NextImage->Canvas->Pen->Color = clWhite;
NextImage->Canvas->LineTo(X2-1,Y1);
NextImage->Canvas->LineTo(X1,Y1);
}
void __fastcall TTetrisForm::ClearSmallBlock(int X, int Y)
{
int X1,X2,Y1,Y2;
X1 = X*SmallWidth;
X2 = X1+SmallWidth;
Y1 = Y*SmallHeight;
Y2 = Y1+SmallHeight;
NextImage->Canvas->Brush->Color = clBlack;
NextImage->Canvas->FillRect(Rect(X1,Y1,X2,Y2));
}
void TTetrisForm::ReadStyle()
{
for (int i=0; i<21; i++)
for (int j=0; j<40; j++)
if (Tetris[i][j]=='-')
Style[i][j]=0;
else Style[i][j]=Tetris[i][j]-'0';
}
void TTetrisForm::WriteNext()
{
NextPatn=floor(random(21));
NextRotat = floor(random(4));
NextColor = floor(random(7));
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
ClearSmallBlock(i,j);
for (int i=0;i<4;i++)
DrawSmallBlock(Style[NextPatn][NextRotat*10+i*2]-1,
Style[NextPatn][NextRotat*10+i*2+1]-1,
Colors[NextColor]);
}
void __fastcall TTetrisForm::WriteObject()
{
if(MoveFlag)
for (int i=0;i<4;i++)
ClearBlock(OldX[i],OldY[i]);
MoveFlag=true;
for (int i=0;i<4;i++){
OldX[i]=ObjectX+Style[Patn][Rotat*10+i*2]-1;
OldY[i]=ObjectY+Style[Patn][Rotat*10+i*2+1]-1;
DrawBlock(OldX[i],OldY[i],Colors[ObjectColor]);
}
}
void __fastcall TTetrisForm::Start()
{
if(PlayMusic)
MediaPlayer1->Play();
for (int i=0;i<GlassWidth;i++)
for (int j=0;j<GlassHeight;j++){
GlassMap[i][j]=0;
ClearBlock(i,j);
}
Level = 1;
Score = 0;
Label3->Caption="1";
Label4->Caption="0";
MoveFlag=false;
ObjectX=3; ObjectY=0;
WriteNext();
Patn = NextPatn;
ObjectColor = NextColor;
Rotat=NextRotat;
WriteObject();
WriteNext();
isPlaying=true;
Timer1->Interval=800-Level*100;
Timer1->Enabled=true;
}
void __fastcall TTetrisForm::MoveRight()
{
int xx=Style[Patn][Rotat*10]+ObjectX;
if (xx <10){
xx=Style[Patn][Rotat*10+2]+ObjectX;
if (xx <10){
xx=Style[Patn][Rotat*10+4]+ObjectX;
if (xx <10){
xx=Style[Patn][Rotat*10+6]+ObjectX;
if (xx <10){
if (HitCheck(ObjectX+1,ObjectY,Rotat)){
ObjectX++;
WriteObject();
}
}
}
}
}
}
void __fastcall TTetrisForm::MoveLeft()
{
int xx=Style[Patn][Rotat*10]+ObjectX-2;
if (xx >=0){
xx=Style[Patn][Rotat*10+2]+ObjectX-2;
if (xx >=0){
xx=Style[Patn][Rotat*10+4]+ObjectX-2;
if (xx >=0){
xx=Style[Patn][Rotat*10+6]+ObjectX-2;
if (xx >=0){
if (HitCheck(ObjectX-1,ObjectY,Rotat)){
ObjectX--;
WriteObject();
}
}
}
}
}
}
bool __fastcall TTetrisForm::HitCheck(int X, int Y,int Ro)
{
int xx,yy;
for (int i=0;i<4;i++){
xx=X+Style[Patn][Ro*10+i*2]-1;
yy=Y+Style[Patn][Ro*10+i*2+1]-1;
if(GlassMap[xx][yy]!=0) return false;
}
return true;
}
void __fastcall TTetrisForm::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (!isPlaying) return;
switch(Key){
case VK_LEFT:
MoveLeft();
break;
case VK_RIGHT:
MoveRight();
break;
case VK_UP:
Rotate();
break;
case VK_DOWN:
MoveDown();
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TTetrisFo
评论0