首先这种游戏需要地图刷新
大家最先想到的一定是system("cls");了
但是这有一个很大的缺陷
只要走的太快就会闪屏
所以这时需要的就是重设控制台输出位置来保证不闪屏
函数如下:
void gotoxy(int y,int x){ COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); }//刷新屏幕
讯享网
但是,这么写光标依然会乱跳,怎么办呢,那就隐藏光标好了
讯享网 CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//光标隐藏
库函数 Windows.h
接下来做按键判断
需要_getch(); 以及 _kbhit();前者得到按下结果, 后者判断键盘是否按下
代码我就不放了
其他都很简单
但有一点,判断一样的数自动显示,这就需要我们万恶的电风扇DFS了
函数代码:
void dianpd(int i, int j) {//深搜点出相同的点 if(_isMap[i - 1][j - 1] == _isMap[youy][youx] && mapflag[i - 1][j - 1] == false) { mapflag[i - 1][j - 1] = true; dianpd(i - 1, j - 1); } if(_isMap[i - 1][j] == _isMap[youy][youx]&& mapflag[i - 1][j] == false){ mapflag[i - 1][j] = true; dianpd(i - 1, j); } if(_isMap[i - 1][j + 1] == _isMap[youy][youx] && mapflag[i - 1][j + 1] == false) { mapflag[i - 1][j + 1] = true; dianpd(i - 1, j + 1); } if(_isMap[i][j - 1] == _isMap[youy][youx] && mapflag[i][j - 1] == false) { mapflag[i][j - 1] = true; dianpd(i, j - 1); } if(_isMap[i][j + 1] == _isMap[youy][youx] && mapflag[i][j + 1] == false) { mapflag[i ][j + 1] = true; dianpd(i , j + 1); } if(_isMap[i + 1][j - 1] == _isMap[youy][youx] && mapflag[i + 1][j - 1] == false) { mapflag[i + 1][j - 1] = true; dianpd(i + 1, j - 1); } if(_isMap[i + 1][j] == _isMap[youy][youx] && mapflag[i + 1][j] == false) { mapflag[i + 1][j] = true; dianpd(i + 1, j); } if(_isMap[i + 1][j + 1] == _isMap[youy][youx] && mapflag[i + 1][j + 1] == false) { mapflag[i + 1][j + 1] = true; dianpd(i + 1, j + 1); } }
嗯接下来基本没有什么了
全代码如下:
讯享网//_kbhit() 按下键 //_getch() 读取键 #include<bits/stdc++.h> #include <conio.h> #include <windows.h> using namespace std; void gotoxy(int y,int x){ COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); }//刷新屏幕 int A; int youx = 1; int res; int youy = 1; int x; int win = 0; int y; //你的x,y坐标 bool mapflag[102][102] = {false}; char themap[102][102] = {'.'};//地图 char _isMap[102][102] = {0}; bool flag = 0; void dianpd(int i, int j) {//深搜点出相同的点 if(_isMap[i - 1][j - 1] == _isMap[youy][youx] && mapflag[i - 1][j - 1] == false) { mapflag[i - 1][j - 1] = true; dianpd(i - 1, j - 1); } if(_isMap[i - 1][j] == _isMap[youy][youx]&& mapflag[i - 1][j] == false){ mapflag[i - 1][j] = true; dianpd(i - 1, j); } if(_isMap[i - 1][j + 1] == _isMap[youy][youx] && mapflag[i - 1][j + 1] == false) { mapflag[i - 1][j + 1] = true; dianpd(i - 1, j + 1); } if(_isMap[i][j - 1] == _isMap[youy][youx] && mapflag[i][j - 1] == false) { mapflag[i][j - 1] = true; dianpd(i, j - 1); } if(_isMap[i][j + 1] == _isMap[youy][youx] && mapflag[i][j + 1] == false) { mapflag[i ][j + 1] = true; dianpd(i , j + 1); } if(_isMap[i + 1][j - 1] == _isMap[youy][youx] && mapflag[i + 1][j - 1] == false) { mapflag[i + 1][j - 1] = true; dianpd(i + 1, j - 1); } if(_isMap[i + 1][j] == _isMap[youy][youx] && mapflag[i + 1][j] == false) { mapflag[i + 1][j] = true; dianpd(i + 1, j); } if(_isMap[i + 1][j + 1] == _isMap[youy][youx] && mapflag[i + 1][j + 1] == false) { mapflag[i + 1][j + 1] = true; dianpd(i + 1, j + 1); } } void keypd(char key) {//判断按下的键 if((key == 'W' || key == 'w') && youy != 1) { youy --; } else if((key == 'S' || key == 's') && youy != x ) { youy ++; } else if((key == 'A' || key == 'a') && youx != 1 ) { youx --; } else if((key == 'D' || key == 'd') && youx != y ) { youx ++; } else if((key == 'Z' || key == 'z') && _isMap[youy][youx] == '*') { cout << "你死了"; win = 1; return; } else if((key == 'Z' || key == 'z') && _isMap[youy][youx] != '*') { dianpd(youy, youx);//深搜寻找相同 mapflag[youy][youx] = true; }//判断雷的数量 else if(key == 'Q' ) { if(!flag) flag = 1; else flag = 0; } } int main() {//运行 CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//光标隐藏 K: win = 0; youy = 1; youx = 1; system("cls"); cout << "请输入长, 宽, 地雷数量:"; cin >> x >> y >> A; system("cls"); for(int i = 1; i <= x; i ++) { for(int j = 1; j <= y; j ++) { mapflag[i][j] = 0; _isMap[i][j] = 0; themap[i][j] = '.'; } } srand(time(0)); for(int i = 1; i <= A; i++){ int q, w; a:q = rand() % x + 1; w = rand() % y + 1; if((q <= 1 && w <= 1) || q > x || w > y || _isMap[q][w] == '*') { goto a; }//随机生成雷 else { _isMap[q][w] = '*'; } } for(int i = 1; i <= x; i ++) { for(int j = 1; j <= y; j ++) { if(_isMap[i][j] != '*') { _isMap[i][j] = '0'; if(_isMap[i - 1][j - 1] == '*') _isMap[i][j]++; if(_isMap[i - 1][j] == '*') _isMap[i][j]++; if(_isMap[i - 1][j + 1] == '*') _isMap[i][j]++; if(_isMap[i][j - 1] == '*') _isMap[i][j]++; if(_isMap[i][j + 1] == '*') _isMap[i][j]++; if(_isMap[i + 1][j - 1] == '*') _isMap[i][j]++; if(_isMap[i + 1][j] == '*') _isMap[i][j]++; if(_isMap[i + 1][j + 1] == '*') _isMap[i][j]++; } if(i == youy && j == youx) themap[i][j] = '&'; else themap[i][j] = '.'; cout << themap[i][j] << " "; } cout << endl; } cout << "boom:" << A << endl << endl << endl; while(1) { if(_kbhit()) { while(_kbhit()) { keypd(_getch()); } if(win) { Sleep(3000); goto K; } gotoxy(0,0); //system("cls"); res = 0; for(int i = 1; i <= x; i ++) { for(int j = 1; j <= y; j ++) { if(i == youy && j == youx) themap[i][j] = '&'; else if(mapflag[i][j] || flag == 1) themap[i][j] = _isMap[i][j]; else themap[i][j] = '.'; cout << themap[i][j] << " "; if(themap[i][j] == '.') res ++; } cout << endl; } cout << "boom:" << A << endl; if(((res == A && mapflag[youy][youx] != 0))) { cout << "你赢了"; Sleep(3000); goto K; } // Sleep(500); } } return 0; }
本人欢迎交友!
代码是几年前刚学的时候写的所以写的很丑请见谅)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/45882.html