2017-06-05 15 views
-4

ボール/オブジェクトヒットゲームをC++で作成するプロジェクトに取り掛かりました。 system("cls");を使用して画面を消去し、Game Overメッセージを表示しました。ユーザーのスコアは0にリセットされます0system("cls");を削除してもユーザーのスコアには影響しません。 vairablesの値をクリアせずに画面をクリアする良い方法はありますか?システム( "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

Uhm、それはすべてsystem( "cls")ですが、画面をクリアします。あなたの変数には何もしません。 –

+0

'total_scores'変数はどこから来ますか?それはグローバル変数ですか? – Felipe

+0

'system(" cls ");が問題の原因ですか? – Bernard

答えて

-2

system("cls")変数の値をクリアしないでください。問題がある場合は、画面をクリアしてsystem("cls")行の後に読み込む前に、変数をファイルに保存してください。

関連する問題