俄罗斯方块小demo

来源:互联网 发布:怪兽使者与少年知乎 编辑:程序博客网 时间:2024/05/16 09:08

其实c++早已经学过,这几天突发奇想又想提升一下c++水平,所以想做点东西出来,于是就选择了做这个俄罗斯方块demo,花了点时间,也做的挺开心的学到了些东西,所以发出来与大家分享,首先说几点心得吧。

心得:

1.好的程序结构是好程序的一半,当你构建了好的结构后,会为你节约出大量的时间与精力,同时好的结构也会使得添加功能更加快捷方便(这也是为啥架构师比程序员更高级)(这里为了方便上传代码所以没有用类之类的面向对象写法)

2.功能尽量模块化,能用函数就用函数,否则修改会相当麻烦

3.调试对于学程序的绝对是必不可少的技能,单步调试相当重要!!!!!!!

4.自学相当重要,基础要不断巩固,电脑技术更新之快超出人想象,但是基础的东西短时间内不可能改变,自学细节也就可以使得自己的小demo更加优秀,积累下来会很有收获

5.学会找到乐趣,虽然这只是一个很小的游戏,但是是自己做出来的感觉就十分自豪(而且自己做出来可以与大家一起分享的东西更是超级开心,成就感十足),而且也并不一定要做成原来的样子,可以搞些创意(我的目前没有,但是搭建好结构后,我添加功能是十分快捷的)

6.学会写注释,否则以后程序越来越大就会遗忘(就算你自己写的也是如此)

7.学会记录所学,因为也会遗忘,可以就这样写写博客或者其他方法

样例图片:

开始界面:

游戏中间:

游戏结束:

细节知识:

1.rect与二维数组的对应关系,rect是以左上角为原点以X,Y轴延伸,而二维数组的行与列与rect对应恰好相反,用时需要注意一下

2.&引用符号,简单来说就是可以使函数的变化得以保存(完整用法请百度(你搜狗我也没办法,略略略得意得意得意))

3.我的这个demo中的方块占了两个列,需注意其坐标与行与列的关系

4.全局变量,越大的项目越要谨慎使用

5.做东西出来要有三分之一的时间用来思考搭建结构,还有三分之一用来优化结构,使代码简单,真正敲代码的时间并不一定会很多(当然如果是学习打基础时,当然要多敲代码,提升实力我认为最好的方法就是有具体的东西研究,学习,最后攻克)

6.一个函数一个功能,一个地方蕴含太多功能可能会使程序时间线混乱

好了,目前就想到这么多了吧,我会将源代码上传,相信会对一些人有些帮助,主要功能我都注释了,但是细节问题我也没有把握说清,只有自学探索才是王道,另外我的水平也有待提高,所以希望大家多留言批评指正。

代码:

#include<iostream>#include <string>#include <ctime>#include <cstdlib>#include <windows.h>#include <conio.h>using namespace std;int map[21][12], grade = 0;//矩阵记录游戏境况,以及得了多少分struct fangkuai{int kind;//记录方块的种类COORD pos1, pos2, pos3, pos4;//方块四个点的坐标int situation;//状态形状bool ifstop = false;//是否停下}a, b;void givefangkuaipos(fangkuai &a)//以一个点为标准推算出每个点的坐标{if (a.kind == 1)//以方块一个点为标准点结合其形状就可以推出所有方块位置{if (a.situation == 1){a.pos2.X = a.pos1.X + 2;a.pos3.X = a.pos2.X + 2;a.pos4.X = a.pos3.X + 2;a.pos2.Y = a.pos3.Y = a.pos4.Y = a.pos1.Y;}else if (a.situation == 2){a.pos2.Y = a.pos1.Y + 1;a.pos3.Y = a.pos2.Y + 1;a.pos4.Y = a.pos3.Y + 1;a.pos2.X = a.pos3.X = a.pos4.X = a.pos1.X;}}else if (a.kind == 2){a.pos2.X = a.pos1.X + 2;a.pos2.Y = a.pos1.Y;a.pos3.X = a.pos1.X + 2;a.pos3.Y = a.pos1.Y + 1;a.pos4.X = a.pos1.X;a.pos4.Y = a.pos1.Y + 1;}else if (a.kind == 3){if (a.situation == 1){a.pos2.X = a.pos1.X + 2;a.pos2.Y = a.pos1.Y;a.pos3.X = a.pos1.X + 2;a.pos3.Y = a.pos1.Y - 1;a.pos4.X = a.pos1.X + 2;a.pos4.Y = a.pos1.Y - 2;}else if (a.situation == 2){a.pos2.X = a.pos1.X;a.pos2.Y = a.pos1.Y + 1;a.pos3.X = a.pos1.X + 2;a.pos3.Y = a.pos1.Y + 1;a.pos4.X = a.pos1.X + 4;a.pos4.Y = a.pos1.Y + 1;}else if (a.situation == 3){a.pos2.X = a.pos1.X - 2;a.pos2.Y = a.pos1.Y;a.pos3.X = a.pos1.X - 2;a.pos3.Y = a.pos1.Y + 1;a.pos4.X = a.pos1.X - 2;a.pos4.Y = a.pos1.Y + 2;}else if (a.situation == 4){a.pos2.X = a.pos1.X;a.pos2.Y = a.pos1.Y - 1;a.pos3.X = a.pos1.X - 2;a.pos3.Y = a.pos1.Y - 1;a.pos4.X = a.pos1.X - 4;a.pos4.Y = a.pos1.Y - 1;}}else if (a.kind == 4){if (a.situation == 1){a.pos2.X = a.pos1.X - 2;a.pos2.Y = a.pos1.Y;a.pos3.X = a.pos1.X + 2;a.pos3.Y = a.pos1.Y;a.pos4.X = a.pos1.X;a.pos4.Y = a.pos1.Y - 1;}else if (a.situation == 2){a.pos2.X = a.pos1.X;a.pos2.Y = a.pos1.Y - 1;a.pos3.X = a.pos1.X;a.pos3.Y = a.pos1.Y + 1;a.pos4.X = a.pos1.X + 2;a.pos4.Y = a.pos1.Y;}else if (a.situation == 3){a.pos2.X = a.pos1.X - 2;a.pos2.Y = a.pos1.Y;a.pos3.X = a.pos1.X + 2;a.pos3.Y = a.pos1.Y;a.pos4.X = a.pos1.X;a.pos4.Y = a.pos1.Y + 1;}else if (a.situation == 4){a.pos2.X = a.pos1.X;a.pos2.Y = a.pos1.Y - 1;a.pos3.X = a.pos1.X;a.pos3.Y = a.pos1.Y + 1;a.pos4.X = a.pos1.X - 2;a.pos4.Y = a.pos1.Y;}}else if (a.kind == 5){if (a.situation == 1){a.pos2.X = a.pos1.X - 2;a.pos2.Y = a.pos1.Y;a.pos3.X = a.pos1.X - 2;a.pos3.Y = a.pos1.Y - 1;a.pos4.X = a.pos1.X - 2;a.pos4.Y = a.pos1.Y - 2;}else if (a.situation == 2){a.pos2.X = a.pos1.X;a.pos2.Y = a.pos1.Y - 1;a.pos3.X = a.pos1.X + 2;a.pos3.Y = a.pos1.Y - 1;a.pos4.X = a.pos1.X + 4;a.pos4.Y = a.pos1.Y - 1;}else if (a.situation == 3){a.pos2.X = a.pos1.X + 2;a.pos2.Y = a.pos1.Y;a.pos3.X = a.pos1.X + 2;a.pos3.Y = a.pos1.Y + 1;a.pos4.X = a.pos1.X + 2;a.pos4.Y = a.pos1.Y + 2;}else if (a.situation == 4){a.pos2.X = a.pos1.X;a.pos2.Y = a.pos1.Y + 1;a.pos3.X = a.pos1.X - 2;a.pos3.Y = a.pos1.Y + 1;a.pos4.X = a.pos1.X - 4;a.pos4.Y = a.pos1.Y + 1;}}}void intlwindow(HANDLE &table)//初始化游戏界面{for (int i = 0; i < 21; i++)//三个for循环画出二维数组的边框{for (int j = 0; j < 11; j++){map[i][j] = 0;}}for (int i = 0; i < 21; i++){map[i][0] = 1;map[i][11] = 1;}for (int i = 0; i < 12; i++){map[20][i] = 1;}COORD size = { 80,25 };//画出矩阵(有点坑,请看单独的笔记)SetConsoleScreenBufferSize(table, size);SMALL_RECT rect = { 0,0,79,24 };SetConsoleWindowInfo(table, true, &rect);CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };SetConsoleCursorInfo(table, &cursor_info);}void movemouse(HANDLE &table, int x, int y)//移动光标(就是黑框框闪的那玩意){COORD pos;//一种结构,坐标点pos.X = x; pos.Y = y;SetConsoleCursorPosition(table, pos);}void draw(HANDLE &table, fangkuai &a)//画出方块,并在二维数组中记录下来{movemouse(table, a.pos1.X, a.pos1.Y);cout << "■"; map[a.pos1.Y][a.pos1.X / 2] = 1;movemouse(table, a.pos2.X, a.pos2.Y);cout << "■"; map[a.pos2.Y][a.pos2.X / 2] = 1;movemouse(table, a.pos3.X, a.pos3.Y);cout << "■"; map[a.pos3.Y][a.pos3.X / 2] = 1;movemouse(table, a.pos4.X, a.pos4.Y);cout << "■"; map[a.pos4.Y][a.pos4.X / 2] = 1;}void clear(HANDLE &table, fangkuai a)//清空上一次的方块并在二维数组中也消去{movemouse(table, a.pos1.X, a.pos1.Y);cout << "  "; map[a.pos1.Y][a.pos1.X / 2] = 0;movemouse(table, a.pos2.X, a.pos2.Y);cout << "  "; map[a.pos2.Y][a.pos2.X / 2] = 0;movemouse(table, a.pos3.X, a.pos3.Y);cout << "  "; map[a.pos3.Y][a.pos3.X / 2] = 0;movemouse(table, a.pos4.X, a.pos4.Y);cout << "  "; map[a.pos4.Y][a.pos4.X / 2] = 0;}void readygame(HANDLE &table)//做出帅气界面,我觉得哈哈哈{cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;cout << "■           欢迎光临我的俄罗斯方块!           ■" << endl;cout << "■           1.w控制方块变形                    ■" << endl;cout << "■           2.a向左移动,d向右移动             ■" << endl;cout << "■           3.s加速向下,空格键暂停             ■" << endl;cout << "■           4.注意使用英文输入法!!!            ■" << endl;cout << "■                                              ■" << endl;cout << "■                                              ■" << endl;cout << "■        请输入yes开始游戏:                 ■" << endl;cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;movemouse(table, 28, 8);string str;while (1){cin >> str;//输入yes才开始游戏if (str == "yes"){system("cls");SetConsoleTextAttribute(table, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);for (int i = 0; i < 20; ++i)//界面做出边框{cout << "■                    ■☆                      ☆" << endl;}movemouse(table, 26, 0);cout << "☆☆☆☆☆☆☆☆☆☆☆";movemouse(table, 0, 20);cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆";movemouse(table, 26, 1);cout << "下一方块:      ";movemouse(table, 26, 12);cout << "分    数:  ";movemouse(table, 26, 16);SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN);cout << "制 作 人: Changing   ";break;}else{movemouse(table, 28, 8); cout << "             ";movemouse(table, 28, 8);}}}bool judgement_left(fangkuai a)//判断是否方块可以向左移动{if (a.kind == 1){if (a.situation == 1){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos2.Y][a.pos2.X / 2 - 1] == 1 ||map[a.pos3.Y][a.pos3.X / 2 - 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}}else if (a.kind == 2){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}else if (a.kind == 3){if (a.situation == 1){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 - 1] == 1|| map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos2.Y][a.pos2.X / 2 - 1] == 1){return false;}}else if (a.situation == 3){if (map[a.pos2.Y][a.pos2.X / 2 - 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 - 1] == 1|| map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}else if (a.situation == 4){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}}else if (a.kind == 4){if (a.situation == 1){if (map[a.pos2.Y][a.pos2.X / 2 - 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos2.Y][a.pos2.X / 2 - 1] == 1|| map[a.pos3.Y][a.pos3.X / 2 - 1] == 1){return false;}}else if (a.situation == 3){if (map[a.pos2.Y][a.pos2.X / 2 - 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}else if (a.situation == 4){if (map[a.pos2.Y][a.pos2.X / 2 - 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 - 1] == 1|| map[a.pos3.Y][a.pos3.X / 2 - 1] == 1){return false;}}}else if (a.kind == 5){if (a.situation == 1){if (map[a.pos2.Y][a.pos2.X / 2 - 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 - 1] == 1 ||map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos2.Y][a.pos1.X / 2 - 1] == 1){return false;}}else if (a.situation == 3){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 - 1] == 1|| map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}else if (a.situation == 4){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 - 1] == 1){return false;}}}return true;}bool judgement_right(fangkuai a)//判断是否方块可以向右移动{if (a.kind == 1){if (a.situation == 1){if (map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos2.Y][a.pos2.X / 2 + 1] == 1 ||map[a.pos3.Y][a.pos3.X / 2 + 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}}else if (a.kind == 2){if (map[a.pos2.Y][a.pos2.X / 2 + 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 + 1] == 1){return false;}}else if (a.kind == 3){if (a.situation == 1){if (map[a.pos2.Y][a.pos2.X / 2 + 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 + 1] == 1|| map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 3){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 + 1] == 1|| map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 4){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos2.Y][a.pos2.X / 2 + 1] == 1){return false;}}}else if (a.kind == 4){if (a.situation == 1){if (map[a.pos3.Y][a.pos3.X / 2 + 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos4.Y][a.pos4.X / 2 + 1] == 1 || map[a.pos2.Y][a.pos2.X / 2 + 1] == 1|| map[a.pos3.Y][a.pos3.X / 2 + 1] == 1){return false;}}else if (a.situation == 3){if (map[a.pos3.Y][a.pos3.X / 2 + 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 4){if (map[a.pos2.Y][a.pos2.X / 2 + 1] == 1 || map[a.pos1.Y][a.pos1.X / 2 + 1] == 1|| map[a.pos3.Y][a.pos3.X / 2 + 1] == 1){return false;}}}else if (a.kind == 5){if (a.situation == 1){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 + 1] == 1|| map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 3){if (map[a.pos2.Y][a.pos2.X / 2 + 1] == 1 || map[a.pos3.Y][a.pos3.X / 2 + 1] == 1|| map[a.pos4.Y][a.pos4.X / 2 + 1] == 1){return false;}}else if (a.situation == 4){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos2.Y][a.pos2.X / 2 + 1] == 1){return false;}}}return true;}bool judgement_down(fangkuai a)//判断是否可以下落{if (a.kind == 1){if (a.situation == 1){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos2.Y + 1][a.pos2.X / 2] == 1|| map[a.pos3.Y + 1][a.pos3.X / 2] == 1 || map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.situation == 2){if (map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}}else if (a.kind == 2){if (map[a.pos3.Y + 1][a.pos3.X / 2] == 1 || map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.kind == 3){if (a.situation == 1){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos2.Y + 1][a.pos2.X / 2] == 1){return false;}}else if (a.situation == 2){if (map[a.pos2.Y + 1][a.pos2.X / 2] == 1 || map[a.pos3.Y + 1][a.pos3.X / 2] == 1|| map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.situation == 3){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.situation == 4){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos3.Y + 1][a.pos3.X / 2] == 1|| map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}}else if (a.kind == 4){if (a.situation == 1){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos2.Y + 1][a.pos2.X / 2] == 1|| map[a.pos3.Y + 1][a.pos3.X / 2] == 1){return false;}}else if (a.situation == 2){if (map[a.pos3.Y + 1][a.pos3.X / 2] == 1 || map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.situation == 3){if (map[a.pos2.Y + 1][a.pos2.X / 2] == 1 || map[a.pos3.Y + 1][a.pos3.X / 2] == 1|| map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.situation == 4){if (map[a.pos3.Y + 1][a.pos3.X / 2] == 1 || map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}}else if (a.kind == 5){if (a.situation == 1){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos2.Y + 1][a.pos2.X / 2] == 1){return false;}}else if (a.situation == 2){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos3.Y + 1][a.pos3.X / 2] == 1|| map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.situation == 3){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}else if (a.situation == 4){if (map[a.pos2.Y + 1][a.pos2.X / 2] == 1 || map[a.pos3.Y + 1][a.pos3.X / 2] == 1|| map[a.pos4.Y + 1][a.pos4.X / 2] == 1){return false;}}}return true;}bool judgement_changing(fangkuai &a)////判断是否方块可以变化{if (a.kind == 1){if (a.situation == 1){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos1.Y + 2][a.pos1.X / 2] == 1|| map[a.pos1.Y + 3][a.pos1.X / 2] == 1){return false;}a.situation = 2;}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos1.Y][a.pos1.X / 2 + 2] == 1|| map[a.pos1.Y][a.pos1.X / 2 + 3] == 1){return false;}a.situation = 1;}}else if (a.kind == 2){return false;}else if (a.kind == 3){if (a.situation == 1){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos1.Y + 1][a.pos1.X / 2 + 1] == 1|| map[a.pos1.Y + 1][a.pos1.X / 2 + 2] == 1){return false;}a.situation = 2;}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos1.Y + 1][a.pos1.X / 2 - 1] == 1|| map[a.pos1.Y + 2][a.pos1.X / 2 - 1] == 1){return false;}a.situation = 3;}else if (a.situation == 3){if (map[a.pos1.Y - 1][a.pos1.X / 2] == 1 || map[a.pos1.Y - 1][a.pos1.X / 2 - 1] == 1|| map[a.pos1.Y - 1][a.pos1.X / 2 - 2] == 1){return false;}a.situation = 4;}else if (a.situation == 4){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos1.Y - 1][a.pos1.X / 2 + 1] == 1|| map[a.pos1.Y - 2][a.pos1.X / 2 + 1] == 1){return false;}a.situation = 1;}}else if (a.kind == 4){if (a.situation == 1){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1){return false;}a.situation = 2;}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1){return false;}a.situation = 3;}else if (a.situation == 3){if (map[a.pos1.Y - 1][a.pos1.X / 2] == 1){return false;}a.situation = 4;}else if (a.situation == 4){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1){return false;}a.situation = 1;}}else if (a.kind == 5){if (a.situation == 1){if (map[a.pos1.Y - 1][a.pos1.X / 2] == 1 || map[a.pos1.Y - 1][a.pos1.X / 2 + 1] == 1|| map[a.pos1.Y - 1][a.pos1.X / 2 + 2] == 1){return false;}a.situation = 2;}else if (a.situation == 2){if (map[a.pos1.Y][a.pos1.X / 2 + 1] == 1 || map[a.pos1.Y + 1][a.pos1.X / 2 + 1] == 1|| map[a.pos1.Y + 2][a.pos1.X / 2 + 1] == 1){return false;}a.situation = 3;}else if (a.situation == 3){if (map[a.pos1.Y + 1][a.pos1.X / 2] == 1 || map[a.pos1.Y + 1][a.pos1.X / 2 - 1] == 1|| map[a.pos1.Y + 1][a.pos1.X / 2 - 2] == 1){return false;}a.situation = 4;}else if (a.situation == 4){if (map[a.pos1.Y][a.pos1.X / 2 - 1] == 1 || map[a.pos1.Y - 1][a.pos1.X / 2 - 1] == 1|| map[a.pos1.Y - 2][a.pos1.X / 2 - 1] == 1){return false;}a.situation = 1;}}return true;}void move(HANDLE &table, fangkuai &a)//游戏引擎{if (_kbhit()){char control = _getch();if (control == ' ')//控制暂停{movemouse(table, 26, 10);system("pause");movemouse(table, 26, 10);cout << "                   ";}else if (control == 'w')//汽车人变形,略略略{if (judgement_changing(a)){clear(table, a);givefangkuaipos(a);draw(table, a);}}else if (control == 's')//加速下降{if (judgement_down(a)){clear(table, a);a.pos1.Y += 1;givefangkuaipos(a);draw(table, a);}else{a.ifstop = true;}}else if (control == 'a')//向左移动{if (judgement_left(a)){clear(table, a);a.pos1.X -= 2;givefangkuaipos(a);draw(table, a);}}else if (control == 'd')//向右移动{if (judgement_right(a)){clear(table, a);a.pos1.X += 2;givefangkuaipos(a);draw(table, a);}}}if (judgement_down(a))//自然下降{clear(table, a);a.pos1.Y += 1;givefangkuaipos(a);draw(table, a);}else{a.ifstop = true;}}void downworld(HANDLE &table, int ko)//当消除一行后,整体下降{int rect_x, rect_y;for (int i = ko; i >= 1; i--){rect_y = i;for (int j = 1; j <= 10; j++){map[i][j] = map[i - 1][j];rect_x = j * 2;movemouse(table, rect_x, rect_y);if (map[i][j] == 1){cout << "■";}else cout << " ";}}}void clearworld(HANDLE &table)//判断是否满足消除条件,是就消除{int ok = 0;for (int i = 19; i >= 0; i--){for (int j = 1; j <= 10; j++){if (map[i][j] == 1){ok++;}}if (ok == 10){int rect_x, rect_y = i;for (int k = 1; k <= 10; k++){map[i][k] = 0;rect_x = k * 2;movemouse(table, rect_x, rect_y);cout << ' ';grade++;}ok = 0;downworld(table, i);i++;}ok = 0;}}void intlfangkuai(fangkuai &a)//初始化方块,给出最初坐标以及形态种类{a.kind = rand() % 5 + 1;a.pos1.X = 6;a.pos1.Y = 1;if (a.kind == 1){a.situation = rand() % 2 + 1;}else if (a.kind == 3 || a.kind == 4 || a.kind == 5){a.situation = rand() % 4 + 1;}}void tellnext(HANDLE &table, fangkuai a)//天气预报,哈哈哈哈{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN);movemouse(table, 40, 12);cout << grade;movemouse(table, a.pos1.X + 28, a.pos1.Y + 3);cout << "■";movemouse(table, a.pos2.X + 28, a.pos2.Y + 3);cout << "■";movemouse(table, a.pos3.X + 28, a.pos3.Y + 3);cout << "■";movemouse(table, a.pos4.X + 28, a.pos4.Y + 3);cout << "■";}void cleartellnext(HANDLE &table, fangkuai a)//消除天气预报,哇哈哈哈哈哈{movemouse(table, 40, 12);cout << "     ";movemouse(table, a.pos1.X + 28, a.pos1.Y + 3);cout << " ";movemouse(table, a.pos2.X + 28, a.pos2.Y + 3);cout << " ";movemouse(table, a.pos3.X + 28, a.pos3.Y + 3);cout << " ";movemouse(table, a.pos4.X + 28, a.pos4.Y + 3);cout << " ";}void endofgame(HANDLE &table)//游戏结束界面{system("cls");cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;cout << "■              GAMEOVER                ■" << endl;cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;cout << "☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆" << endl;system("pause");}int main(){srand((int)time(NULL));HANDLE game = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄//来一个操作界面int rem = 0;//作为一个条件判断是否轮到下一个方块出场intlfangkuai(a);givefangkuaipos(a);intlwindow(game);readygame(game);while (1){move(game, a);Sleep(300);//缓冲一下,免得运行太快if (a.ifstop == false && rem == 0){intlfangkuai(b);//初始化另一个方块,实现天气预报功能givefangkuaipos(b);rem = 1;tellnext(game, b);}if (a.ifstop == true){if (a.pos1.Y == 2 || a.pos2.Y == 2 || a.pos3.Y == 2 || a.pos4.Y == 2){endofgame(game);break;//游戏结束界面 //判断你游戏输了没 }cleartellnext(game, b);rem = 0;clearworld(game);a = b;a.ifstop = false;}}return 0;}



原创粉丝点击