⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 经典游戏俄罗斯方块的制作程序,想成为游戏制作高手,次程序不能不看
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#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 TTetrisForm::Rotate()
{
     int Ro=(Rotat+1)%4;
     int xx,yy;
     for (int i=0;i<4;i++){
          xx=ObjectX+Style[Patn][Ro*10+i*2]-1;
          yy=ObjectY+Style[Patn][Ro*10+i*2+1]-1;
          if (xx>9||xx<0||yy>22||yy<0) return;
     }
     if (HitCheck(ObjectX,ObjectY,Ro)){
          Rotat=Ro;
          WriteObject();
     }
}


void __fastcall TTetrisForm::MoveDown()
{
     bool chk=false;
     int xx,yy=Style[Patn][Rotat*10+1]+ObjectY;
     if (yy<23){
     yy=Style[Patn][Rotat*10+3]+ObjectY;
     if (yy<23){
     yy=Style[Patn][Rotat*10+5]+ObjectY;
     if (yy<23){
     yy=Style[Patn][Rotat*10+7]+ObjectY;
     if (yy<23){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -