#今天给大家看看如何十二步自创一个扫雷小游戏
1.载入需要的库文件( windows.h等不在bits/stdc++.h内部的库文件需要自行载入)代码如下:
#include <iostream>//本文代码原创-吝旭涵
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <bits/stdc++.h>
using namespace std;
int sy=9;
int board[15][15];
int show[15][15];
2.创建color方法,来给地雷,旗子等特殊标记进行上色。
#为了简洁,我们直接将需要的颜色全部写出来,后面直接调用color(n)即可。
void Color(int colorpaint){
if(colorpaint==0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(colorpaint==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(colorpaint==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
}
3.编写输出打开游戏界面
void intro()
{
cout << "╔═══════════════════════════════════════════════════════════════════╗" << endl;
cout << "║ ║" << endl;
cout << "║ ***欢迎运行扫雷游戏!*** ║" << endl;
cout << "║ ║" << endl;
cout << "║ 【游戏规则如下:】 ║" << endl;
cout << "║ ║" << endl;
cout << "║ ║" << endl;
cout << "║ ║" << endl;
cout << "║ 在这个游戏里会有一个 9*9 的网格,网格中埋着 10颗雷 ║" << endl;
cout << "║ ║" << endl;
cout << "║ 游戏目标是找出所有的雷,如果触雷,则游戏失败 ║" << endl;
cout << "║ ║" << endl;
cout << "║ 每次输入要操作的格子 行、列,1表示翻开,2表示标记/取消旗子?? ║" << endl;
cout << "║ ║" << endl;
cout << "║ 格子中的数字表示它周围八个格中有几个雷 ║" << endl;
cout << "║ ║" << endl;
cout << "║ *第一次翻格子不会是雷 ║" << endl;
cout << "║ ║" << endl;
cout << "║ ║" << endl;
cout << "║ ║" << endl;
cout << "║ 按下 回车键 开始游戏 ║" << endl;
cout << "╚═══════════════════════════════════════════════════════════════════╝" << endl;
getchar();
}
4.创建方法,打印棋盘。
void drawBoard()
{
cout << "\033c" << flush;
// 输出行号
cout<<"剩余地雷数量:"<<sy<<"个"<<endl;
cout << " ";
for (int i = 1; i <= 9; i++) cout << " " << i;
cout << " \n";
// 输出上边框
cout <<