1. 程式人生 > >C語言-控制檯俄羅斯方塊

C語言-控制檯俄羅斯方塊

/*
//
定義了一個RandBlock[4][4]用來存方塊
BlockGroup[][]用來繪製介面
//
*/
#include <conio.h>
#include<iostream>
#include<time.h>
#include<windows.h>
#include <stdlib.h>
using namespace std;


#define Level 100//初始難度;
#define Score 0//初始分數;
const int WallWide_x = 20;//牆的寬度
const int WallLength_y = 28;//牆的長度
int TempGroup[4][4];//用於旋轉時存放方塊
int BlockGroup[WallWide_x][WallLength_y];//定義一個數組用來顯示介面,及初始化背景
int RandBlock[2][4];//定義一個數組用來存方塊
int main()
{
void ShowFirstWindow();
void CreateBlock(int RandBlock[2][4]);//建立隨機方塊
void GotoXY(int, int);//設定游標位置
void PutBlockToBack();//把方塊放到背景裡面去
void BlockMoveDown();
void ShowGame();
//控制方塊移動
void BlockMoveLeft();
void BlockMoveRight();
void BlockRotate();
void BlockMoveDownFast();
//
srand((unsigned(time(NULL))));//以時間作為種子產生隨機數
for (int i = 0; i < WallLength_y; i++)//y座標
{
BlockGroup[0][i] = 1;//初始化最左邊的牆
for (int j = 1; j < WallWide_x; j++)//x座標
{
if (i == 0)//初始化最上面的牆
{
BlockGroup[j][0] = 1;
}
else
{
if (i == WallLength_y - 1)//初始化最下面的牆
{
BlockGroup[j][WallLength_y - 1] = 1;
}
else
BlockGroup[j][i] = 0;
}
}
BlockGroup[WallWide_x - 1][i] = 1;//初始化最右邊的牆


}


CreateBlock(RandBlock);//先產生方塊
PutBlockToBack();//把方塊放到背景裡面去
ShowFirstWindow();//顯示遊戲開始介面;
GotoXY(1, 1);
while(1!=BlockGroup[9][3])//第2行值為1代表方塊堆滿了,遊戲結束
{
BlockMoveDown();//方塊下落
ShowGame();//判斷方塊能否下落,以及處理方塊疊加
while(_kbhit())
{
char a = _getch();
switch(a)
{
case 'a':BlockMoveLeft();break;
case 'd':BlockMoveRight();break;
case 'w':BlockRotate();break;
}
}
GotoXY(1, 1);
Sleep(Level);
}
GotoXY(10, 10);//顯示遊戲結束
cout<<"GAME OVER!"<<endl;
GotoXY(1, 29);
return 0;
}


///////////////////////////////////////////////////
///////////////設定游標位置////////////////////////
///////////////////////////////////////////////////
void GotoXY(int x, int y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}




//產生隨機方塊
void CreateBlock(int RandBlock[2][4])
{
int BlockType = rand() % 5;
switch (BlockType)
{
case 0:
{
RandBlock[0][0] = 0, RandBlock[0][1] = 2, RandBlock[0][2] = 2, RandBlock[0][3] = 0;
RandBlock[1][0] = 2, RandBlock[1][1] = 2, RandBlock[1][2] = 0, RandBlock[1][3] = 0;
break;
}
case 1:
{
RandBlock[0][0] = 0, RandBlock[0][1] = 2, RandBlock[0][2] = 2, RandBlock[0][3] = 0;
RandBlock[1][0] = 0, RandBlock[1][1] = 2, RandBlock[1][2] = 2, RandBlock[1][3] = 0;
break;
}
case 2:
{
RandBlock[0][0] = 0, RandBlock[0][1] = 2, RandBlock[0][2] = 0, RandBlock[0][3] = 0;
RandBlock[1][0] = 2, RandBlock[1][1] = 2, RandBlock[1][2] = 2, RandBlock[1][3] = 0;
break;
}
case 3:
{
RandBlock[0][0] = 2, RandBlock[0][1] = 0, RandBlock[0][2] = 0, RandBlock[0][3] = 0;
RandBlock[1][0] = 2, RandBlock[1][1] = 2, RandBlock[1][2] = 2, RandBlock[1][3] = 0;
break;
}
case 4:
{
RandBlock[0][0] = 2, RandBlock[0][1] = 2, RandBlock[0][2] = 2, RandBlock[0][3] = 2;
RandBlock[1][0] = 0, RandBlock[1][1] = 0, RandBlock[1][2] = 0, RandBlock[1][3] = 0;
break;
}
}
}




//把產生隨機方塊陣列中的方塊放到背景中去
void PutBlockToBack()
{
for(int i = 0; i<2; i++)
for(int j = 0; j<4; j++)
{
BlockGroup[j+8][i+1] = RandBlock[i][j];
}
}




//方塊向下移動
void BlockMoveDown()
{
for(int i = WallLength_y-1; i>0; i--)
for(int j = 0; j<WallWide_x; j++)
if(2==BlockGroup[j][i])
{
BlockGroup[j][i+1] = BlockGroup[j][i];
BlockGroup[j][i] = 0;
}
}




//進入遊戲顯示遊戲介面
void ShowFirstWindow()
{
GotoXY(24, 6);
cout<<"俄羅斯方塊";


GotoXY(24, 8);
cout<<"版本1.0";


GotoXY(24, 10);
cout<<"按enter檢視遊戲說明!";


_getch();
system("cls");


GotoXY(24, 6);
cout<<"遊戲說明";


GotoXY(24, 8);
cout<<"W:旋轉方塊, S:讓方塊直接下落, A:方塊左移, D:方塊右移";


GotoXY(24, 10);
cout<<"按enter進入遊戲!";
_getch();
system("cls");
}




//方塊左移
void BlockMoveLeft()
{
for(int j = 0; j<WallLength_y; j++)
{
for(int i = 1; i<WallWide_x; i++)
{
if(2 == BlockGroup[i][j])
{
if(1 != BlockGroup[i-1][j])
{
BlockGroup[i-1][j] = BlockGroup[i][j];
BlockGroup[i][j] = 0;
}
}
}
}
}




//方塊右移
void BlockMoveRight()
{
for(int j = 0; j<WallLength_y; j++)
{
for(int i = WallWide_x-1; i>0; i--)
{
if(2 == BlockGroup[i][j])
{
if(1 != BlockGroup[i+1][j])
{
BlockGroup[i+1][j] = BlockGroup[i][j];
BlockGroup[i][j] = 0;
}
}
}
}
}




//方塊旋轉
void  BlockRotate()
{
for(int j = 0; j<WallLength_y; j++)
{
for(int i = 0; i<WallWide_x; i++)
{
if(2 == BlockGroup[i][j])
{
for(int k = 0; k<4; k++)
{
for(int f = 0; f<4; f++)
{
TempGroup[k][f] = BlockGroup[i][j-2];
}
}
}
}
}


int TempBlockGroup[WallWide_x][WallLength_y];
for(int i = 0; i<WallLength_y; i++)
{
for(int j = 0; j<WallWide_x; j++)
{
TempBlockGroup[j][i] = BlockGroup[j][i];
}
}


for(i = 0; i<WallLength_y; i++)
{
for(int j = 0; j<WallWide_x; j++)
{
BlockGroup[i][j] = TempBlockGroup[j][i];


}
}
}




//方塊快速下落
void BlockMoveDownFast()
{
}




//判斷方塊能否下落,以及處理方塊疊加
void ShowGame()
{
for (int i = 0; i < WallLength_y; i++)
{
for(int j = 0; j<WallWide_x; j++)
{
if (BlockGroup[j][i] == 0)
cout << "  ";
else
{
if (BlockGroup[j][i] == 2)
{
if(1==BlockGroup[j][i+1] || 1==BlockGroup[j+1][i+1] || 1==BlockGroup[j+2][i+1] || 1==BlockGroup[j+3][i+1])
{
{
BlockGroup[j][i]=1;
if(2 == BlockGroup[j+1][i])
BlockGroup[j+1][i]=1;
if(2 == BlockGroup[j+2][i])
BlockGroup[j+2][i]=1;
if(2 == BlockGroup[j+3][i])
BlockGroup[j+3][i]=1;
if(2==BlockGroup[j][i-1])
BlockGroup[j][i-1]=1;
if(2==BlockGroup[j+1][i-1])
BlockGroup[j+1][i-1]=1;
if(2==BlockGroup[j+2][i-1])
BlockGroup[j+2][i-1]=1;
if(2==BlockGroup[j+3][i-1])
BlockGroup[j+3][i-1]=1;
if(2==BlockGroup[j+4][i-1])
BlockGroup[j+4][i-1]=1;
if(2==BlockGroup[j+5][i-1])
BlockGroup[j+5][i-1]=1;
}
cout << "█";
CreateBlock(RandBlock);//先產生方塊
PutBlockToBack();//把方塊放到背景裡面去
}
else
cout<<"□";
}
else
cout << "█";
}
}
cout << endl;
}
}

有諸多BUG,沒有消除滿行的。沒有旋轉的,希望哪位大神就此程式運行了幫忙指點下。