#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include <graphics.h>
#define ROW_AR 28
#define COL_AR 14
#define BLOCK_X 4
#define BLOCK_Y 2
#define originallocationX 7
#define originallocationY 7
int start, end,NUM=0;
char p,l,x,L,T,D,L2,l1,x1,L1,T1,L21,v;
enum m
{
BLock_null,
BLock_Frame,
BLock_BL,
};
enum BLock
{
row,
col
};
int MapArr[ROW_AR][COL_AR] = { 0 };
char BlockLt[BLOCK_X][BLOCK_Y] = { 0 };
int GameOver()
{
for (int i = 0; i < BLOCK_X; ++i)
{
if (BlockLt[i][row] < 8)
{
return 1;
}
}
return 0;
}
void Score()//得分
{
int F;
for (F = ROW_AR-2; F >= 8; F--)
{
p = 'T';
for (int G = 1; G < COL_AR-1; G++)
{
if (MapArr[F][G] == BLock_null)
{
p = 'F';
break;
}
}
if (p == 'T')
{
for (int i =F; i >= 8; i--)
{
for (int j = 1; j < COL_AR-1; j++)
{
MapArr[i][j] = MapArr[i-1][j];
}
}
NUM++;
F++;
}
}
}
char IsBlockchange()//判断是否能变形
{
p = 'T';
switch (x)
{
case 0://长条状
if (l == 0)
{
if (MapArr[BlockLt[1][row] - 1][BlockLt[1][col]] != BLock_null || MapArr[BlockLt[1][row] + 1][BlockLt[1][col]] != BLock_null || MapArr[BlockLt[1][row] + 2][BlockLt[1][col]] != BLock_null)
p = 'F';
}
else if (l == 1)
{
if (MapArr[BlockLt[1][row]][BlockLt[1][col]-1] != BLock_null || MapArr[BlockLt[1][row]][BlockLt[1][col]+1] != BLock_null || MapArr[BlockLt[1][row]][BlockLt[1][col]+2] != BLock_null)
p = 'F';
}
break;
case 1://L状
switch (L)
{
case 0:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col] - 1] != BLock_null)
p = 'F';
break;
case 1:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col]] != BLock_null)
p = 'F';
break;
case 2:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
case 3:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] + 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
}
break;
case 2://反7状
switch (L2)
{
case 0:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
case 1:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row] + 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
case 2:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] + 1][BlockLt[2][col] - 1] != BLock_null)
p = 'F';
break;
case 3:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col] - 1] != BLock_null)
p = 'F';
break;
}
break;
case 3://T状
switch (T)
{
case 0:
if (MapArr[BlockLt[1][row]][BlockLt[1][col] + 1] != BLock_null)
p = 'F';
break;
case 1:
if (MapArr[BlockLt[1][row]+1][BlockLt[1][col]] != BLock_null)
p = 'F';
break;
case 2:
if (MapArr[BlockLt[1][row]][BlockLt[1][col] - 1] != BLock_null)
p = 'F';
break;
case 3:
if (MapArr[BlockLt[1][row]-1][BlockLt[1][col]] != BLock_null)
p = 'F';
break;
}
break;
}
return p;
}
void ChangeBlock()//块的变形操作
{
switch (x)
{
case 0://长条状
if (l == 0)
{
BlockLt[0][row] = BlockLt[1][row] - 1;
BlockLt[0][col] = BlockLt[1][col];
BlockLt[2][row] = BlockLt[1][row] + 1;
BlockLt[2][col] = BlockLt[1][col];
BlockLt[3][row] = BlockLt[1][row] + 2;
BlockLt[3][col] = BlockLt[1][col];
l = 1;
}
else if (l == 1)
{
BlockLt[0][row] = BlockLt[1][row];
BlockLt[0][col] = BlockLt[1][col] - 1;
BlockLt[2][row] = BlockLt[1][row];
BlockLt[2][col] = BlockLt[1][col] + 1;
BlockLt[3][row] = BlockLt[1][row];
BlockLt[3][col] = BlockLt[1][col] + 2;
l = 0;
}
break;
case 1://L状
switch (L)
{
case 0:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] - 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] + 1;
L = 1;
break;
case 1:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row] - 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] + 1;
BlockLt[3][col] = BlockLt[2][col];
L = 2;
break;
case 2:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] + 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] - 1;
L = 3;
break;
case 3:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row] + 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] - 1;
BlockLt[3][col] = BlockLt[2][col];
L = 0;
break;
}
break;
case 2://反7状
switch (L2)
{
case 0:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row] - 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] + 1;
BlockLt[3][col] = BlockLt[2][col];
L2 = 1;
break;
case 1:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] + 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] - 1;
L2 = 2;
break;
case 2:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row] + 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] - 1;
BlockLt[3][col] = BlockLt[2][col];
L2 = 3;
break;
case 3:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] - 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] + 1;
L2 = 0;
break;
}
break;
case 3://T状
switch (T)
{
case 0:
BlockLt[0][row] = BlockLt[1][row];
BlockLt[0][col] = BlockLt[1][col]-1;
BlockLt[2][row] = BlockLt[1][row];
BlockLt[2][col] = BlockLt[1][col]+1;
BlockLt[3][row] = BlockLt[1][row]-1;
BlockLt[3][col] = BlockLt[1][col];
T = 1;
break;
case 1:
BlockLt[0][row] = BlockLt[1][row]-1;
BlockLt[0][col] = BlockLt[1][col];
BlockLt[2][row] = BlockLt[1][row]+1;
BlockLt[2][col] = BlockLt[1][col];
BlockLt[3][row] = BlockLt[1][row];
BlockLt[3][col] = BlockLt[1][col]+1;
T = 2;
break;
case 2:
BlockLt[0][row] = BlockLt[1][row];
BlockLt[0][col] = BlockLt[1][col]+1;
BlockLt[2][row] = BlockLt[1][row];
BlockLt[2][col] = BlockLt[1][col]-1;
BlockLt[3][row] = BlockLt[1][row]+1;
BlockLt[3][col] = BlockLt[1][col];
T = 3;
break;
case 3:
BlockLt[0][row] = BlockLt[1][row]+1;
BlockLt[0][col] = BlockLt[1][col];
BlockLt[2][row] = BlockLt[1][row]-1;
BlockLt[2][col] = BlockLt[1][col];
BlockLt[3][row] = BlockLt[1][row];
BlockLt[3][col] = BlockLt[1][col]-1;
T = 0;
break;
}
break;
}
}
char Ismove(char n)//判断是否能下降和左右移动
{
p = 'T';
switch (n)
{
case 'L':
for (int i = 0; i < BLOCK_X; ++i)
{
if ((MapArr[BlockLt[i][row]][BlockLt[i][col] - 1] != BLock_null))
{
p = 'F';
break;
}
}
break;
case 'R':
for (int i = 0; i < BLOCK_X; ++i)
{
if ((MapArr[BlockLt[i][row]][BlockLt[i][col] + 1] != BLock_null))
{
p = 'F';
break;
}
}
break;
case 'D':
for (int i = 0; i < BLOCK_X; ++i)
{
if ((MapArr[BlockLt[i][row] + 1][BlockLt[i][col]] != BLock_null) && BlockLt[i][row] != 5)
{
p = 'F';
break;
}
}
break;
}
return p;
}
void Remove(int block)//复写操作
{
for (int i = 0; i < BLOCK_X; ++i)
{
if (BlockLt[i][row] == 6)
MapArr[BlockLt[i][row]][BlockLt[i][col]] = BLock_Frame;
else if ((BlockLt[i][row] > 6))
MapArr[BlockLt[i][row]][BlockLt[i][col]] = block;
}
}
char Key()//获取玩家操作
{
if (_kbhit())
{
return _getch();
}
return 0;
}
void BlockDown()//块下落
{
for (int i = 0; i < BLOCK_X; ++i)
{
BlockLt[i][row]++;
}
}
void BlockMoveAndChange()//块的运动
{
switch (Key())
{
case 'W':
case 'w':
if (IsBlockchange() == 'T')
ChangeBlock();
break;
case 'A':
case 'a':
if (Ismove('L') == 'T')
{
for (int i = 0; i < BLOCK_X; ++i)//左移
{
BlockLt[i][col]--;
}
}
break;
case 'S'://快速下
case 's':
if (Ismove('D') == 'T')
{
BlockDown();
}
break;
case 'D':
case 'd':
if (Ismove('R') == 'T')
{
for (int i = 0; i < BLOCK_X; ++i)//右移
{
BlockLt[i][col]++;
}
}
break;
}
}
void InitBlock(char x,char l,char L, char L2, char T ,char arr[][2],int m,int n)//初始化块
{
switch (x)
{
case 0://长条状,两种形态
switch (l)
{
case 0:
arr[0][row] = m;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n + 1;
arr[3][row] = m;
arr[3][col] = n + 2;
break;
case 1:
arr[0][row] = m - 1;
arr[0][col] = n;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m + 1;
arr[2][col] = n;
arr[3][row] = m + 2;
arr[3][col] = n;
break;
}
break;
case 1://L状,4种形态
switch (L)
{
case 0:
arr[0][row] = m + 1;
arr[0][col] = n + 1;
arr[1][row] = m + 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m - 1;
arr[3][col] = n;
break;
case 1:
arr[0][row] = m + 1;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n - 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n + 1;
break;
case 2:
arr[0][row] = m - 1;
arr[0][col] = n - 1;
arr[1][row] = m - 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m + 1;
arr[3][col] = n;
break;
case 3:
arr[0][row] = m - 1;
arr[0][col] = n + 1;
arr[1][row] = m;
arr[1][col] = n + 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n - 1;
break;
}
break;
case 2://反7 4种状态
switch (L2)
{
case 0:
arr[0][row] = m - 1;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n - 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n + 1;
break;
case 1:
arr[0][row] = m - 1;
arr[0][col] = n + 1;
arr[1][row] = m - 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m + 1;
arr[3][col] = n;
break;
case 2:
arr[0][row] = m + 1;
arr[0][col] = n + 1;
arr[1][row] = m;
arr[1][col] = n + 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n - 1;
break;
case 3:
arr[0][row] = m + 1;
arr[0][col] = n - 1;
arr[1][row] = m + 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m - 1;
arr[3][col] = n;
break;
}
break;
case 3://T状.四种形态
switch (T)
{
case 0:
arr[0][row] = m + 1;
arr[0][col] = n;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m - 1;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n - 1;
break;
case 1:
arr[0][row] = m;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n + 1;
arr[3][row] = m - 1;
arr[3][col] = n;
break;
case 2:
arr[0][row] = m - 1;
arr[0][col] = n;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m + 1;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n + 1;
break;
case 3:
arr[0][row] = m;
arr[0][col] = n + 1;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n - 1;
arr[3][row] = m + 1;
arr[3][col] = n;
break;
}
break;
case 4://田字状,一种形态
arr[0][row] = m;
arr[0][col] = originallocationY - 1;
arr[1][row] = m;
arr[1][col] = originallocationY;
arr[2][row] = m - 1;
arr[2][col] = originallocationY - 1;
arr[3][row] = m - 1;
arr[3][col] = originallocationY;
break;
}
}
void ShowNextMatch(int m, int n,int block)//显示当前块
{
char Show[BLOCK_X][BLOCK_Y] = { 0 };
InitBlock(x1,l1,L1,L21,T1,Show, m, n);
for (int i = 0; i < 4; ++i)
{
MapArr[Show[i][row]][Show[i][col]] = block;
}
}
void DraWing(int x,int y)//渲染
{
//for (int i = 0; i < x; ++i)
//{
// for (int j = 0; j < y; ++j)
// {
// switch (MapArr[i][j])
// {
// case 0:
// printf(" ");
// break;
// case 1:
// printf("█");
// break;
// case 2:
// printf("□");
// break;
// }
// }
// putchar('\n');
//}
BeginBatchDraw();
for (int i = 0; i < ROW_AR; ++i)
{
for (int j = 0; j < COL_AR; ++j)
{
switch (MapArr[i][j])
{
case BLock_null:
//printf(" ");
setfillcolor(RGB(0, 0, 0));
setlinecolor(RGB(0, 0, 0));
fillrectangle(j * 25, i * 25, (j + 1) * 25, (i + i) * 25);
break;
case BLock_Frame:
//printf("* ");
setfillcolor(RGB(0, 0, 255));
setlinecolor(RGB(255, 255, 255));
fillrectangle(j * 25, i * 25, (j + 1) * 25, (i + i) * 25);
break;
case BLock_BL:
//printf("# ");
setfillcolor(RGB(255, 0, 0));
setlinecolor(RGB(255, 255, 255));
fillrectangle(j * 25, i * 25, (j + 1) * 25, (i + i) * 25);
break;
}
}
//printf("\n");
}
EndBatchDraw();
}
void Random()//随机两组数据一组显示下个方块,一组初始化当前方块
{
if (v != 'F')
{
x = rand() % 5;
l = rand() % 2;
L = rand() % 4;
L2 = rand() % 4;
T = rand() % 4;
v = 'F';
}
else
{
x = x1;
l = l1;
L = L1;
L2 = L21;
T = T1;
}
x1 = rand() % 5;
l1 = rand() % 2;
L1 = rand() % 4;
L21 = rand() % 4;
T1 = rand() % 4;
ShowNextMatch(2, 6, BLock_BL);//显示
}
void InitGame()//初始化游戏
{
for (int i = 0; i < ROW_AR; ++i)
{
for (int j = 0; j < COL_AR; ++j)
{
if ((i == 6 || j == 0 || i == ROW_AR-1 || j == COL_AR-1)&&i>5)
{
MapArr[i][j] = BLock_Frame;
}
else
{
MapArr[i][j] = BLock_null;
}
}
}
Random();
InitBlock(x,l,L,L2,T,BlockLt,originallocationX, originallocationY);
start = clock();
}
char Update()//更新
{
Remove(BLock_null);//覆盖消除
BlockMoveAndChange();//变形
end = clock();
if (end - start >= 300)//再移动
{
if (Ismove('D') == 'T')
{
BlockDown();
}
else
{
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 14; ++j)
{
MapArr[i][j] = BLock_null;
}
}
Random();
if (GameOver())
{
return 'F';
}
Remove(BLock_BL);//写入
Score();//计数消的行数
InitBlock(x,l,L,L2,T,BlockLt, originallocationX, originallocationY);//重新初始化块
}
start = end;
}
Remove(BLock_BL);//再次写入
return 0;
}
int main()
{
srand((unsigned)time(NULL));
initgraph(350, 700);
InitGame();//初始化游戏
while (1)
{
if (Update()=='F')
{
break;
}//更新
DraWing(ROW_AR, COL_AR);//渲染
/* printf("得分为 :%d", NUM);*/
/*system("cls");*/
}
//printf("game over\n");
//system("pause");
closegraph();
return 0;
}
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include <graphics.h>
#define ROW_AR 28
#define COL_AR 14
#define BLOCK_X 4
#define BLOCK_Y 2
#define originallocationX 7
#define originallocationY 7
int start, end,NUM=0;
char p,l,x,L,T,D,L2,l1,x1,L1,T1,L21,v;
enum m
{
BLock_null,
BLock_Frame,
BLock_BL,
};
enum BLock
{
row,
col
};
int MapArr[ROW_AR][COL_AR] = { 0 };
char BlockLt[BLOCK_X][BLOCK_Y] = { 0 };
int GameOver()
{
for (int i = 0; i < BLOCK_X; ++i)
{
if (BlockLt[i][row] < 8)
{
return 1;
}
}
return 0;
}
void Score()//得分
{
int F;
for (F = ROW_AR-2; F >= 8; F--)
{
p = 'T';
for (int G = 1; G < COL_AR-1; G++)
{
if (MapArr[F][G] == BLock_null)
{
p = 'F';
break;
}
}
if (p == 'T')
{
for (int i =F; i >= 8; i--)
{
for (int j = 1; j < COL_AR-1; j++)
{
MapArr[i][j] = MapArr[i-1][j];
}
}
NUM++;
F++;
}
}
}
char IsBlockchange()//判断是否能变形
{
p = 'T';
switch (x)
{
case 0://长条状
if (l == 0)
{
if (MapArr[BlockLt[1][row] - 1][BlockLt[1][col]] != BLock_null || MapArr[BlockLt[1][row] + 1][BlockLt[1][col]] != BLock_null || MapArr[BlockLt[1][row] + 2][BlockLt[1][col]] != BLock_null)
p = 'F';
}
else if (l == 1)
{
if (MapArr[BlockLt[1][row]][BlockLt[1][col]-1] != BLock_null || MapArr[BlockLt[1][row]][BlockLt[1][col]+1] != BLock_null || MapArr[BlockLt[1][row]][BlockLt[1][col]+2] != BLock_null)
p = 'F';
}
break;
case 1://L状
switch (L)
{
case 0:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col] - 1] != BLock_null)
p = 'F';
break;
case 1:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col]] != BLock_null)
p = 'F';
break;
case 2:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
case 3:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] + 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
}
break;
case 2://反7状
switch (L2)
{
case 0:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
case 1:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row] + 1][BlockLt[2][col] + 1] != BLock_null)
p = 'F';
break;
case 2:
if (MapArr[BlockLt[2][row]-1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row]+1][BlockLt[2][col]] != BLock_null || MapArr[BlockLt[2][row] + 1][BlockLt[2][col] - 1] != BLock_null)
p = 'F';
break;
case 3:
if (MapArr[BlockLt[2][row]][BlockLt[2][col] + 1] != BLock_null || MapArr[BlockLt[2][row]][BlockLt[2][col] - 1] != BLock_null || MapArr[BlockLt[2][row] - 1][BlockLt[2][col] - 1] != BLock_null)
p = 'F';
break;
}
break;
case 3://T状
switch (T)
{
case 0:
if (MapArr[BlockLt[1][row]][BlockLt[1][col] + 1] != BLock_null)
p = 'F';
break;
case 1:
if (MapArr[BlockLt[1][row]+1][BlockLt[1][col]] != BLock_null)
p = 'F';
break;
case 2:
if (MapArr[BlockLt[1][row]][BlockLt[1][col] - 1] != BLock_null)
p = 'F';
break;
case 3:
if (MapArr[BlockLt[1][row]-1][BlockLt[1][col]] != BLock_null)
p = 'F';
break;
}
break;
}
return p;
}
void ChangeBlock()//块的变形操作
{
switch (x)
{
case 0://长条状
if (l == 0)
{
BlockLt[0][row] = BlockLt[1][row] - 1;
BlockLt[0][col] = BlockLt[1][col];
BlockLt[2][row] = BlockLt[1][row] + 1;
BlockLt[2][col] = BlockLt[1][col];
BlockLt[3][row] = BlockLt[1][row] + 2;
BlockLt[3][col] = BlockLt[1][col];
l = 1;
}
else if (l == 1)
{
BlockLt[0][row] = BlockLt[1][row];
BlockLt[0][col] = BlockLt[1][col] - 1;
BlockLt[2][row] = BlockLt[1][row];
BlockLt[2][col] = BlockLt[1][col] + 1;
BlockLt[3][row] = BlockLt[1][row];
BlockLt[3][col] = BlockLt[1][col] + 2;
l = 0;
}
break;
case 1://L状
switch (L)
{
case 0:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] - 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] + 1;
L = 1;
break;
case 1:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row] - 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] + 1;
BlockLt[3][col] = BlockLt[2][col];
L = 2;
break;
case 2:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] + 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] - 1;
L = 3;
break;
case 3:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row] + 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] - 1;
BlockLt[3][col] = BlockLt[2][col];
L = 0;
break;
}
break;
case 2://反7状
switch (L2)
{
case 0:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row] - 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] + 1;
BlockLt[3][col] = BlockLt[2][col];
L2 = 1;
break;
case 1:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] + 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] + 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] - 1;
L2 = 2;
break;
case 2:
BlockLt[0][row] = BlockLt[2][row] + 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row] + 1;
BlockLt[1][col] = BlockLt[2][col];
BlockLt[3][row] = BlockLt[2][row] - 1;
BlockLt[3][col] = BlockLt[2][col];
L2 = 3;
break;
case 3:
BlockLt[0][row] = BlockLt[2][row] - 1;
BlockLt[0][col] = BlockLt[2][col] - 1;
BlockLt[1][row] = BlockLt[2][row];
BlockLt[1][col] = BlockLt[2][col] - 1;
BlockLt[3][row] = BlockLt[2][row];
BlockLt[3][col] = BlockLt[2][col] + 1;
L2 = 0;
break;
}
break;
case 3://T状
switch (T)
{
case 0:
BlockLt[0][row] = BlockLt[1][row];
BlockLt[0][col] = BlockLt[1][col]-1;
BlockLt[2][row] = BlockLt[1][row];
BlockLt[2][col] = BlockLt[1][col]+1;
BlockLt[3][row] = BlockLt[1][row]-1;
BlockLt[3][col] = BlockLt[1][col];
T = 1;
break;
case 1:
BlockLt[0][row] = BlockLt[1][row]-1;
BlockLt[0][col] = BlockLt[1][col];
BlockLt[2][row] = BlockLt[1][row]+1;
BlockLt[2][col] = BlockLt[1][col];
BlockLt[3][row] = BlockLt[1][row];
BlockLt[3][col] = BlockLt[1][col]+1;
T = 2;
break;
case 2:
BlockLt[0][row] = BlockLt[1][row];
BlockLt[0][col] = BlockLt[1][col]+1;
BlockLt[2][row] = BlockLt[1][row];
BlockLt[2][col] = BlockLt[1][col]-1;
BlockLt[3][row] = BlockLt[1][row]+1;
BlockLt[3][col] = BlockLt[1][col];
T = 3;
break;
case 3:
BlockLt[0][row] = BlockLt[1][row]+1;
BlockLt[0][col] = BlockLt[1][col];
BlockLt[2][row] = BlockLt[1][row]-1;
BlockLt[2][col] = BlockLt[1][col];
BlockLt[3][row] = BlockLt[1][row];
BlockLt[3][col] = BlockLt[1][col]-1;
T = 0;
break;
}
break;
}
}
char Ismove(char n)//判断是否能下降和左右移动
{
p = 'T';
switch (n)
{
case 'L':
for (int i = 0; i < BLOCK_X; ++i)
{
if ((MapArr[BlockLt[i][row]][BlockLt[i][col] - 1] != BLock_null))
{
p = 'F';
break;
}
}
break;
case 'R':
for (int i = 0; i < BLOCK_X; ++i)
{
if ((MapArr[BlockLt[i][row]][BlockLt[i][col] + 1] != BLock_null))
{
p = 'F';
break;
}
}
break;
case 'D':
for (int i = 0; i < BLOCK_X; ++i)
{
if ((MapArr[BlockLt[i][row] + 1][BlockLt[i][col]] != BLock_null) && BlockLt[i][row] != 5)
{
p = 'F';
break;
}
}
break;
}
return p;
}
void Remove(int block)//复写操作
{
for (int i = 0; i < BLOCK_X; ++i)
{
if (BlockLt[i][row] == 6)
MapArr[BlockLt[i][row]][BlockLt[i][col]] = BLock_Frame;
else if ((BlockLt[i][row] > 6))
MapArr[BlockLt[i][row]][BlockLt[i][col]] = block;
}
}
char Key()//获取玩家操作
{
if (_kbhit())
{
return _getch();
}
return 0;
}
void BlockDown()//块下落
{
for (int i = 0; i < BLOCK_X; ++i)
{
BlockLt[i][row]++;
}
}
void BlockMoveAndChange()//块的运动
{
switch (Key())
{
case 'W':
case 'w':
if (IsBlockchange() == 'T')
ChangeBlock();
break;
case 'A':
case 'a':
if (Ismove('L') == 'T')
{
for (int i = 0; i < BLOCK_X; ++i)//左移
{
BlockLt[i][col]--;
}
}
break;
case 'S'://快速下
case 's':
if (Ismove('D') == 'T')
{
BlockDown();
}
break;
case 'D':
case 'd':
if (Ismove('R') == 'T')
{
for (int i = 0; i < BLOCK_X; ++i)//右移
{
BlockLt[i][col]++;
}
}
break;
}
}
void InitBlock(char x,char l,char L, char L2, char T ,char arr[][2],int m,int n)//初始化块
{
switch (x)
{
case 0://长条状,两种形态
switch (l)
{
case 0:
arr[0][row] = m;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n + 1;
arr[3][row] = m;
arr[3][col] = n + 2;
break;
case 1:
arr[0][row] = m - 1;
arr[0][col] = n;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m + 1;
arr[2][col] = n;
arr[3][row] = m + 2;
arr[3][col] = n;
break;
}
break;
case 1://L状,4种形态
switch (L)
{
case 0:
arr[0][row] = m + 1;
arr[0][col] = n + 1;
arr[1][row] = m + 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m - 1;
arr[3][col] = n;
break;
case 1:
arr[0][row] = m + 1;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n - 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n + 1;
break;
case 2:
arr[0][row] = m - 1;
arr[0][col] = n - 1;
arr[1][row] = m - 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m + 1;
arr[3][col] = n;
break;
case 3:
arr[0][row] = m - 1;
arr[0][col] = n + 1;
arr[1][row] = m;
arr[1][col] = n + 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n - 1;
break;
}
break;
case 2://反7 4种状态
switch (L2)
{
case 0:
arr[0][row] = m - 1;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n - 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n + 1;
break;
case 1:
arr[0][row] = m - 1;
arr[0][col] = n + 1;
arr[1][row] = m - 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m + 1;
arr[3][col] = n;
break;
case 2:
arr[0][row] = m + 1;
arr[0][col] = n + 1;
arr[1][row] = m;
arr[1][col] = n + 1;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n - 1;
break;
case 3:
arr[0][row] = m + 1;
arr[0][col] = n - 1;
arr[1][row] = m + 1;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n;
arr[3][row] = m - 1;
arr[3][col] = n;
break;
}
break;
case 3://T状.四种形态
switch (T)
{
case 0:
arr[0][row] = m + 1;
arr[0][col] = n;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m - 1;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n - 1;
break;
case 1:
arr[0][row] = m;
arr[0][col] = n - 1;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n + 1;
arr[3][row] = m - 1;
arr[3][col] = n;
break;
case 2:
arr[0][row] = m - 1;
arr[0][col] = n;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m + 1;
arr[2][col] = n;
arr[3][row] = m;
arr[3][col] = n + 1;
break;
case 3:
arr[0][row] = m;
arr[0][col] = n + 1;
arr[1][row] = m;
arr[1][col] = n;
arr[2][row] = m;
arr[2][col] = n - 1;
arr[3][row] = m + 1;
arr[3][col] = n;
break;
}
break;
case 4://田字状,一种形态
arr[0][row] = m;
arr[0][col] = originallocationY - 1;
arr[1][row] = m;
arr[1][col] = originallocationY;
arr[2][row] = m - 1;
arr[2][col] = originallocationY - 1;
arr[3][row] = m - 1;
arr[3][col] = originallocationY;
break;
}
}
void ShowNextMatch(int m, int n,int block)//显示当前块
{
char Show[BLOCK_X][BLOCK_Y] = { 0 };
InitBlock(x1,l1,L1,L21,T1,Show, m, n);
for (int i = 0; i < 4; ++i)
{
MapArr[Show[i][row]][Show[i][col]] = block;
}
}
void DraWing(int x,int y)//渲染
{
//for (int i = 0; i < x; ++i)
//{
// for (int j = 0; j < y; ++j)
// {
// switch (MapArr[i][j])
// {
// case 0:
// printf(" ");
// break;
// case 1:
// printf("█");
// break;
// case 2:
// printf("□");
// break;
// }
// }
// putchar('\n');
//}
BeginBatchDraw();
for (int i = 0; i < ROW_AR; ++i)
{
for (int j = 0; j < COL_AR; ++j)
{
switch (MapArr[i][j])
{
case BLock_null:
//printf(" ");
setfillcolor(RGB(0, 0, 0));
setlinecolor(RGB(0, 0, 0));
fillrectangle(j * 25, i * 25, (j + 1) * 25, (i + i) * 25);
break;
case BLock_Frame:
//printf("* ");
setfillcolor(RGB(0, 0, 255));
setlinecolor(RGB(255, 255, 255));
fillrectangle(j * 25, i * 25, (j + 1) * 25, (i + i) * 25);
break;
case BLock_BL:
//printf("# ");
setfillcolor(RGB(255, 0, 0));
setlinecolor(RGB(255, 255, 255));
fillrectangle(j * 25, i * 25, (j + 1) * 25, (i + i) * 25);
break;
}
}
//printf("\n");
}
EndBatchDraw();
}
void Random()//随机两组数据一组显示下个方块,一组初始化当前方块
{
if (v != 'F')
{
x = rand() % 5;
l = rand() % 2;
L = rand() % 4;
L2 = rand() % 4;
T = rand() % 4;
v = 'F';
}
else
{
x = x1;
l = l1;
L = L1;
L2 = L21;
T = T1;
}
x1 = rand() % 5;
l1 = rand() % 2;
L1 = rand() % 4;
L21 = rand() % 4;
T1 = rand() % 4;
ShowNextMatch(2, 6, BLock_BL);//显示
}
void InitGame()//初始化游戏
{
for (int i = 0; i < ROW_AR; ++i)
{
for (int j = 0; j < COL_AR; ++j)
{
if ((i == 6 || j == 0 || i == ROW_AR-1 || j == COL_AR-1)&&i>5)
{
MapArr[i][j] = BLock_Frame;
}
else
{
MapArr[i][j] = BLock_null;
}
}
}
Random();
InitBlock(x,l,L,L2,T,BlockLt,originallocationX, originallocationY);
start = clock();
}
char Update()//更新
{
Remove(BLock_null);//覆盖消除
BlockMoveAndChange();//变形
end = clock();
if (end - start >= 300)//再移动
{
if (Ismove('D') == 'T')
{
BlockDown();
}
else
{
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 14; ++j)
{
MapArr[i][j] = BLock_null;
}
}
Random();
if (GameOver())
{
return 'F';
}
Remove(BLock_BL);//写入
Score();//计数消的行数
InitBlock(x,l,L,L2,T,BlockLt, originallocationX, originallocationY);//重新初始化块
}
start = end;
}
Remove(BLock_BL);//再次写入
return 0;
}
int main()
{
srand((unsigned)time(NULL));
initgraph(350, 700);
InitGame();//初始化游戏
while (1)
{
if (Update()=='F')
{
break;
}//更新
DraWing(ROW_AR, COL_AR);//渲染
/* printf("得分为 :%d", NUM);*/
/*system("cls");*/
}
//printf("game over\n");
//system("pause");
closegraph();
return 0;
}