Hello everyone!想必大家有时肯定会突然冒出想自己写游戏的想法,可却又不知道怎么写,从今天开始小编就教大家如何用c++编写一个2D版我的世界(如有错误,请大佬多多指出,后续我会修改,大佬勿喷ヾ(≧▽≦*)o)
1. 标题界面
一个游戏都有一个合格的标题界面,但先别急,我们先来学两个函数——Sleep和system函数
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main()
{
cout<<1;
Sleep(1000);
system("cls");
cout<<2;
return 0;
}
把以上代码运行,会发生什么?
没错,当输出1时,代码停顿了1秒,然后清屏输出2.想必你已经猜到了,Sleep函数是使代码停顿,1000为1秒,而system函数是可以清屏的,但注意要用windows.h头文件(万能不行)
这样,我们就可以实现一个游戏的界面了:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int main()
{
//初始菜单
string s="| MINCRAFT 2D |";
string s1="| by Tony |";
string s2="| Pleas type 'p' start the game... |";
string s3="| Type 'o' exit the game.|";
cout<<"@----------------------------------------------------------@"<<endl;
cout<<"| |"<<endl;
cout<<"|