嘿嘿,
我又来啦!
最近小编思考了很久,决定用C++写一个走迷宫游戏,写完之后,觉得还行,就来给大家讲解一下
下载代码链接:c++走迷宫(原创作品)如要转载请标版权-其他文档类资源-CSDN下载
1.游戏界面
对于游戏界面这段代码,小编也不多说了,因为看过我博客的小伙伴(所以一定要关注啊!)
因该都知道,之前在一篇教你如何用C++写2D版我的世界(1)的博客中我就讲过怎们写一个游戏的标题界面,所以有兴趣的小伙伴可以去看看,这里我就不多说了φ(* ̄0 ̄)
2.游戏玩法
每个游戏都会有个新手教程或玩法介绍,下面我们来看看该怎么实现。
比如当我们写好标题界面的代码后,我们可以让用户按下b键来查看,拿着该怎么实现呢?我们来看一个函数:
getch()
比如这段代码:
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
int main()
{
char ch;
ch=getch();
if(ch=='a'){
cout<<1;
}
if(ch=='b'){
cout<<2;
}
return 0;
}
把上面代码运行,按a会发生什么,按b会发生什么?
没错这就是getch的功能,控制用户键盘指令。
这样实现打开玩法介绍的代码就是:
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int main()
{
system("title 走迷宫");//标题
system("mode con cols=60 lines=37");//设置窗口大小
string s="| 走迷宫 |";
string s1="| 开 始 游 戏 |";
string s2="| 玩 法 说 明 |";
string s3="| 关 卡 选 择 |";
cout<<"@==========================================================@"<<endl;
cout<<"| |"<<endl;
cout<<"| |"<<endl;
for(int i=0;i<s.size();i++){
cout<<s[i];
Sleep(10);//Sleep函数延长时间
}
cout<<endl;
cout<<"| |"<<endl;
for(int i=0;i<s1.size();i++){
cout<<s1[i];
Sleep(10);
}
cout<<endl;
cout<<"| |"<<endl;
for(int i=0;i<s2.size();i++){
cout<<s2[i];
Sleep(10);
}
cout<<endl;
cout<<"| |"<<endl;
cout<<"| |"<<endl;
cout<<"| |"<<endl;
cout<<"|(按‘k’开始游戏,按‘b’看玩法说明) |"<<endl;
cout<<"@==========================================================@"<<endl;
char ch;
ch=getch();//提取键盘指令
if(ch=='b'){
system("cls");
string c="使用wasd控制‘*’(玩家)移动,‘#’为边界,‘0’是终点";
string c1="玩家目标是通过移动到达终点,即可胜利。";
string c2="预告:1.1版本将会为迷宫增加难度,敬请期待。";
cout<<"版本:1.0"<<endl;
cout<<"按1直接显示所有内容,否则按2。"<<endl;
int q;
q=getch();
if(q=='1'){
cout<<c<<endl<<c1<<endl<<c2<<endl;
}
if(q=='2'){
for(int i=0;i<c.size();i++){
cout<<c[i];
Sleep(100);//延时
}
cout<<endl;
for(int i=0;i<c1.size();i++){
cout<<c1[i];
Sleep(100);
}
cout<<endl;
for(int i=0;i<c2.size();i++){
cout<<c2[i];
Sleep(100);
}
cout<<endl;
}
cout<<"按y开始游戏。";
ch=getch();
}
retrun 0;
}
玩法介绍的内容可以自行修改,如改个颜色,改变输出方法,调换按键……
3.游戏
重点重点重点!!!!
一个游戏的精华就是他的内容,所以:
(1)开始游戏
开始游戏就是当用户按下指定按键后,游戏便会开始,那么这怎么实现呢??
getch()!!!!!!
没错,直接上代码,
目前已写:
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int main()
{
system("title 走迷宫");//标题
system("mode con cols=60 lines=37");//设置窗口大小
string s="| 走迷宫 |";
string s1="| 开 始 游 戏 |";
string s2="| 玩 法 说 明 |";
string s3&#