我正在开发一个项目,用c ++制作球/对象命中游戏。
我使用system("cls");
清除屏幕并显示Game Over
消息。用户分数正在重置为零0
。删除system("cls");
并不会影响用户的得分。有没有什么好方法可以在不清除vairables值的情况下清除屏幕?
这是一个示例代码。这是我游戏2级的功能。 我已经评论了导致我这个问题的那条线。
int level_2(string user_name,int scores)
{
system("CLS");
for(int i=5;i>0;i--)
{
cout<< "\n\n\n\n\n_________________________________\n";
cout<< "| |\n";
cout<< "| level 2 starts in "<<i<<" seconds\t|\n";
cout<< "| |\n";
cout<< "_________________________________\n";
Sleep(1000);
system("CLS");
}
system("CLS");
string s = "=======";
string h = " ";
int i=5, x=10, y=29;
cout << "Press Arrow Keys to move, press q to quit\tWelcome "<<user_name<<"!\n";
cout<< "_________________________________\n";
for(int i=0;i<31;i++)
cout<< "| |\n";
cout<< "_________________________________\n";
int x1 = rand() % 19 + 1,
y1 = rand() % 19+ 1,
x2 = rand() % 19 + 1,
y2 = rand() % 19 + 1,
x3=2,
y3=2,
incX=-1,
incY=-1,
incXX=1,
incYY=1,
speed=70;
while (true){
gotoxy(x, y);
cout << s;
if (kbhit())
{
gotoxy(x, y);
cout << h;
i = getch();
if (i == 'q')
{
break;
}
if (i == UP && y>UPLIMIT)
y=y-2;
else if (i == DOWN && y<LOWLIMIT)
y=y+2;
if (i == LEFT && x>LEFTLIMIT)
x = x - 3;
else if (i == RIGHT && x<RIGHTLIMIT-6)
x = x + 3;
}
drawObject(x3, y3);
print_scores(scores);
hideObject(x3, y3);
if(y1<LOWLIMIT)
drawObject(x1, y1);
if(y2<LOWLIMIT)
drawObject(x2, y2);
Sleep(speed);// to slow down the process
hideObject(x1, y1);
hideObject(x2, y2);
if (x1 == RIGHTLIMIT) incX = -1;
if (x1 == LEFTLIMIT) incX = 1;
if (y1 == LOWLIMIT)
{
incY = 0;
speed=40;
}
if (x1>x-2 && x1<x+7 )
{ if(y1==y-1)
{
incY=-1;
scores=scores+10;
total_scores=total_scores+10;
}
}
if (y1 == UPLIMIT) incY = 1;
x1 += incX;
y1 += incY;
if (x2 == RIGHTLIMIT) incXX = -1;
if (x2 == LEFTLIMIT) incXX = 1;
if (y2 == LOWLIMIT )
{
incYY = 0;
speed=40;
}
if (x2>x-2 && x2<x+7 )
{ if(y2==y-1){
incYY=-1;
scores=scores+10;
total_scores=total_scores+10;
}
}
if (y2 == UPLIMIT) incYY = 1;
x2 += incXX;
y2 += incYY;
speed=speed-1;
if (speed<25)
speed=25;
if(incY==0 && incYY==0)
{
system("cls");
cout<<"\n\n\n\tG";Sleep(50);cout<<"a";Sleep(50);cout<<"m";Sleep(50);cout<<"e";Sleep(50);cout<<" ";Sleep(50);cout<<"O";Sleep(50);cout<<"v";Sleep(50);cout<<"e";Sleep(50);cout<<"r";
system("pause>nul");
system("cls"); //this line is clearing my scores
break;
}
if(incY==0 && incYY==0)
break;
if(scores>=score2)
{
ofstream out("records.txt");
out<<scores;
out.close();
ofstream out1("total_scores.txt");
out1<<total_scores;
out1.close();
break;
}
}
}
答案 0 :(得分:-2)
system("cls")
不要清除变量的值。如果您遇到问题,请在清除屏幕之前将变量保存到文件中,然后在system("cls")
行之后加载。