C# 俄罗斯方块链接如下所示:
控制台《俄罗斯方块》小游戏,Copy代码可直接运行!
用C# 写的俄罗斯方块只用了 1000 行左右的代码,而用C写的源码达到了 1500行,很明显我用C# 写俄罗斯方块进行了优化,希望可以帮助到爱学习的你!
C写的俄罗斯方块 素材只有一首音乐:
程序的效果如下所示:
小游戏制作俄罗斯方块
代码实现如下所示:
//相关头文件
#include <graphics.h>//图形头文件
#include <time.h>//时间头文件
#include <stdlib.h>//动态分配内存头文件
#include <conio.h>//标准输入输出头文件
#include <stdio.h>//标准输入输出头文件
#include <mmsystem.h>//播放音乐的头文件
#pragma comment(lib,"winmm.lib")//播放音乐的库文件//宏定义
#define HEIGHT 20 //数组的大小 地图大小
#define WIDTH 10// 1 方块 2 已经到底或者不能动的方块 0 空
int background_sz[HEIGHT][WIDTH] = { 0 }; //背景分割 大小为20行,10列
int generate[2][4] = { 0 }; //用于存储生成的方块,用于赋值int a; //用来标记每个不同的方块 七种的方块,分别用1~7表示
int t1, tt1;
int flag = 1; //用来判断消行
int delay_time;
int long_flag = 0; //用来判断长方块变形的标记,0是横向的,1是纵向的
int z_flag1 = 0; //Z形的两种方块,0是横向的,1是纵向的
int z_flag2 = 0;
int preiection_flag = 0; //中间凸起的方块
int l_flag1 = 0;
int l_flag2 = 0;/**********************************************************函数声明********************************************************/void FriendlyReminder(); //友情提醒 游戏规则
void InitGraph(); //初始化游戏界面
void Font(); //梦的颜色一直变 用来装逼
void BlockGenerated(); //方块生成函数
void DrawBlock(); //画出方块
void DeclineBlock(); //方块下降 每隔500个毫秒,就下移一次
void BlockLeft(); //方块左移
void BlockRight(); //方块右移
void BlockDown(); //方块下移
void MoveBlock(); //移动方块
int BottomStop(); //方块到达底部, 返回 1 停止
int DownStop(); //方块下降遇到方块, 返回 1 停止
void JudgeDownStop(); //将上面的两个函数包含在一起,形成一个向下移动的判断
int LeftStop(); //判断向左移动,越界的情况 1 可以移动 0不可以移动
int LeftStop_1(); //判断向左移动,遇到方块移动不了 返回 1 停止
int RightStop(); //判断向右移动,越界的情况 1 可以移动 0不可以移动
int RightStop_1(); //判断向右移动,遇到方块移动不了 返回 1 停止
void JudgeGame(); //判断是否结束游戏
void Score(); //消灭方块
int JudgeLongBlockTransformation(); //判断长方块 变形 1 可以变形
void LongBlockTransformation(); //长方块变形
int Judge_Z_BlockTransformation(); // 判断Z形的方块能否变形
void _Z_BlockTransformation(); // Z形方块变形、 a == 2,3的情况
int JudgePreiectionBlockTransformation(); // 判断中间凸起的方块能否变形
void PreiectionBlockTransformation(); // 中间凸起的方块变形
int Judge_L_BlockTransformation(); // 判断L形的方块能否变形
void _L_BlockTransformation(); // L形方块变形 a == 6,7的情况/********************************************************入口函数**********************************************************/int main()
{FriendlyReminder();InitGraph(); //初始化界面BlockGenerated(); //随机生成方块while (1){tt1 = GetTickCount(); //获取系统时间DrawBlock(); //显示方块if (_kbhit()) //每如按下了键值,就移动方块{MoveBlock();}if (tt1 - t1 > delay_time) //每隔500个毫秒就下降一次{DeclineBlock(); //方块下降Font(); //梦 的颜色一直变t1 = tt1;}JudgeGame(); //判断游戏是否结束}return 0;
}/*****************************************************自定义函数***********************************************************///初始化游戏界面
void InitGraph()
{//播放背景音乐mciSendString("open 1.mp3 alias cong", 0, 0, 0);mciSendString("play cong repeat", 0, 0, 0);srand((unsigned)time(0));initgraph(WIDTH * 20 + 100, HEIGHT * 20); //界面大小setbkcolor(RGB(0, 255, 255)); //背景颜色cleardevice(); //刷新界面setlinecolor(WHITE);for (int i = 0; i <= HEIGHT; i++) //画出网格线{for (int j = 0; j <= WIDTH; j++){line(j * 20, 0, j * 20, HEIGHT * 20);line(0, i * 20, WIDTH * 20, i * 20);}}settextstyle(25, 15, "楷体");settextcolor(YELLOW);outtextxy(230, 20, "花");outtextxy(230, 50, "里");outtextxy(230, 80, "胡");outtextxy(230, 110, "哨");outtextxy(240, 150, "D");settextstyle(70, 35, "楷体");outtextxy(220, 190, "梦");t1 = GetTickCount(); //获取系统时间
}//梦的颜色一直变
void Font()
{settextcolor(RGB(rand() % 256, rand() % 256, rand() % 256));outtextxy(220, 190, "梦");
}//方块生成函数
void BlockGenerated()
{Score(); //消行 每次就消灭掉一行!a = rand() % 7 + 1; //方块一共有七种 用随机数产生switch (a){case 1: generate[0][0] = 1; generate[0][1] = 1; generate[0][2] = 0; generate[0][3] = 0;generate[1][0] = 1; generate[1][1] = 1; generate[1][2] = 0; generate[1][3] = 0;break;case 2: generate[0][0] = 1; generate[0][1] = 1; generate[0][2] = 0; generate[0][3] = 0;generate[1][0] = 0; generate[1][1] = 1; generate[1][2] = 1; generate[1][3] = 0;z_flag1 = 0;break;case 3: generate[0][0] = 0; generate[0][1] = 1; generate[0][2] = 1; generate[0][3] = 0;generate[1][0] = 1; generate[1][1] = 1; generate[1][2] = 0; generate[1][3] = 0;z_flag2 = 0;break;case 4: generate[0][0] = 1; generate[0][1] = 1; generate[0][2] = 1; generate[0][3] = 1;generate[1][0] = 0; generate[1][1] = 0; generate[1][2] = 0; generate[1][3] = 0;long_flag = 0;break;case 5: generate[0][0] = 0; generate[0][1] = 1; generate[0][2] = 0; generate[0][3] = 0;generate[1][0] = 1; generate[1][1] = 1; generate[1][2] = 1; generate[1][3] = 0;preiection_flag = 0;break;case 6: generate[0][0] = 1; generate[0][1] = 0; generate[0][2] = 0; generate[0][3] = 0;generate[1][0] = 1; generate[1][1] = 1; generate[1][2] = 1; generate[1][3] = 0;l_flag1 = 0;break;case 7: generate[0][0] = 0; generate[0][1] = 0; generate[0][2] = 1; generate[0][3] = 0;generate[1][0] = 1; generate[1][1] = 1; generate[1][2] = 1; generate[1][3] = 0;l_flag2 = 0;break;}//赋值,将生成的方块在界面的数组中展示出来for (int i = 0; i < 2; i++){for (int j = 0; j < 4; j++){background_sz[i][j + 3] = generate[i][j]; //在正中间出现}}//设置生成的方块颜色setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
}//画出方块
void DrawBlock()
{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1 || background_sz[i][j] == 2) //中间有点缝,这样看起来好看一些{//图形界面的X,Y是和控制台相反的,需要特别注意!!!solidrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);}}}
}//方块下降 每隔500个毫秒,就下移一次
void DeclineBlock()
{JudgeDownStop();for (int i = HEIGHT - 1; i >= 0; i--){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){background_sz[i + 1][j] = background_sz[i][j];background_sz[i][j] = 0;clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);}}}JudgeDownStop(); //向下的判断
}//方块左移
void BlockLeft()
{for (int j = 0; j < WIDTH; j++){for (int i = HEIGHT - 1; i >= 0; i--){if (background_sz[i][j] == 1){background_sz[i][j - 1] = background_sz[i][j];background_sz[i][j] = 0;clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);}}}
}//方块右移
void BlockRight()
{for (int j = WIDTH - 1; j >= 0; j--){for (int i = 0; i < HEIGHT; i++){if (background_sz[i][j] == 1){background_sz[i][j + 1] = background_sz[i][j];background_sz[i][j] = 0;clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);}}}
}//方块下移
void BlockDown()
{for (int i = HEIGHT - 1; i >= 0; i--){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){background_sz[i + 1][j] = background_sz[i][j];background_sz[i][j] = 0;clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);}}}
}//移动方块
void MoveBlock()
{char ch = getch();switch (ch){case 32: // 用于暂停system("pause");break;case 72: // 用于变形switch (a){case 1: //正方形的方块,不需要变形break;case 2:case 3:if (Judge_Z_BlockTransformation()){_Z_BlockTransformation();}break;case 4:if (JudgeLongBlockTransformation()){LongBlockTransformation();}break;case 5:if (JudgePreiectionBlockTransformation()){PreiectionBlockTransformation();}break;case 6:case 7:if (Judge_L_BlockTransformation){_L_BlockTransformation();}break;}break;case 75: //左移if (LeftStop() && LeftStop_1()){BlockLeft();}break;case 77: //右移 if (RightStop() && RightStop_1()){BlockRight();}break;case 80: //下移 JudgeDownStop();BlockDown();break;}}//方块到达底部,停止
int BottomStop()
{for (int j = 0; j < WIDTH; j++){if (background_sz[HEIGHT - 1][j] == 1){return 1;}}return 0;
}//方块下降遇到方块, 返回 1 停止
int DownStop()
{for (int i = HEIGHT - 1; i >= 0; i--){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i + 1][j] == 2){return 1;}}}}return 0;
}//将上面的两个函数包含在一起,形成一个向下移动的判断
void JudgeDownStop()
{if (BottomStop()){for (int i = HEIGHT - 1; i >= 0; i--){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){background_sz[i][j] = 2;}}}BlockGenerated();}else if (DownStop()){for (int i = HEIGHT - 1; i >= 0; i--){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){background_sz[i][j] = 2;}}}BlockGenerated();}
}//判断向左移动,越界的情况
int LeftStop()
{for (int i = 0; i < HEIGHT; i++){if (background_sz[i][0] == 1)return 0;}return 1;
}//判断向左移动,遇到方块移动不了
int LeftStop_1()
{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j - 1] == 2)return 0;}}}return 1;
}//判断向右移动,越界的情况
int RightStop()
{for (int i = HEIGHT - 1; i >= 0; i--){if (background_sz[i][HEIGHT - 1] == 1)return 0;}return 1;
}//判断向右移动,遇到方块移动不了
int RightStop_1()
{for (int i = 0; i < HEIGHT; i++){for (int j = WIDTH - 1; j >= 0; j--){if (background_sz[i][j] == 1){if (background_sz[i][j + 1] == 2)return 0;}}}return 1;
}//判断是否结束游戏
void JudgeGame()
{for (int j = 0; j < WIDTH; j++){if (background_sz[1][j] == 1 && background_sz[2][j] == 2){setbkcolor(YELLOW);cleardevice();settextcolor(RED);settextstyle(20, 20, "宋体");outtextxy((WIDTH * 20 + 100) / 2 - 100, (HEIGHT * 20) / 2 - 50, "Game Over!");exit(0); //退出程序}}
}//判断得分 (消行)
void Score()
{for (int i = HEIGHT - 1; i > 3; i--) //为什么要大于3?? {flag = 1;for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] != 2 && background_sz[i][j] != 1){flag = 0;break;}}if (flag) //找到可以清行的存在{//把所有的格子清空for (int k = 0; k < HEIGHT; k++){for (int j = 0; j < WIDTH; j++){if (background_sz[k][j] == 1 || background_sz[k][j] == 2){//图形界面的X,Y是和控制台相反的,需要特别注意!!!clearrectangle(j * 20 + 2, k * 20 + 2, j * 20 + 18, k * 20 + 18);}}}for (int n = i; n > 3; n--) //把消行的上面所有数组值,向下赋值{for (int j = 0; j < WIDTH; j++){background_sz[n][j] = background_sz[n - 1][j];}}i++;}DrawBlock();}
}//游戏规则
void FriendlyReminder()
{printf("友情提醒:\n\n");printf("按键盘的 ← 、→ 、↓ 控制方块移动!\n");printf("按下 SPACE 键暂停游戏!\t");printf("按键盘的 ↑,方块变形!!!\n\n");printf("注:本游戏无计分设置,主要目的是听歌,打发时间\n\n\t\t=.=\n");printf("\n\n\n请输入方块下落速度,单位毫秒ms\t\t按回车继续\n延迟时间是:");scanf("%d", &delay_time);getch();
}//判断长方块变形
int JudgeLongBlockTransformation()
{if (long_flag == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){for (int k = i - 1; k >= i - 3; k--){if (background_sz[k][j] == 2)return 0;}return 1;}}}}else{for (int i = HEIGHT - 1; i > 0; i--){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){for (int k = j + 1; k <= j + 3; k++){if (background_sz[i][k] == 2)return 0;}return 1;}}}}
}//长方块变形
void LongBlockTransformation()
{if (long_flag == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i > 2){clearrectangle((j + 1) * 20 + 2, i * 20 + 2, (j + 1) * 20 + 18, i * 20 + 18);clearrectangle((j + 2) * 20 + 2, i * 20 + 2, (j + 2) * 20 + 18, i * 20 + 18);clearrectangle((j + 3) * 20 + 2, i * 20 + 2, (j + 3) * 20 + 18, i * 20 + 18);background_sz[i][j + 1] = 0;background_sz[i][j + 2] = 0;background_sz[i][j + 3] = 0;background_sz[i - 1][j] = 1;background_sz[i - 2][j] = 1;background_sz[i - 3][j] = 1;DrawBlock();long_flag = 1;return;}elsereturn;}}}}else{for (int i = HEIGHT - 1; i > 0; i--){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j < WIDTH - 3){clearrectangle(j * 20 + 2, (i - 1) * 20 + 2, j * 20 + 18, (i - 1) * 20 + 18);clearrectangle(j * 20 + 2, (i - 2) * 20 + 2, j * 20 + 18, (i - 2) * 20 + 18);clearrectangle(j * 20 + 2, (i - 3) * 20 + 2, j * 20 + 18, (i - 3) * 20 + 18);background_sz[i - 1][j] = 0;background_sz[i - 2][j] = 0;background_sz[i - 3][j] = 0;background_sz[i][j + 1] = 1;background_sz[i][j + 2] = 1;background_sz[i][j + 3] = 1;DrawBlock();long_flag = 0;return;}elsereturn;}}}}
}// 判断Z形的方块能否变形
int Judge_Z_BlockTransformation()
{if (a == 2){if (z_flag1 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i - 1][j + 1] == 2 || background_sz[i + 1][j] == 2)return 0;return 1;}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i + 2][j] == 2 || background_sz[i + 2][j + 1] == 2)return 0;return 1;}}}}}else{if (z_flag2 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i + 1][j + 1] == 2 || background_sz[i + 2][j + 1] == 2)return 0;return 1;}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j + 1] == 2 || background_sz[i + 1][j - 1] == 2)return 0;return 1;}}}}}
}// Z形方块变形 a == 2 . 3的情况
void _Z_BlockTransformation()
{if (a == 2){if (z_flag1 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i > 0){clearrectangle((j + 1) * 20 + 2, (i + 1) * 20 + 2, (j + 1) * 20 + 18, (i + 1) * 20 + 18);clearrectangle((j + 2) * 20 + 2, (i + 1) * 20 + 2, (j + 2) * 20 + 18, (i + 1) * 20 + 18);background_sz[i + 1][j + 1] = 0;background_sz[i + 1][j + 2] = 0;background_sz[i - 1][j + 1] = 1;background_sz[i + 1][j] = 1;DrawBlock();z_flag1 = 1;return;}else{return;}}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j < WIDTH - 1){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle((j - 1) * 20 + 2, (i + 2) * 20 + 2, (j - 1) * 20 + 18, (i + 2) * 20 + 18);background_sz[i][j] = 0;background_sz[i + 2][j - 1] = 0;background_sz[i + 2][j] = 1;background_sz[i + 2][j + 1] = 1;DrawBlock();z_flag1 = 0;return;}else{return;}}}}}}else{if (z_flag2 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i < HEIGHT - 2){clearrectangle((j + 1) * 20 + 2, i * 20 + 2, (j + 1) * 20 + 18, i * 20 + 18);clearrectangle((j - 1) * 20 + 2, (i + 1) * 20 + 2, (j - 1) * 20 + 18, (i + 1) * 20 + 18);background_sz[i][j + 1] = 0;background_sz[i + 1][j - 1] = 0;background_sz[i + 1][j + 1] = 1;background_sz[i + 2][j + 1] = 1;DrawBlock();z_flag2 = 1;return;}elsereturn;}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j > 0){clearrectangle((j + 1) * 20 + 2, (i + 1) * 20 + 2, (j + 1) * 20 + 18, (i + 1) * 20 + 18);clearrectangle((j + 1) * 20 + 2, (i + 2) * 20 + 2, (j + 1) * 20 + 18, (i + 2) * 20 + 18);background_sz[i + 1][j + 1] = 0;background_sz[i + 2][j + 1] = 0;background_sz[i][j + 1] = 1;background_sz[i + 1][j - 1] = 1;DrawBlock();z_flag2 = 0;return;}else{return;}}}}}}
}// 判断中间凸起的方块能否变形
int JudgePreiectionBlockTransformation()
{if (preiection_flag == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j - 1] == 2 || background_sz[i - 1][j - 1] == 2)return 0;return 1;}}}}else if (preiection_flag == 1){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j + 1] == 2 || background_sz[i][j + 2] == 2)return 0;return 1;}}}}else if (preiection_flag == 2){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i + 1][j + 2] == 2 || background_sz[i + 2][j + 2] == 2)return 0;return 1;}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i + 2][j - 2] == 2 || background_sz[i + 2][j - 1] == 2)return 0;return 1;}}}}
}// 中间凸起的方块变形
void PreiectionBlockTransformation()
{if (preiection_flag == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i > 0){clearrectangle(j * 20 + 2, (i + 1) * 20 + 2, j * 20 + 18, (i + 1) * 20 + 18);clearrectangle((j + 1) * 20 + 2, (i + 1) * 20 + 2, (j + 1) * 20 + 18, (i + 1) * 20 + 18);background_sz[i + 1][j] = 0;background_sz[i + 1][j + 1] = 0;background_sz[i][j - 1] = 1;background_sz[i - 1][j - 1] = 1;DrawBlock();preiection_flag = 1;return;}else{return;}}}}}else if (preiection_flag == 1){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j < WIDTH - 2){clearrectangle(j * 20 + 2, (i + 1) * 20 + 2, j * 20 + 18, (i + 1) * 20 + 18);clearrectangle(j * 20 + 2, (i + 2) * 20 + 2, j * 20 + 18, (i + 2) * 20 + 18);background_sz[i + 1][j] = 0;background_sz[i + 2][j] = 0;background_sz[i][j + 1] = 1;background_sz[i][j + 2] = 1;DrawBlock();preiection_flag = 2;return;}else{return;}}}}}else if (preiection_flag == 2){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i < HEIGHT - 2){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle((j + 1) * 20 + 2, i * 20 + 2, (j + 1) * 20 + 18, i * 20 + 18);background_sz[i][j] = 0;background_sz[i][j + 1] = 0;background_sz[i + 1][j + 2] = 1;background_sz[i + 2][j + 2] = 1;DrawBlock();preiection_flag = 3;return;}else{return;}}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j >= 2){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle(j * 20 + 2, (i + 1) * 20 + 2, j * 20 + 18, (i + 1) * 20 + 18);background_sz[i][j] = 0;background_sz[i + 1][j] = 0;background_sz[i + 2][j - 2] = 1;background_sz[i + 2][j - 1] = 1;DrawBlock();preiection_flag = 0;return;}else{return;}}}}}
}// 判断L形的方块能否变形
int Judge_L_BlockTransformation()
{if (a == 6){if (l_flag1 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i - 1][j] == 2 || background_sz[i - 1][j + 1] == 2)return 0;return 1;}}}}}else if (l_flag1 == 1){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i][j - 1] == 2 || background_sz[i + 1][j + 1] == 2)return 0;return 1;}}}}}else if (l_flag1 == 2){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i + 1][j + 1] == 2 || background_sz[i - 1][j + 2] == 2)return 0;return 1;}}}}}else if (l_flag1 == 3){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i + 1][j - 1] == 2 || background_sz[i + 2][j + 1] == 2)return 0;return 1;}}}}}}else{if (l_flag2 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i][j - 1] == 2 || background_sz[i - 1][j - 1] == 2)return 0;return 1;}}}}}else if (l_flag2 == 1){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i + 1][j + 1] == 2 || background_sz[i + 1][j + 2] == 2)return 0;return 1;}}}}}else if (l_flag2 == 2){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i + 1][j + 1] == 2 || background_sz[i + 2][j + 1] == 2)return 0;return 1;}}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (background_sz[i][j] == 1){if (background_sz[i + 1][j - 1] == 2 || background_sz[i + 1][j] == 2)return 0;return 1;}}}}}}
}// Z形方块变形 a == 2 . 3的情况
void _L_BlockTransformation()
{if (a == 6){if (l_flag1 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i > 0){clearrectangle((j + 1) * 20 + 2, (i + 1) * 20 + 2, (j + 1) * 20 + 18, (i + 1) * 20 + 18);clearrectangle((j + 2) * 20 + 2, (i + 1) * 20 + 2, (j + 2) * 20 + 18, (i + 1) * 20 + 18);background_sz[i + 1][j + 1] = 0;background_sz[i + 1][j + 2] = 0;background_sz[i - 1][j] = 1;background_sz[i - 1][j + 1] = 1;DrawBlock();l_flag1 = 1;return;}else{return;}}}}}else if (l_flag1 == 1){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j > 0){clearrectangle(j * 20 + 2, (i + 1) * 20 + 2, j * 20 + 18, (i + 1) * 20 + 18);clearrectangle(j * 20 + 2, (i + 2) * 20 + 2, j * 20 + 18, (i + 2) * 20 + 18);background_sz[i + 1][j] = 0;background_sz[i + 2][j] = 0;background_sz[i][j - 1] = 1;background_sz[i + 1][j + 1] = 1;DrawBlock();l_flag1 = 2;return;}else{return;}}}}}else if (l_flag1 == 2){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i > 0){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle((j + 1) * 20 + 2, i * 20 + 2, (j + 1) * 20 + 18, i * 20 + 18);background_sz[i][j] = 0;background_sz[i][j + 1] = 0;background_sz[i + 1][j + 1] = 1;background_sz[i - 1][j + 2] = 1;DrawBlock();l_flag1 = 3;return;}else{return;}}}}}else if (l_flag1 == 3){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j < WIDTH - 1){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle(j * 20 + 2, (i + 1) * 20 + 2, j * 20 + 18, (i + 1) * 20 + 18);background_sz[i][j] = 0;background_sz[i + 1][j] = 0;background_sz[i + 1][j - 1] = 1;background_sz[i + 2][j + 1] = 1;DrawBlock();l_flag1 = 0;return;}else{return;}}}}}}else{if (l_flag2 == 0){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i > 0){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle((j - 2) * 20 + 2, (i + 1) * 20 + 2, (j - 2) * 20 + 18, (i + 1) * 20 + 18);background_sz[i][j] = 0;background_sz[i + 1][j - 2] = 0;background_sz[i][j - 1] = 1;background_sz[i - 1][j - 1] = 1;DrawBlock();l_flag2 = 1;return;}else{return;}}}}}else if (l_flag2 == 1){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j < WIDTH - 2){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle((j + 1) * 20 + 2, (i + 2) * 20 + 2, (j + 1) * 20 + 18, (i + 2) * 20 + 18);background_sz[i][j] = 0;background_sz[i + 2][j + 1] = 0;background_sz[i + 1][j + 1] = 1;background_sz[i + 1][j + 2] = 1;DrawBlock();l_flag2 = 2;return;}else{return;}}}}}else if (l_flag2 == 2){for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (i < HEIGHT - 2){clearrectangle(j * 20 + 2, (i + 1) * 20 + 2, j * 20 + 18, (i + 1) * 20 + 18);clearrectangle((j + 2) * 20 + 2, i * 20 + 2, (j + 2) * 20 + 18, i * 20 + 18);background_sz[i + 1][j] = 0;background_sz[i][j + 2] = 0;background_sz[i + 1][j + 1] = 1;background_sz[i + 2][j + 1] = 1;DrawBlock();l_flag2 = 3;return;}else{return;}}}}}else{for (int i = 0; i < HEIGHT; i++){for (int j = 0; j < WIDTH; j++){if (background_sz[i][j] == 1){if (j > 0){clearrectangle(j * 20 + 2, i * 20 + 2, j * 20 + 18, i * 20 + 18);clearrectangle((j + 1) * 20 + 2, (i + 2) * 20 + 2, (j + 1) * 20 + 18, (i + 2) * 20 + 18);background_sz[i][j] = 0;background_sz[i + 2][j + 1] = 0;background_sz[i + 1][j - 1] = 1;background_sz[i + 1][j] = 1;DrawBlock();l_flag2 = 0;return;}else{return;}}}}}}
}//代码结束!!! 够累...