C语言俄罗斯方块(新版本完整代码)

article/2025/9/25 2:08:13

2020博客之星年度总评选进行中:请为74号的狗子投上宝贵的一票!
我的投票地址:点击为我投票
在这里插入图片描述


写在前面:
大家好,我是 花狗Fdog ,来自内蒙古的一个小城市,目前在泰州读书。
很感谢能有这样一个平台让我能够在这里分享所学所感。
我喜欢编程,喜欢代码,喜欢去做一个程序员。
努力学习,争取多年后,给亲人更好的生活。
QQ/WX:2506897252 欢迎交流。

文章目录

  • 一.游戏效果
  • 二.代码实现
    • 1.绘制地图
    • 2.随机主方块生成
    • 3.按键响应
    • 4.方块坐标全部确定
    • 5.显示完整方块
    • 6.判断左右界限
    • 7.移动光标
    • 8.颜色设定
    • 9.判断落地
    • 10.判断整行是否填满
    • 11.若填满,则清楚该行,并刷新地图
    • 12.显示信息
    • 13.坐标更新1
    • 14.坐标更新2
    • 15.显示信息更新
    • 16.主函数内容
  • 三.完整代码奉上,也可自行下载

一.游戏效果

在这里插入图片描述
旧版800行代码,这个只有500行,并且不闪屏,看着舒服。废话不多说,上图,上代码。

二.代码实现

1.绘制地图

void DeawMap()
{for (int i = 0; i < WIDTH; i++)PRINTF LINE  //上边框for (int i = 1; i < HEIGHT - 1; i++)          //打印左右边框{for (int j = 0; j < WIDTH; j++){if (j == 0 || j == WIDTH - 1){PRINTFif (j == WIDTH - 1)LINE}else EMPTY}}for (int i = 0; i < WIDTH; i++)PRINTF LINE  //下边框system("color 03");
}

2.随机主方块生成

Tetris * BlockRand(int code_y)
{srand((int)time(0));Tetris * Block = (Tetris*)malloc(sizeof(Tetris));Block->x_1 = 8;Block->y_1 = 4;//规定初始中心方块的坐标为(8,4)Block->code = code_y;if (Phead == NULL)Phead = Block;else Pend->next = Block;Block->next = NULL;Pend = Block;return Block;
}

3.按键响应

void JudgeDirection(Tetris ** Block)
{if (GetAsyncKeyState(VK_UP) && 0x8000){form += 1;if (form == 4){form = 0;}Form(&Return);}if (GetAsyncKeyState(VK_DOWN) && 0x8000){//加速向下  时间加速UP = 1;}if (GetAsyncKeyState(VK_LEFT) && 0x8000){//向左移动if (JudgeWall(&Return) != -1) Location_y(&Return, -1, 0, -1, 0, -1, 0, -1, 0);}if (GetAsyncKeyState(VK_RIGHT) && 0x8000){//向右移动if (JudgeWall(&Return) != -2) Location_y(&Return, 1, 0, 1, 0, 1, 0, 1, 0);}if (GetAsyncKeyState(VK_ESCAPE) && 0x0D){MoveCursor(27, 15);printf("游戏暂停");//判断Escwhile (1){if (GetAsyncKeyState(VK_ESCAPE) && 0x0D){MoveCursor(27, 15);printf("       ");break;}}}
}

4.方块坐标全部确定

void Form(Tetris ** Block)
{//先确实哪一类,再细分switch ((*Block)->code){case 1:if (form == 0)Location(&Return, 0, 0, -1, 0, 0, -1, 1, 0);if (form == 1)Location(&Return, 0, 0, 0, 1, 0, -1, 1, 0);if (form == 2)Location(&Return, 0, 0, 0, 1, -1, 0, 1, 0);if (form == 3)Location(&Return, 0, 0, 0, 1, -1, 0, 0, -1);break;case 2:Location(&Return, 0, 0, 1, 0, 0, 1, 1, 1);break;case 3:if (form == 0 || form == 2)Location(&Return, 0, 0, 0, -1, 0, 1, 0, 2);if (form == 1 || form == 3)Location(&Return, 0, 0, -1, 0, 1, 0, 2, 0);break;case 4:if (form == 0)Location(&Return, 0, 0, -1, 0, 1, 0, 1, -1);if (form == 1)Location(&Return, 0, 0, 0, -1, 1, 0, 0, -2);if (form == 2)Location(&Return, 0, 0, 0, -1, 1, -1, 2, -1);if (form == 3)Location(&Return, 0, 0, 0, -1, 0, -2, -1, -2);break;case 5:if (form == 0)Location(&Return, 0, 0, 1, 0, 2, 0, 0, -1);if (form == 1)Location(&Return, 0, 0, 1, 0, 1, -1, 1, -2);if (form == 2)Location(&Return, 0, 0, 1, 0, 2, 0, 2, 1);if (form == 3)Location(&Return, 0, 0, 1, 0, 0, 1, 0, 2);break;case 6:if (form == 0 || form == 2)Location(&Return, 0, 0, 0, -1, 1, 0, 1, 1);if (form == 1 || form == 3)Location(&Return, 0, 0, 0, -1, 1, -1, -1, 0);break;case 7:if (form == 0 || form == 2)Location(&Return, 0, 0, 0, 1, 1, 0, 1, -1);if (form == 1 || form == 3)Location(&Return, 0, 0, 0, 1, -1, 0, 1, 1);}
}

5.显示完整方块

void ShowBlock(Tetris ** Block)
{while (1){Form(&Return);if ((*Block)->code == 1)SetColour(13);if ((*Block)->code == 2)SetColour(15);if ((*Block)->code == 3)SetColour(12);if ((*Block)->code == 4)SetColour(10);if ((*Block)->code == 5)SetColour(6);if ((*Block)->code == 6)SetColour(4);if ((*Block)->code == 7)SetColour(8);MoveCursor((*Block)->x_1, (*Block)->y_1); PRINTFMoveCursor((*Block)->x_2, (*Block)->y_2); PRINTFMoveCursor((*Block)->x_3, (*Block)->y_3); PRINTFMoveCursor((*Block)->x_4, (*Block)->y_4); PRINTFif (JudgeGroud(Phead, &Return) == 0){system("color 03");break;}if (UP == 0){for (int i = 0; i <= 400000000; i++) {}}if (UP == 1){for (int i = 0; i <= 40000000; i++) {}UP = 0;}MoveCursor((*Block)->x_1, (*Block)->y_1); EMPTYMoveCursor((*Block)->x_2, (*Block)->y_2); EMPTYMoveCursor((*Block)->x_3, (*Block)->y_3); EMPTYMoveCursor((*Block)->x_4, (*Block)->y_4); EMPTYLocation_y(&Return, 0, 1, 0, 1, 0, 1, 0, 1);JudgeDirection(&Return);JudgeEntire(Phead);}
}

6.判断左右界限

int JudgeWall(Tetris ** Block)
{if ((*Block)->x_1 == ZERO || (*Block)->x_2 == ZERO || (*Block)->x_3 == ZERO || (*Block)->x_4 == ZERO)return -1;if ((*Block)->x_1 == HEIGHT_1 || (*Block)->x_2 == HEIGHT_1 || (*Block)->x_3 == HEIGHT_1 || (*Block)->x_4 == HEIGHT_1)return -2;return 0;
}

7.移动光标

void MoveCursor(int x, int y)//设置光标位置(就是输出显示的开始位置)
{COORD pos = { x * 2,y };HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);//获得 标准输出的句柄   SetConsoleCursorPosition(output, pos); //设置控制台光标位置
}

8.颜色设定

void SetColour(int c)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);//API函数可以改变控制台颜色
}

9.判断落地

int JudgeGroud(Tetris * Phead, Tetris ** Block)
{Tetris * P = Phead;//如果到达最低层。直接经行下一循环if ((*Block)->y_1 == 26 || (*Block)->y_2 == 26 || (*Block)->y_3 == 26 || (*Block)->y_4 == 26)return 0;while (P->next != NULL){if (P->y_1 == (*Block)->y_1 + 1){if (P->x_1 == (*Block)->x_1)return 0;}if (P->y_2 == (*Block)->y_1 + 1){if (P->x_2 == (*Block)->x_1)return 0;}if (P->y_3 == (*Block)->y_1 + 1){if (P->x_3 == (*Block)->x_1)return 0;}if (P->y_4 == (*Block)->y_1 + 1){if (P->x_4 == (*Block)->x_1)return 0;}if (P->y_1 == (*Block)->y_2 + 1){if (P->x_1 == (*Block)->x_2)return 0;}if (P->y_2 == (*Block)->y_2 + 1){if (P->x_2 == (*Block)->x_2)return 0;}if (P->y_3 == (*Block)->y_2 + 1){if (P->x_3 == (*Block)->x_2)return 0;}if (P->y_4 == (*Block)->y_2 + 1){if (P->x_4 == (*Block)->x_2)return 0;}if (P->y_1 == (*Block)->y_3 + 1){if (P->x_1 == (*Block)->x_3)return 0;}if (P->y_2 == (*Block)->y_3 + 1){if (P->x_2 == (*Block)->x_3)return 0;}if (P->y_3 == (*Block)->y_3 + 1){if (P->x_3 == (*Block)->x_3)return 0;}if (P->y_4 == (*Block)->y_3 + 1){if (P->x_4 == (*Block)->x_3)return 0;}if (P->y_1 == (*Block)->y_4 + 1){if (P->x_1 == (*Block)->x_4)return 0;}if (P->y_2 == (*Block)->y_4 + 1){if (P->x_2 == (*Block)->x_4)return 0;}if (P->y_3 == (*Block)->y_4 + 1){if (P->x_3 == (*Block)->x_4)return 0;}if (P->y_4 == (*Block)->y_4 + 1){if (P->x_4 == (*Block)->x_4)return 0;}P = P->next;}return 1;
}

10.判断整行是否填满

void JudgeEntire(Tetris * Head)
{Tetris * PHead = Head;//从1到26for (int y = 26; y >= 1; y--){int sum = 0;while (PHead->next != NULL){if (PHead->y_1 == y)sum++;if (PHead->y_2 == y)sum++;if (PHead->y_3 == y)sum++;if (PHead->y_4 == y)sum++;MoveCursor(20, 28);PHead = PHead->next;}PHead = Head;if (sum == 18){//如果成行则,执行NewEntire()  清空该行,并将所有y下降一个单位。NewEntire(Phead, y);fengs += 10;Show(code_y);}sum = 0;}
}

11.若填满,则清楚该行,并刷新地图

void NewEntire(Tetris * head, int y)
{Tetris * PHead = head;while (PHead->next != NULL){if (PHead->y_1 == y){MoveCursor(PHead->x_1, PHead->y_1); EMPTYPHead->x_1 = 99;PHead->y_1 = 99;}if (PHead->y_2 == y){MoveCursor(PHead->x_2, PHead->y_2); EMPTYPHead->x_2 = 99;PHead->y_2 = 99;}if (PHead->y_3 == y){MoveCursor(PHead->x_3, PHead->y_3); EMPTYPHead->x_3 = 99;PHead->y_3 = 99;}if (PHead->y_4 == y){MoveCursor(PHead->x_4, PHead->y_4); EMPTYPHead->x_4 = 99;PHead->y_4 = 99;}PHead = PHead->next;}PHead = head;while (PHead->next != NULL){if (PHead->y_1 < y){MoveCursor(PHead->x_1, PHead->y_1); EMPTYPHead->y_1 += 1;MoveCursor(PHead->x_1, PHead->y_1); PRINTF}if (PHead->y_2 < y){MoveCursor(PHead->x_2, PHead->y_2); EMPTYPHead->y_2 += 1;MoveCursor(PHead->x_2, PHead->y_2); PRINTF}if (PHead->y_3 < y){MoveCursor(PHead->x_3, PHead->y_3); EMPTYPHead->y_3 += 1;MoveCursor(PHead->x_3, PHead->y_3); PRINTF}if (PHead->y_4 < y){MoveCursor(PHead->x_4, PHead->y_4); EMPTYPHead->y_4 += 1;MoveCursor(PHead->x_4, PHead->y_4); PRINTF}PHead = PHead->next;}
}

12.显示信息

void Show(int n)
{//显示下一个方块//先清空该区域for (int j = 4; j <= 8; j++){for (int i = 23; i <= 28; i++){MoveCursor(i, j);  EMPTY}}MoveCursor(24, 3);printf("下一个方块种类:");MoveCursor(24, 10);printf("游戏得分:%d", fengs);MoveCursor(24, 12);printf("花狗Fdog出品,必属精品。");if (n == 1){SetColour(13);Location_x(-1, 0, 0, 0, 1, 0, -1, -1);}if (n == 2){SetColour(15);Location_x(0, 0, 1, 0, -1, 1, 1, 1);}if (n == 3){SetColour(12);Location_x(0, 0, 1, 0, 2, 0, 3, 0);}if (n == 4){SetColour(10);Location_x(0, 0, 1, 0, 2, 0, 2, -1);}if (n == 5){SetColour(6);Location_x(0, -1, 0, 0, 1, 0, 2, 0);}if (n == 6){SetColour(4);Location_x(-1, -1, -1, 0, 0, 0, 0, 1);}if (n == 7){SetColour(8);Location_x(0, -1, 0, 0, -1, 0, -1, 1);}
}

13.坐标更新1

void Location(Tetris ** Block, int x, int y, int a, int b, int c, int d, int e, int f)
{(*Block)->x_1 = (*Block)->x_1 + x;(*Block)->y_1 = (*Block)->y_1 + y;(*Block)->x_2 = (*Block)->x_1 + a;(*Block)->y_2 = (*Block)->y_1 + b;(*Block)->x_3 = (*Block)->x_1 + c;(*Block)->y_3 = (*Block)->y_1 + d;(*Block)->x_4 = (*Block)->x_1 + e;(*Block)->y_4 = (*Block)->y_1 + f;
}

14.坐标更新2

void Location_y(Tetris ** Block, int x, int y, int a, int b, int c, int d, int e, int f)
{(*Block)->x_1 = (*Block)->x_1 + x;(*Block)->y_1 = (*Block)->y_1 + y;(*Block)->x_2 = (*Block)->x_2 + a;(*Block)->y_2 = (*Block)->y_2 + b;(*Block)->x_3 = (*Block)->x_3 + c;(*Block)->y_3 = (*Block)->y_3 + d;(*Block)->x_4 = (*Block)->x_4 + e;(*Block)->y_4 = (*Block)->y_4 + f;
}

15.显示信息更新

void Location_x(int x, int y, int a, int b, int c, int d, int e, int f)
{MoveCursor(Loca_x + x, Loca_y + y); PRINTFMoveCursor(Loca_x + a, Loca_y + b); PRINTFMoveCursor(Loca_x + c, Loca_y + d); PRINTFMoveCursor(Loca_x + e, Loca_y + f); PRINTF
}

16.主函数内容

int main()
{DeawMap();code_y = rand() % 7 + 1;while (1){Return = BlockRand(code_y);code_y = rand() % 7 + 1;Show(code_y);ShowBlock(&Return);}system("pause>nul");return 0;
}

三.完整代码奉上,也可自行下载

//vs2015编译运行
#include<stdio.h>
#include<time.h>
#include<Windows.h>
#define HEIGHT  28  //设置地图高度
#define WIDTH   20  //设置地图宽度
#define ZERO    1
#define HEIGHT_1 18
#define Loca_y    6
#define Loca_x    25
#define PRINTF  printf("■");
#define LINE    printf("\n");
#define EMPTY   printf("  ");
typedef struct Tetris
{int x_1, y_1;   //主x坐标,主y坐标,下面三个为附属,通过主坐标确定三个附属int x_2, y_2;int x_3, y_3;int x_4, y_4;int code;//7种方块形态代号Tetris * next;
}Tetris;
void DeawMap();                     	//绘制地图 
Tetris * BlockRand(int code);    	 	//随机主方块生成  
void JudgeDirection(Tetris ** Block); //按键响应 
void Form(Tetris ** Block);      		//方块坐标全部确定
void ShowBlock(Tetris ** Block);    	//显示完整方块 
int JudgeWall(Tetris ** Block);     	//判断左右界限
void MoveCursor(int x, int y);      	//移动光标  不闪屏是因为每次不会刷新全部地图,只会刷新某一特定区域
void SetColour(int c);              	//颜色设定
int JudgeGroud(Tetris * Phead, Tetris ** Block);//判断落地
void JudgeEntire(Tetris * Head);    	//判断整行是否填满
void NewEntire(Tetris * head, int y);  //若上面函数成立,若清除该行,并刷新地图
void Show(int n);        			//显示信息,下一个方块得分情况,如果想加入一些信息可以在该函数内修改
void Location(Tetris ** Block, int x, int y, int a, int b, int c, int d, int e, int f);   //坐标更新1
void Location_y(Tetris ** Block, int x, int y, int a, int b, int c, int d, int e, int f); //坐标更新2
void Location_x(int x, int y, int a, int b, int c, int d, int e, int f);                  //信息更新
Tetris *Phead = NULL;   //链表头指针
Tetris *Pend = NULL;    //跟随指针 (尾插法需要)
Tetris * Return = NULL; //节点地址返回
int form = 0; //判断形态
int UP = 0;   //下降速度判断
int code_y = 0;//随机形态
int fengs = 0; //得分int main()
{DeawMap();code_y = rand() % 7 + 1;while (1){Return = BlockRand(code_y);code_y = rand() % 7 + 1;Show(code_y);ShowBlock(&Return);}system("pause>nul");return 0;
}void DeawMap()
{for (int i = 0; i < WIDTH; i++)PRINTF LINE  //上边框for (int i = 1; i < HEIGHT - 1; i++)          //打印左右边框{for (int j = 0; j < WIDTH; j++){if (j == 0 || j == WIDTH - 1){PRINTFif (j == WIDTH - 1)LINE}else EMPTY}}for (int i = 0; i < WIDTH; i++)PRINTF LINE  //下边框system("color 03");
}void Location(Tetris ** Block, int x, int y, int a, int b, int c, int d, int e, int f)
{(*Block)->x_1 = (*Block)->x_1 + x;(*Block)->y_1 = (*Block)->y_1 + y;(*Block)->x_2 = (*Block)->x_1 + a;(*Block)->y_2 = (*Block)->y_1 + b;(*Block)->x_3 = (*Block)->x_1 + c;(*Block)->y_3 = (*Block)->y_1 + d;(*Block)->x_4 = (*Block)->x_1 + e;(*Block)->y_4 = (*Block)->y_1 + f;
}void Location_y(Tetris ** Block, int x, int y, int a, int b, int c, int d, int e, int f)
{(*Block)->x_1 = (*Block)->x_1 + x;(*Block)->y_1 = (*Block)->y_1 + y;(*Block)->x_2 = (*Block)->x_2 + a;(*Block)->y_2 = (*Block)->y_2 + b;(*Block)->x_3 = (*Block)->x_3 + c;(*Block)->y_3 = (*Block)->y_3 + d;(*Block)->x_4 = (*Block)->x_4 + e;(*Block)->y_4 = (*Block)->y_4 + f;
}Tetris * BlockRand(int code_y)
{srand((int)time(0));Tetris * Block = (Tetris*)malloc(sizeof(Tetris));Block->x_1 = 8;Block->y_1 = 4;//规定初始中心方块的坐标为(8,4)Block->code = code_y;if (Phead == NULL)Phead = Block;else Pend->next = Block;Block->next = NULL;Pend = Block;return Block;
}void ShowBlock(Tetris ** Block)
{while (1){Form(&Return); if ((*Block)->code == 1)SetColour(13);if ((*Block)->code == 2)SetColour(15);if ((*Block)->code == 3)SetColour(12);if ((*Block)->code == 4)SetColour(10);if ((*Block)->code == 5)SetColour(6);if ((*Block)->code == 6)SetColour(4);if ((*Block)->code == 7)SetColour(8);MoveCursor((*Block)->x_1, (*Block)->y_1); PRINTFMoveCursor((*Block)->x_2, (*Block)->y_2); PRINTFMoveCursor((*Block)->x_3, (*Block)->y_3); PRINTFMoveCursor((*Block)->x_4, (*Block)->y_4); PRINTFif (JudgeGroud(Phead, &Return) == 0){system("color 03");break;}if (UP == 0){for (int i = 0; i <= 400000000; i++) {}}if (UP == 1){for (int i = 0; i <= 40000000; i++) {}UP = 0;}MoveCursor((*Block)->x_1, (*Block)->y_1); EMPTYMoveCursor((*Block)->x_2, (*Block)->y_2); EMPTYMoveCursor((*Block)->x_3, (*Block)->y_3); EMPTYMoveCursor((*Block)->x_4, (*Block)->y_4); EMPTYLocation_y(&Return, 0, 1, 0, 1, 0, 1, 0, 1);JudgeDirection(&Return);JudgeEntire(Phead);}
}void JudgeDirection(Tetris ** Block)
{if (GetAsyncKeyState(VK_UP) && 0x8000){form += 1;if (form == 4){form = 0;}Form(&Return);}if (GetAsyncKeyState(VK_DOWN) && 0x8000){//加速向下  时间加速UP = 1;}if (GetAsyncKeyState(VK_LEFT) && 0x8000){//向左移动if (JudgeWall(&Return) != -1) Location_y(&Return, -1, 0, -1, 0, -1, 0, -1, 0);}if (GetAsyncKeyState(VK_RIGHT) && 0x8000){//向右移动if (JudgeWall(&Return) != -2) Location_y(&Return, 1, 0, 1, 0, 1, 0, 1, 0);}if (GetAsyncKeyState(VK_ESCAPE) && 0x0D){MoveCursor(27, 15);printf("游戏暂停");//判断Escwhile (1){if (GetAsyncKeyState(VK_ESCAPE) && 0x0D){MoveCursor(27, 15);printf("       ");break;}}}
}void Form(Tetris ** Block)
{//先确实哪一类,再细分switch ((*Block)->code){case 1:if (form == 0)Location(&Return, 0, 0, -1, 0, 0, -1, 1, 0);if (form == 1)Location(&Return, 0, 0, 0, 1, 0, -1, 1, 0);if (form == 2)Location(&Return, 0, 0, 0, 1, -1, 0, 1, 0);if (form == 3)Location(&Return, 0, 0, 0, 1, -1, 0, 0, -1);break;case 2:Location(&Return, 0, 0, 1, 0, 0, 1, 1, 1);break;case 3:if (form == 0 || form == 2)Location(&Return, 0, 0, 0, -1, 0, 1, 0, 2);if (form == 1 || form == 3)Location(&Return, 0, 0, -1, 0, 1, 0, 2, 0);break;case 4:if (form == 0)Location(&Return, 0, 0, -1, 0, 1, 0, 1, -1);if (form == 1)Location(&Return, 0, 0, 0, -1, 1, 0, 0, -2);if (form == 2)Location(&Return, 0, 0, 0, -1, 1, -1, 2, -1);if (form == 3)Location(&Return, 0, 0, 0, -1, 0, -2, -1, -2);break;case 5:if (form == 0)Location(&Return, 0, 0, 1, 0, 2, 0, 0, -1);if (form == 1)Location(&Return, 0, 0, 1, 0, 1, -1, 1, -2);if (form == 2)Location(&Return, 0, 0, 1, 0, 2, 0, 2, 1);if (form == 3)Location(&Return, 0, 0, 1, 0, 0, 1, 0, 2);break;case 6:if (form == 0 || form == 2)Location(&Return, 0, 0, 0, -1, 1, 0, 1, 1);if (form == 1 || form == 3)Location(&Return, 0, 0, 0, -1, 1, -1, -1, 0);break;case 7:if (form == 0 || form == 2)Location(&Return, 0, 0, 0, 1, 1, 0, 1, -1);if (form == 1 || form == 3)Location(&Return, 0, 0, 0, 1, -1, 0, 1, 1);}
}void MoveCursor(int x, int y)//设置光标位置(就是输出显示的开始位置)
{COORD pos = { x * 2,y };HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);//获得 标准输出的句柄   SetConsoleCursorPosition(output, pos); //设置控制台光标位置
}void SetColour(int c)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);//API函数可以改变控制台颜色
}
int JudgeWall(Tetris ** Block)
{if ((*Block)->x_1 == ZERO || (*Block)->x_2 == ZERO || (*Block)->x_3 == ZERO || (*Block)->x_4 == ZERO)return -1;if ((*Block)->x_1 == HEIGHT_1 || (*Block)->x_2 == HEIGHT_1 || (*Block)->x_3 == HEIGHT_1 || (*Block)->x_4 == HEIGHT_1)return -2;return 0;
}int JudgeGroud(Tetris * Phead, Tetris ** Block)
{Tetris * P = Phead;//如果到达最低层。直接经行下一循环if ((*Block)->y_1 == 26 || (*Block)->y_2 == 26 || (*Block)->y_3 == 26 || (*Block)->y_4 == 26)return 0;while (P->next != NULL){if (P->y_1 == (*Block)->y_1 + 1){if (P->x_1 == (*Block)->x_1)return 0;}if (P->y_2 == (*Block)->y_1 + 1){if (P->x_2 == (*Block)->x_1)return 0;}if (P->y_3 == (*Block)->y_1 + 1){if (P->x_3 == (*Block)->x_1)return 0;}if (P->y_4 == (*Block)->y_1 + 1){if (P->x_4 == (*Block)->x_1)return 0;}if (P->y_1 == (*Block)->y_2 + 1){if (P->x_1 == (*Block)->x_2)return 0;}if (P->y_2 == (*Block)->y_2 + 1){if (P->x_2 == (*Block)->x_2)return 0;}if (P->y_3 == (*Block)->y_2 + 1){if (P->x_3 == (*Block)->x_2)return 0;}if (P->y_4 == (*Block)->y_2 + 1){if (P->x_4 == (*Block)->x_2)return 0;}if (P->y_1 == (*Block)->y_3 + 1){if (P->x_1 == (*Block)->x_3)return 0;}if (P->y_2 == (*Block)->y_3 + 1){if (P->x_2 == (*Block)->x_3)return 0;}if (P->y_3 == (*Block)->y_3 + 1){if (P->x_3 == (*Block)->x_3)return 0;}if (P->y_4 == (*Block)->y_3 + 1){if (P->x_4 == (*Block)->x_3)return 0;}if (P->y_1 == (*Block)->y_4 + 1){if (P->x_1 == (*Block)->x_4)return 0;}if (P->y_2 == (*Block)->y_4 + 1){if (P->x_2 == (*Block)->x_4)return 0;}if (P->y_3 == (*Block)->y_4 + 1){if (P->x_3 == (*Block)->x_4)return 0;}if (P->y_4 == (*Block)->y_4 + 1){if (P->x_4 == (*Block)->x_4)return 0;}P = P->next;}return 1;
}void JudgeEntire(Tetris * Head)
{Tetris * PHead = Head;//从1到26for (int y = 26; y >= 1; y--){int sum = 0;while (PHead->next != NULL){if (PHead->y_1 == y)sum++;if (PHead->y_2 == y)sum++;if (PHead->y_3 == y)sum++;if (PHead->y_4 == y)sum++;MoveCursor(20, 28);PHead = PHead->next;}PHead = Head;if (sum == 18){//如果成行则,执行NewEntire()  清空该行,并将所有y下降一个单位。NewEntire(Phead, y);fengs += 10;Show(code_y);}sum = 0;}
}void NewEntire(Tetris * head,int y)
{Tetris * PHead = head;while (PHead->next != NULL){if (PHead->y_1 == y){MoveCursor(PHead->x_1, PHead->y_1); EMPTYPHead->x_1 = 99;PHead->y_1 = 99;}if (PHead->y_2 == y){MoveCursor(PHead->x_2, PHead->y_2); EMPTYPHead->x_2 = 99;PHead->y_2 = 99;}if (PHead->y_3 == y){MoveCursor(PHead->x_3, PHead->y_3); EMPTYPHead->x_3 = 99;PHead->y_3 = 99;}if (PHead->y_4 == y){MoveCursor(PHead->x_4, PHead->y_4); EMPTYPHead->x_4 = 99;PHead->y_4 = 99;}PHead = PHead->next;}PHead = head;while (PHead->next != NULL){if (PHead->y_1 < y){MoveCursor(PHead->x_1, PHead->y_1); EMPTYPHead->y_1 += 1;MoveCursor(PHead->x_1, PHead->y_1); PRINTF}if (PHead->y_2 < y){MoveCursor(PHead->x_2, PHead->y_2); EMPTYPHead->y_2 += 1;MoveCursor(PHead->x_2, PHead->y_2); PRINTF}if (PHead->y_3 < y){MoveCursor(PHead->x_3, PHead->y_3); EMPTYPHead->y_3 += 1;MoveCursor(PHead->x_3, PHead->y_3); PRINTF}if (PHead->y_4 < y){MoveCursor(PHead->x_4, PHead->y_4); EMPTYPHead->y_4 += 1;MoveCursor(PHead->x_4, PHead->y_4); PRINTF}PHead = PHead->next;}
}void Show(int n)
{//显示下一个方块//先清空该区域for (int j = 4; j <= 8; j++){for (int i = 23; i <= 28; i++){MoveCursor(i, j);  EMPTY}}MoveCursor(24, 3); printf("下一个方块种类:");MoveCursor(24, 10);printf("游戏得分:%d", fengs);MoveCursor(24, 12);printf("花狗Fdog出品,必属精品。");if (n == 1){SetColour(13);Location_x(-1, 0, 0, 0, 1, 0, -1, -1);}if (n == 2){SetColour(15);Location_x(0, 0, 1, 0, -1, 1, 1, 1);}if (n == 3){SetColour(12);Location_x(0, 0, 1, 0, 2, 0, 3, 0);}if (n == 4){SetColour(10);Location_x(0, 0, 1, 0, 2, 0, 2, -1);}if (n == 5){SetColour(6);Location_x(0, -1, 0, 0, 1, 0, 2, 0);}if (n == 6){SetColour(4);Location_x(-1, -1, -1, 0, 0, 0, 0, 1);}if (n == 7){SetColour(8);Location_x(0, -1, 0, 0, -1, 0, -1, 1);}
}void Location_x(int x, int y, int a, int b, int c, int d, int e, int f)
{MoveCursor(Loca_x+x, Loca_y+y); PRINTFMoveCursor(Loca_x+a, Loca_y+b); PRINTFMoveCursor(Loca_x+c, Loca_y+d); PRINTFMoveCursor(Loca_x+e, Loca_y+f); PRINTF
}

若有错误,欢迎指正批评,欢迎讨论。
每文一句:生活,就是面对现实微笑,就是越过障碍注视未来;生活,就是用心灵之剪,在人生之路上裁出叶绿的枝头;生活,就是面对困惑或黑暗时,灵魂深处燃起豆大却明亮且微笑的灯展。


http://chatgpt.dhexx.cn/article/bRwpoLrl.shtml

相关文章

HTML5俄罗斯方块网页游戏代码

HTML5俄罗斯方块网页游戏代码 非常好用 代码如下 注意 把所有文件放在一个文件夹里! 把所有css文件夹里,命名css 把所有js文件夹里,命名js 先看index.html <!DOCTYPE html> <html> <head><meta charset"utf-8"><title>俄罗斯方块…

俄罗斯方块C++语言代码

大家都知道俄罗斯方块&#xff0c;就是控制不同的方块旋转&#xff0c;通过摆满一行方块消除得分&#xff0c;今天就让我们用C制作一个俄罗斯方块小游戏。 首先定义方块&#xff1a; int block00[4][4] { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } }; int block01[…

c++俄罗斯方块 完整源代码

文章目录 1. 功能描述2. 源代码3. 运行截图4. 结尾 1. 功能描述 2. 源代码 #include<iostream> #include<string> #include<cstdlib> #include<windows.h> #include<ctime> #include<conio.h> #include<cstdio> using namespace st…

俄罗斯方块游戏代码

♥️作者&#xff1a;小刘在C站 ♥️个人主页&#xff1a;小刘主页 ♥️每天分享云计算网络运维课堂笔记&#xff0c;努力不一定有回报&#xff0c;但一定会有收获加油&#xff01;一起努力&#xff0c;共赴美好人生&#xff01; ♥️夕阳下&#xff0c;是最美的&#xff0c;绽…

C语言实现俄罗斯方块

目录 一、游戏效果展示 二、完整代码&#xff0c;可以直接拷贝运行 三、所需开发环境 四、具体项目实现 ①游戏欢迎界面 welcome( ) ②游戏背景 initGameScreen( ) ③方块表示 int block[ ][ ][ ] ④新方块表示nextBlock( ) ⑤设计游戏循环main( ) ⑥搭建用户操作框…

❤️VS Code❤️,cmd终端窗口运行,解决中文乱码问题

目录 问题归纳VS Code默认终端配置window窗口弹出相关解释 解决中文乱码问题运行俄罗斯方块程序 问题归纳 在软件Visual Studio中一切都是配置好的&#xff0c;默认终端运行采用弹出cmd窗口形式。而在VS Code中除了一些默认的配置设置外&#xff0c;所有环境配置、界面显示等操…

俄罗斯方块(C语言实现)

文章目录 游戏说明游戏效果展示游戏代码游戏代码详解游戏框架构建隐藏光标光标跳转初始化界面初始化方块信息颜色设置画出方块空格覆盖合法性判断判断得分与结束游戏主体逻辑函数从文件读取最高分更新最高分到文件主函数 游戏说明 俄罗斯方块相信大家都知道&#xff0c;这里就…

IDEA 设置类注释模板

效果展示 步骤&#xff1a;File-->settings-->Editor-->File and Code Templates-->Files 选择Class文件&#xff08;当然你要设置接口的还也可以选择Interface文件&#xff09;

设置idea类注释模板

1.File-->Settings...&#xff0c;或者直接按快捷键CtrlAltS. 2. Editor-->File and Code Templates-->Includes-->File Header,根据右下角Description的提示&#xff0c;添加自己需要的注释。 3.在右侧空白处设置自己的模板&#xff0c;若提示cant parse class.可…

IDEA 创建类注释模板设置

1.idea类注释 打开&#xff1a;file->setting->Editor->Filr and Code Templates->Includes->File Header 类注释模板&#xff1a; /** * title: ${NAME}* projectName ${PROJECT_NAME}* description: TODO* author ${USER}* date ${DATE}${TIME}*/ 2.方法注释…

java idea 配置注释模板

java idea 配置注释模板 最近项目组在加强代码规范管理。代码优雅从代码注释开始。奥力给&#xff01;&#xff01;&#xff01; 类注释的 File >> Settings >> Editor >> File and code Templates >> Files >> class 上图中的模板内容如下&…

idea类注释模板,方法注释模板。

idea设置--实时模板 新建组 类模板 /*** description $description$* author YJJ* date $date$ $time$* version 1.0*/ 方法模板 ** * description $description$ $params$ $return$* author YJJ* date $date$ $time$*/ params脚本&#xff1a; groovyScript("def r…

IDEA代码注释模板

存在问题: 每次写代码&#xff0c;新建类都没有注释&#xff0c;这对一个拥有代码强迫症患者来说&#xff0c;是多么不爽的一件事&#xff0c;不利于开发和后期问题排查 举个例子&#xff1a; 上次写代码的时候&#xff0c;这一段只有我和上帝知道&#xff0c;而现在&#xff0…

idea添加注释模板

自动添加描述 class // 创建时间有的也没有 仅仅就是class的描述信息&#xff0c;看个人公司情况吧&#xff0c;描述信息最好创建时间有一个空行&#xff0c;我忘记了/** * 描述信息 * * create: ${YEAR}-${MONTH}-${DAY} ${HOUR}:${MINUTE} */method // 一会要写入模板的内…

【idea生成类注释模板快捷键设置】

idea类注释模板快捷键 设置在方法上生成注释的快捷键1&#xff0c;ctrlaltS打开idea设置设置模板中的参数取值来源使用效果展示 设置在方法上生成注释的快捷键 1&#xff0c;ctrlaltS打开idea设置 设置模板中的参数取值来源 使用效果展示

IntelliJ IDEA类和方法注释模板配置

1、设置文件注释 设置Java类的注释模板&#xff0c;创建Java类自动生成该注释模板。 先看文件注释效果&#xff1a; 1.1、选择 IntelliJ IDEA —> 选择 Preferences... 打开IDEA属性设置面板 1.2、选择 File Code Templates—>选择 Includes 选项卡—>选择 File He…

IDEA配置方法类注释模板

IDEA配置方法类注释模板 IDEA20.1配置方法注释注释的种类注释使用规范 摘自阿里开发手册 IDEA方法模板注释一、打开idea 点击菜单File->Setting-Editor->Live Template二、选择Live Template 添加快键键缩写注释模板代码三、配置Edit variables四、配置入参和返回字段的d…

IDEA类和方法注释模板配置

1、类的注释模板配置 File-->Settings-->Editor-->File and Code Templates-->File --->Class: 模板代码下所示&#xff1a; /*** ClassName: ${NAME}* Author : ${USER}* Date :${DATE} ${TIME}* Description: TODO* Version :1.0*/ 上述${NAME}为类名&…

IDEA方法注释模板设置

IDEA类和方法注释模板设置 1、File–>Settings–>Editor–>Live Templates &#xff08;1&#xff09;新建组&#xff1a;命名为userDefine &#xff08;2&#xff09;新建模板&#xff1a;选中你刚才创建的组&#xff0c;然后右上角新建模板&#xff0c;命名为* &…

IDEA注释模板配置

最初&#xff0c;从Eclipse转到IDEA&#xff0c;总是不习惯IDEA的注释方式&#xff0c;所以在网上找来找去加上自己的测试&#xff0c;终于形成一套相对详细的配置流程。之前每次用到都靠收藏里的别人文章应付&#xff0c;但是在配置新的开发环境或者有新的小组成员加入时总要用…