2017-06-05 70 views
-4

我正在一個項目中使用C++編寫球/對象命中游戲。 我使用system("cls");來清除屏幕並顯示Game Over消息。用戶分數正被重置爲零0。同時刪除system("cls");不會影響用戶的分數。有沒有什麼好的方法來清除屏幕而不清除可變值的值?是否系統(「CLS」)也清除我的變量存儲在C++

下面是一個示例代碼。這是我遊戲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; 
    } 
    } 
} 
+7

呃,這就是所有的'system(「cls」)'',清除屏幕。它不會對你的變量做任何事情。 –

+0

'total_scores'變量來自哪裏?它是一個全局變量嗎? – Felipe

+0

你確定'system(「cls」);'是你問題的原因嗎? – Bernard

回答

-2

system("cls")不清除變量的值。如果您遇到問題,請在清除屏幕並在system("cls")行之後加載之前將變量保存在文件中。