今天突发奇想,想做一个小游戏,标题是这样的
cout<<"一个简单的小游戏";
很质朴,也很单调。
所以我决定给它升级一下,比如
string str[] {"一个简单的小游戏"};
for(int i=0;i<8;i++)
{
cout<<str[i];
}
但字一个一个蹦出来太生硬,所以我决定给它升级一下,比如
string str = "这是一个简单的小游戏";
for(int i=0; i<10; i++)
{
for(int j=0; j<9-i; j++)
{
cout<<" ";
}
for(int j=0; j<i+1; j++)
{
cout<<str[9-i+j];
}
system("cls");
}
好多了,但还是有很多问题,所以我决定再给它升级一下。
终版
支持多排打印,可以选择渐出方向,可以选择三种排版模式,优化清屏函数
#define DIR_LEFT -1
#define DIR_RIGHT 1
#define ALIGN_LEFT -1
#define ALIGN_CENTER 0
#define ALIGN_RIGHT 1
void ReversePrint(string str_a[],const int len,const int direction,const int align)
{
//比较字符串长度
int word = -1;
int temp[len] {};
for(int i=0; i<len; i++)
{
temp[i] = str_a[i].length();
if(temp[i]>word)
{
word = temp[i];
}
}
//排版
for(int i=0; i<len; i++)
{
for(int j=0; j<word-temp[i]; j++)
{
if(align==-1||align==0&&(j<(word-temp[i])/2))
{
str_a[i] = str_a[i]+" ";
}
else if(align==1||align==0&&(j>=(word-temp[i])/2))
{
str_a[i] = " "+str_a[i];
}
else
{
cout<<"\n'"<<align<<"' was not declared in this scope"<<endl;
return;
}
}
}
//打印
const int Cword = word;
for(int i=0; i<word; i++)
{
for(int k=0; k<len; k++)//一回合操作一行
{
string str = str_a[k];
if(direction==1)
{
for(int j=0; j<word-i-1; j++)
{
cout<<" ";
}
}
else
{
word = i+1;
}
for(int j=0; j<i+1; j++)
{
cout<<str[word-i-1+j];
}
cout<<'\n';
}
word = Cword;
if(i!=word-1)//清屏
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { 0, 0 };
SetConsoleCursorPosition( hConsole, coordScreen );
}
}
}
为了美观,最好还是隐藏一下光标
void HideCursor()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
好家伙一个标题占了70行代码······