# c++——写点游戏
## 肯定有些同学想用c++写点游戏,今天,cht就来说说这个(必须有Windows处理器或电脑,Linux不支持)。
# 一、准备
## 1、必备算法
### (1)高精度
### (2)DFS(或BFS)
### (3)递推与递归
### (4)字符串运算
### (5)位运算
### (6)部分STL(sort,swap……)
## 2、头文件
### (1)```#include//万能头文件```
### (2)```#include//getch()```
### (3)```#include//system()```
### (4)```#include```
# 二、模板与语法
## 1、getch();
### 用法:可以在不打换行的前提下输入一个字符(要用conio.h)。
### 模板
```
char op;
op = getch();
while(op == ' '/*条件*/)
{
op = getch();
}
```
### 事例(按空格键继续):
```
#include
#include
using namespace std;
int main()
{
cout << "空格继续" << endl;
char op;
op = getch();
while(op != ' ')
{
op = getch();
}
cout << "再见" << endl;
return 0;
}
```
### 效果:
![批注 2020-04-20 192005.png](https://cdn.acwing.com/media/article/image/2020/04/20/8041_f8a6ac1082-批注-2020-04-20-192005.png)
![批注 2020-04-20 192022.png](https://cdn.acwing.com/media/article/image/2020/04/20/8041_fd5a31be82-批注-2020-04-20-192022.png)
## 2、system()
### 这个函数对应的头文件是:```#include```
### 具体语法:
#### (1)、```system("cls")//清空屏幕```
#### (2)、
```
system("color *F);//这里的*可以填0-7。
```
#### *的具体填法(写个```system("color 10F")```就能出来):
![批注 2020-04-20 192438.png](https://cdn.acwing.com/media/article/image/2020/04/20/8041_8c5c1c4c82-批注-2020-04-20-192438.png)
#### (3)、```system("mode con cols=100 lines=40");//初始化缓冲区大小```
## 3、读入读出句柄与字体设置
### 读入读出句柄是这行:
```
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出句柄(写在using namespace std后面)
CloseHandle(hOut);//关闭标准输出句柄(写在return 0前面)
```
### 就是这么设置(后面的均用这个句柄)。
### 那它能干什么呢?
#### (1)、字体颜色:
#### 蓝色:
```
void blue_border()
{
WORD blue=FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN;//设置字体颜色、背景颜色
SetConsoleTextAttribute(hOut,blue);//字体样式
}
```
#### 白色:
```
void white_back()
{
WORD white=FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY;
SetConsoleTextAttribute(hOut,white);
}
```
#### 青色:
```
void cyan_choose()
{
WORD cyan=FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,cyan);
}
```
#### 不太好的红色和橙色(其实都是可以自己做的,有兴趣的小伙伴不妨试试):
```
void red_choose()
{
WORD red=FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,red);
}
void orange_choose()
{
WORD orange=FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_BLUE|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,orange);
}
```
#### 自己做的例子:
```
void on_Select()
{
WORD select=FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_RED;
SetConsoleTextAttribute(hOut,select);
}
```
### (2)、输出的位置(光标):
### 游戏里最常用的函数。
### pos(x,y);
### 代码:
```
void pos(int x,int y)
{
COORD posPoint = {x,y}; //设置坐标
SetConsoleCursorPosition(hOut,posPoint);
}
```
## (4)、blahblahblah
### 要写标题请加这行(引号里写标题):
```
SetConsoleTitle("");
```
# 三、游戏事例
## 1、小程序:一元一次方程神器:
```
#include //C++万能头
#include//控制台编程主要头文件Sleep();
#include//getch()函数
#include//system()函数
using namespace std;
void pos(int x,int y);//确定光标位置
void blue_border();//蓝色字体
void white_back();//还原亮白色字体
void cyan_choose();//青色字体
void on_Select();//被选中时的样式
void onChoose(int x,int y);//确定所选中的位置
void star();//初始化界面
struct node
{
double a, b;
};
int priority(char c)
{
if (c == '*' || c == '/')
return 2;
if (c == '+' || c == '-')
return 1;
return 0;
}
void calc(stack &op, stack &num)
{
node bb = num.top();
num.pop();
node aa = num.top();
num.pop();
node temp_node;
switch (op.top())
{
case '+':
temp_node.a = aa.a + bb.a;
temp_node.b = aa.b + bb.b;
num.push(temp_node);
break;
case '-':
temp_node.a = aa.a - bb.a;
temp_node.b = aa.b - bb.b;
num.push(temp_node);
break;
case '*':
temp_node.a = aa.a * bb.b + aa.b * bb.a;
temp_node.b = aa.b * bb.b;
num.push(temp_node);
break;
case '/':
temp_node.a = aa.a / bb.b;
temp_node.b = aa.b / bb.b;
num.push(temp_node);
break;
}
op.pop();
}
void d()
{
cyan_choose();
pos(31,14);
cout << "欢迎来到一元一次方程神器!";
Sleep(1000);//等待
system("cls");
system("color 2F");
blue_border();
int x=25,y=10;
char sel;
sel=getch();
while(sel!=' ')
{
pos(x,y);
onChoose(x,y);
sel=getch();
}
system("cls");
system("color 3F");
cyan_choose();
pos(25,23);
cout << "请输入方程:" ;
while (1)
{
string str, str_l, str_r;
cin >>str;
for (int i = 0; i < str.size(); ++ i)
{
if (str[i] == '=')
{
str_l = str.substr(0, i);
str_r = str.substr(i + 1, str.size());
}
}
stack num_l;
stack num_r;
stack op_l;
stack op_r;
int len_l = str_l.size();
int len_r = str_r.size();
for (int i = 0; i < len_l; ++ i)
{
if (isdigit(str_l[i]))
{
node temp_node;
double temp = atof(&str_l[i]);
while (isdigit(str_l[i]) || str_l[i] == '.')
++ i;
if (str_l[i] == 'x')
{
temp_node.a = temp;
temp_node.b = 0;
num_l.push(temp_node);
}
else
{
temp_node.a = 0;
temp_node.b = temp;
num_l.push(temp_node);
-- i;
}
}
else if (str_l[i] == 'x')
{
node temp_node;
temp_node.a = 1;
temp_node.b = 0;
num_l.push(temp_node);
}
else if (str_l[i] == '(')
{
op_l.push(str_l[i]);
}
else if (str_l[i] == ')')
{
while (op_l.top() != '(')
calc(op_l, num_l);
op_l.pop();
}
else if (op_l.empty())
{
op_l.push(str_l[i]);
}
else if (priority(op_l.top()) < priority(str_l[i]))
{
op_l.push(str_l[i]);
}
else if (priority(op_l.top()) >= priority(str_l[i]))
{
while (!op_l.empty() && priority(op_l.top()) >= priority(str_l[i]))
calc(op_l, num_l);
op_l.push(str_l[i]);
}
}
for (int i = 0; i < len_r; ++ i)
{
if (isdigit(str_r[i]))
{
node temp_node;
double temp = atof(&str_r[i]);
while (isdigit(str_r[i]) || str_r[i] == '.')
++ i;
if (str_r[i] == 'x')
{
temp_node.a = temp;
temp_node.b = 0;
num_r.push(temp_node);
}
else
{
temp_node.a = 0;
temp_node.b = temp;
num_r.push(temp_node);
-- i;
}
}
else if (str_r[i] == 'x')
{
node temp_node;
temp_node.a = 1;
temp_node.b = 0;
num_r.push(temp_node);
}
else if (str_r[i] == '(')
{
op_r.push(str_r[i]);
}
else if (str_r[i] == ')')
{
while (op_r.top() != '(')
calc(op_r, num_r);
op_r.pop();
}
else if (op_r.empty())
{
op_r.push(str_r[i]);
}
else if (priority(op_r.top()) < priority(str_r[i]))
{
op_r.push(str_r[i]);
}
else if (priority(op_r.top()) >= priority(str_r[i]))
{
while (!op_r.empty() && priority(op_r.top()) >= priority(str_r[i]))
calc(op_r, num_r);
op_r.push(str_r[i]);
}
}
while (!op_l.empty())
calc(op_l, num_l);
while (!op_r.empty())
calc(op_r, num_r);
double x1 = num_l.top().a, y1 = num_l.top().b;
double x2 = num_r.top().a, y2 = num_r.top().b;
system("cls");
system("color 3F");
pos(20,20);
printf("%.3lf\n", (y2 - y1) / (x1 - x2));
Sleep(2000);
pos(20,21);
cout << "要继续吗?" << endl;
char op;
op = getch();
while(op != ' ')
{
op = getch();
}
system("cls");
system("color 3F");
pos(20,20);
cout << "谢谢使用,再见!";
Sleep(2000);
return;
}
}
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出句柄
int main()
{
system("color 7F");//设置控制台界面背景颜色和前景颜色
system("mode con cols=100 lines=40");//初始化缓冲区大小
SetConsoleTitle("一元一次方程神器");//设置控制台窗口标题
cyan_choose();
cout<<"w,a,s,d 控制光标选择;空格 确定"<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//上边框
pos(5,25);
cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//下边框
for(int i=5,j=6; j<25; j++) //左边框
{
pos(i,j);
cout<<"*";
}
for(int i=85,j=6; j<25; j++) //右边框
{
pos(i,j);
cout<<"*";
}
cyan_choose();
pos(65,10);//指定位置输出
cout<<"2. 选择 ";
pos(25,20);
cout<<"3. 继续 ";
pos(65,20);
cout<<"4. 退出 ";
on_Select();
pos(25,10);
cout<<"1. 开始 ";
//wsad控制光标对进行自由选择
int x=25,y=10;
char sel;
sel=getch();//和cin相比,不需要按enter键
while(sel!=' ')
{
star();
switch(sel)
{
case 'w':
y=y-10;
break;
case 's':
y=y+10;
break;
case 'a':
x=x-40;
break;
case 'd':
x=x+40;
break;
}
//防止超出范围
if(x>=65)
{
x=65;
}
if(y>=20)
{
y=20;
}
if(x<=25)
{
x=25;
}
if(y<=10)
{
y=10;
}
pos(x,y);
onChoose(x,y);
sel=getch();//刷新sel
}
pos(0,30);
system("cls");
system("color 5F");
blue_border();
pos(5,5);
pos(5,25);
cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//下边框
for(int i=5,j=6; j<25; j++) //左边框
{
pos(i,j);
cout<<"*";
}
for(int i=85,j=6; j<25; j++) //右边框
{
pos(i,j);
cout<<"*";
}
d();
cyan_choose();
CloseHandle(hOut);//关闭标准输出句柄
return 0;
}
//设置光标位置
void pos(int x,int y)
{
COORD posPoint = {x,y}; //设置坐标
SetConsoleCursorPosition(hOut,posPoint);
}
void blue_border()
{
WORD blue=FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN;//设置字体颜色、背景颜色
SetConsoleTextAttribute(hOut,blue);//字体样式
}
void white_back()
{
WORD white=FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY;
SetConsoleTextAttribute(hOut,white);
}
void cyan_choose()
{
WORD cyan=FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,cyan);
}
void on_Select()
{
WORD select=FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_RED;
SetConsoleTextAttribute(hOut,select);
}
void onChoose(int x,int y)
{
if(x==25&&y==10)
{
on_Select();
cout<<"1. 开始 ";
}
else if(x==25&&y==20)
{
on_Select();
cout<<"3. 继续 ";
}
else if(x==65&&y==10)
{
on_Select();
cout<<"2. 选择 ";
}
else if(x==65&&y==20)
{
on_Select();
cout<<"4. 退出 ";
}
}
void star()
{
cyan_choose();
pos(25,10);
cout<<"1. 开始 ";
pos(65,10);
cout<<"2. 选择 ";
pos(25,20);
cout<<"3. 继续 ";
pos(65,20);
cout<<"4. 退出 ";
}
```
## 2、画图小程序(bug多多的):
```
#include
#include
#include
#include
using namespace std;
int x[3][2];
int y[4][2];
int x2[3][2];
int y2[4][2];
int x3[3][2];
int y3[4][2];
int x4[4][2];
int y4[4][2];
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
void pos(int x,int y)
{
COORD posPoint = {x,y}; //设置坐标
SetConsoleCursorPosition(hOut,posPoint);
}
int main()
{
system("color 3F");
system("color AF");
system("mode con cols=100 lines=40");
SetConsoleTitle("画图神器");
cout << "欢迎来到画图神器" << endl;
Sleep(1000);
cout << "1键退出" << endl;
Sleep(1000);
cout << "a,b,c,d,z键画图" << endl;
Sleep(1000);
x[0][0] = 0;//****
x[0][1] = 3;
x[1][0] = 0;//****
x[1][1] = 3;
x[2][0] = 1;// **
x[2][1] = 2;
y[0][0] = 0;//**
y[0][1] = 1;
y[1][0] = 0;//***
y[1][1] = 2;
y[2][0] = 0;//***
y[2][1] = 2;
y[3][0] = 0;//**
y[3][1] = 1;
x2[0][0] = 1;
x2[0][1] = 2;
x2[1][0] = 0;
x2[1][1] = 3;
x2[2][0] = 0;
x2[2][1] = 3;
y2[0][0] = 1;
y2[0][1] = 2;
y2[1][0] = 0;
y2[1][1] = 2;
y2[2][0] = 0;
y2[2][1] = 2;
y2[3][0] = 1;
y2[3][1] = 2;
x3[0][0] = 0;
x3[0][1] = 1;
x3[1][0] = 0;
x3[1][1] = 3;
x3[2][0] = 0;
x3[2][1] = 3;
y3[0][0] = 0;
y3[0][1] = 2;
y3[1][0] = 0;
y3[1][1] = 2;
y3[2][0] = 1;
y3[2][1] = 2;
y3[3][0] = 1;
y3[3][1] = 2;
x4[0][0] = 0;
x4[0][1] = 1;
x4[1][0] = 0;
x4[1][1] = 3;
x4[2][0] = 0;
x4[2][1] = 3;
x4[3][0] = 2;
x4[3][1] = 3;
y4[0][0] = 0;
y4[0][1] = 2;
y4[1][0] = 1;
y4[1][1] = 2;
y4[2][0] = 1;
y4[2][1] = 2;
y4[3][0] = 1;
y4[3][1] = 3;
char a;
for(int i = 0;;i ++)
{
cout << endl <<"请按任意键继续" << endl;
char op;
op = getch();
while(op == ' ')
{
op = getch();
}
system("cls");
system("color 3F");
system("color AF");
a = getch();
while(a == ' ')
{
a = getch();
}
if(a == 'a')
{
for(int i = 0;i <= 2; i ++)
{
for(int j = x[i][0]; j <= x[i][1]; j ++)
{
pos(j,i);
cout << "*";
}
cout << endl;
}
for(int i = 0;i <= 3; i ++)
{
for(int j = y[i][0]; j <= y[i][1]; j ++)
{
pos(i,j);
cout << "*";
}
cout << endl;
}
}
else if(a == 'b')
{
for(int i = 0;i <= 2; i ++)
{
for(int j = x2[i][0]; j <= x2[i][1]; j ++)
{
pos(j,i);
cout << "*";
}
cout << endl;
}
for(int i = 0;i <= 3; i ++)
{
for(int j = y2[i][0]; j <= y2[i][1]; j ++)
{
pos(i,j);
cout << "*";
}
cout << endl;
}
}
else if(a == 'c')
{
for(int i = 0;i <= 2; i ++)
{
for(int j = x3[i][0]; j <= x3[i][1]; j ++)
{
pos(j,i);
cout << "*";
}
cout << endl;
}
for(int i = 0;i <= 3; i ++)
{
for(int j = y3[i][0]; j <= y3[i][1]; j ++)
{
pos(i,j);
cout << "*";
}
cout << endl;
}
}
else if(a == '1')
{
break;
}
else if(a == 'd')
{
for(int i = 0;i <= 3; i ++)
{
for(int j = x4[i][0]; j <= x4[i][1]; j ++)
{
pos(j,i);
cout << "*";
}
cout << endl;
}
for(int i = 0;i <= 3; i ++)
{
for(int j = y4[i][0]; j <= y4[i][1]; j ++)
{
pos(i,j);
cout << "*";
}
cout << endl;
}
}
else if(a == 'z')
{
char op;
cout << "好的,按wasd控制线条" << endl;
int i = 0,j = 0;
system("cls");
system("color 3F");
system("color AF");
pos(i,j);
cout << "*";
op = getch();
for(;;)
{
op = getch();
if(op == 'a')
{
if(i != 0)pos(i - 1,j);
cout << "*";
i -= 1;
}
else if(op == 's'){
if(j != 0)pos(i, j + 1);
cout << "*";
j -= 1;
}
else if(op == 'd')
{
pos(i,j - 1);
cout << "*";
i += 1;
}
else if(op == 'w'){
pos(i + 1,j);
cout << "*";
j += 1;
}
else if(op == 'e')
{
break;
}
}
}
Sleep(1000);
}
Sleep(1000);
cout << "谢谢使用,再见!" << endl;
Sleep(4000);
CloseHandle(hOut);
return 0;
}
```
## 3、跑酷天堂(网上改的):
```
#include
#include
#include
using namespace std;
char mmp[1001][1001] = {" ",
" ",
" ### ",
" ",
" ## ##### ",
" ## ",
" ",
" ",
" ## ",
" ",
" ",
" ## ### ",
" ## #### ",
" ### #### ",
" ### ## ",
" ",
" O ",
"##### ",
"##### ",
"####################################################################################################"
};//20 * 50;
char putmmp[20][16];
int jump[20] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1};
int main() {
char ch;
bool t = 1;
int x = 16, y = 2, delayjump = 0;
system("color 3F");
system("mode con cols=100 lines=40");
SetConsoleTitle("跑酷天堂 l1");
while (1) {
Sleep(30);
if (y - 7 < 0) {
for (register int i = 0; i < 20; i++) {
for (register int j = 0; j < 15; j++) {
putmmp[i][j] = mmp[i][j];
}
putmmp[i][15] = '\n';
}
} else if (y + 7 > 99) {
for (register int i = 0; i < 20; i++) {
int k = 14;
for (register int j = 99; j >= 85; j--, k--) {
putmmp[i][k] = mmp[i][j];
}
putmmp[i][15] = '\n';
}
} else {
for (register int i = 0; i < 20; i++) {
int k = 0;
for (register int j = y - 7; j <= y + 7; j++, k++) {
putmmp[i][k] = mmp[i][j];
}
putmmp[i][15] = '\n';
}
}
system("cls");
system("color 3F");
fwrite(putmmp, 1, 20 * 16, stdout);
if (delayjump > 0) {
if (delayjump > 8 && (x + jump[delayjump] == 20 || mmp[x + jump[delayjump]][y] == '#')) {
delayjump = 5;
} else {
if (delayjump <= 8 && (x + jump[delayjump] == -1 || mmp[x + jump[delayjump]][y] == '#')) {
delayjump = 0;
} else {
mmp[x][y] = ' ';
x += jump[delayjump];
mmp[x][y] = 'O';
delayjump--;
}
}
}
if (!delayjump) {
if (mmp[x + 1][y] == ' ' && x < 19 && t) {
t = !t;
mmp[x][y] = ' ';
x++;
mmp[x][y] = 'O';
} else {
t = !t;
}
}
if (kbhit()) {
ch = getch();
switch(ch) {
case 27 :
exit(0);
break;
case -32 :
ch = getch();
switch(ch) {
case 75 :
if (y > 0 && mmp[x][y - 1] == ' ') {
mmp[x][y] = ' ';
y--;
mmp[x][y] = 'O';
}
break;
case 77 :
if (y < 99 && mmp[x][y + 1] == ' ') {
mmp[x][y] = ' ';
y++;
mmp[x][y] = 'O';
}
break;
case 72 :
if (!delayjump) {
delayjump = 18;
}
break;
}
break;
}
}
}
return 0;
}
```
## 4、飞翔的小鸟(网上转载的):
```
#include
#include
#include
#include
#include
#include
using namespace std;
int h,w,c=0;
int score=0;//得分
int xn_x,xn_y,b1,b2,b3;
char asd;
void data(){
h=15;
w=25;
xn_x=0;
xn_y=w/3;
b3=w;
b1=h/4;
b2=h/2;
}
void page(){
int i,j;
char dad ;
if(c==0){
system("color 0C");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" =======================================\n");
printf(" = --->飞翔的小鸟<--- =\n");
printf(" = =\n");
printf(" = 空格控制小鸟移动 =\n");
printf(" = 5键暂停继续 =\n");
printf(" = =\n");
printf(" = =\n");
printf(" = 闪屏纯属正常 =\n");
printf(" = ----朱 * * =\n");
printf(" =======================================\n");
c++;
dad=getch();
}
system("cls");
system("color 02");
for(i=0;i=b2)){
if(i==xn_x && j>xn_y){
printf("▓");
}
else printf(" ▓");
}
else printf(" ");
}
printf("\n");
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),9);
printf("================================================================================\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
printf(" ------------------> 当前得分:%d <------------------\n\n\n",score);
}
void no(){
int randx;
if(xn_y==b3){
if(xn_x>b1&&xn_x the end <------\n");
c=3;
asd=getch();
}
}
xn_x++;
if(b3==0){
b3=w;
b1=rand()%(h-5);
randx=b1;
b2=randx+h/4+2;
}
else
b3--;
Sleep(120);
}
void yes(){
char input;
if(kbhit()){
input=getch();
if(input==' ') xn_x-=3;
if(input=='5') while(getch()!='5'){};//暂停继续
}
}
int main(){
system("title 飞翔的小鸟 2.0版 ---朱 * *");
data();//加载数据
while(1){
page();//页面
no();//与用户无关变量
yes();//与用户有关变量
if(c==3)break;
}
return 0;
}
```
## 5、迷宫(网上的):
```
#include
#include
#include
#include
/*
*迷宫的高度
*/
#define Height 23
/*
*迷宫的宽度
*/
#define Width 39
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
#define Wall 1
#define Road 0
#define Start 2
#define End 3
int map[Height+2][Width+2];
/*
*随机生成迷宫
*/
void create(int x,int y)
{
int c[4][2]={0,1,1,0,0,-1,-1,0}; /*四个方向*/
int i,j,t;
/*
*将方向打乱
*/
for(i=0;i<4;i++)
{
j=rand()%4;
t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;
t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;
}
map[x][y]=Road;
for(i=0;i<4;i++)
if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)
{
map[x+c[i][0]][y+c[i][1]]=Road;
create(x+2*c[i][0],y+2*c[i][1]);
}
}
/*
*移动坐标
*/
void gotoxy(int x,int y)
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
/*
*隐藏光标
*/
void hidden()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;/*赋1为显示,赋0为隐藏*/
SetConsoleCursorInfo(hOut,&cci);
}
void paint(int x,int y)
{
gotoxy(2*y-2,x-1);
switch(map[x][y])
{
case Start:
printf("进");break; /*画入口*/
case End:
printf("出");break; /*画出口*/
case Wall:
printf("■");break; /*画墙*/
case Road:
printf(" ");break; /*画路*/
}
}
/*
*接收按键
*/
int get_key()
{
char c;
while(c=getch())
{
if(c==27) return Esc; /*Esc*/
// if(c!=-32)continue;
// c=getch();
if(c==72) return Up; /*上*/
if(c==80) return Down; /*下*/
if(c==75) return Left; /*左*/
if(c==77) return Right; /*右*/
}
return 0;
}
/*
*画迷宫
*/
void game()
{
int x=2,y=1; /*玩家当前位置,刚开始在入口处*/
int c; /*用来接收按键*/
while(1)
{
gotoxy(2*y-2,x-1);
printf("★"); /*画出玩家当前位置*/
if(map[x][y]==End) /*判断是否到达出口*/
{
gotoxy(30,24);
printf("到达终点,按任意键结束");
getch();
break;
}
c=get_key();
if(c==Esc)
{
gotoxy(0,24);
break;
}
switch(c)
{
case Up: /*向上走*/
if(map[x-1][y]!=Wall)
{
paint(x,y);
x--;
}
break;
case Down: /*向下走*/
if(map[x+1][y]!=Wall)
{
paint(x,y);
x++;
}
break;
case Left: /*向左走*/
if(map[x][y-1]!=Wall)
{
paint(x,y);
y--;
}
break;
case Right: /*向右走*/
if(map[x][y+1]!=Wall)
{
paint(x,y);
y++;
}
break;
}
}
}
int main()
{
int i,j;
system("color 2b");
srand((unsigned)time(NULL)); /*初始化随即种子*/
hidden(); /*隐藏光标*/
for(i=0;i<=Height+1;i++)
for(j=0;j<=Width+1;j++)
if(i==0||i==Height+1||j==0||j==Width+1) /*初始化迷宫*/
map[i][j]=Road;
else map[i][j]=Wall;
create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); /*从随机一个点开始生成迷宫*/
for(i=0;i<=Height+1;i++) /*边界处理*/
{
map[i][0]=Wall;
map[i][Width+1]=Wall;
}
for(j=0;j<=Width+1;j++) /*边界处理*/
{
map[0][j]=Wall;
map[Height+1][j]=Wall;
}
map[2][1]=Start; /*给定入口*/
map[Height-1][Width]=End; /*给定出口*/
for(i=1;i<=Height;i++)
for(j=1;j<=Width;j++) /*画出迷宫*/
paint(i,j);
game(); /*开始游戏*/
return 0;
}
```
## 如果大家会工程还可以看看这几个:
### 1、贪吃蛇(网上的):
### main.cpp:
```
#include
#include
#include
#incldue
#include
#include
#include
#include
#include
%%%
炸了
markdown怎么炸了?
草我不到啊!
TQL orz!!!!
orz
话说这篇文章的后半部分为啥感觉在哪里见过?
orz!(我看见过国外神仙用scratch写的二维Minecraft)
谢谢~
ORZ
orz
#66666
谢谢
# 666
谢谢
卧槽牛逼
hh
都是网上学的,只是转载到acwing上