2015-05-04 105 views
-2

所以我的大部分遊戲都完成了,但是我想將玩家的分數和名字保存到一個文本文件並顯示出來。到目前爲止,用戶輸入工作並保存到一個名爲score.txt的文本文件,但是輸入是不可見的,而且看不到您輸入的內容。有誰知道爲什麼會發生這種情況?輸入隱形C++迷宮遊戲

主要

int main() 
{ 
    DWORD  mode;   /* Preserved console mode */ 
    INPUT_RECORD event;   /* Input event */ 
    BOOL   EXITGAME = FALSE; /* Program termination flag */ 
    unsigned int counter = 0; /* The number of times 'Esc' is pressed */ 


    /* Get the console input handle */ 
    HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE); 

    /* Preserve the original console mode */ 
    GetConsoleMode(hstdin, &mode); 

    /* Set to no line-buffering, no echo, no special-key-processing */ 
    SetConsoleMode(hstdin, 0); 

    srand (time(NULL)); //initialize the random seed 

    // Variables 
    int health = 2; 

    // Declare variable positions 

    player.x=1; 
    player.y=1; 
    treasure.x = 20; 
    treasure.y = 5; 
    treasure.z= 0; 
    traps.x = 1; 
    traps.y = 7; 
    traps.z = 0; 
    lives.x = 1; 
    lives.y = 9; 
    ofstream file; 
    int score = 0; 
    string name; 
    string line; 
/* 
    while(treasure.z < 2) 
    { 
    treasure.x = (rand() % 24); 
    treasure.y = (rand() % 16); 
    if(treasure.x == 0 && treasure.y == 0) 
     { 
     treasure.z++; 
     } 
    } 
    while(traps.z < 3) 
    { 
    traps.x = (rand() % 24); 
    traps.y = (rand() % 16); 
    if(traps.x == 0 && traps.y == 0) 
     { 
     traps.z++; 
     } 
    } 
    while(lives.z < 2) 
    { 
    lives.x = (rand() % 24); 
    lives.y = (rand() % 16); 
    } 
    */ 
    clrscr(); 
    setcolor(15); 


    while (!EXITGAME) 
    { 
     if (WaitForSingleObject(hstdin, 0) == WAIT_OBJECT_0) /* if kbhit */ 
     { 
      DWORD count; /* ignored */ 

      /* Get the input event */ 
      ReadConsoleInput(hstdin, &event, 1, &count); 

      /* Only respond to key release events */ 
      if ((event.EventType == KEY_EVENT) 
      && !event.Event.KeyEvent.bKeyDown) 

      clrscr(); 
      putmenu(); 
      gotoxy(6,20); 
      cout<<"Lives: " << health; 
      gotoxy(6,22); 
      cout<<"Score: " << score; 

      Sleep(50); 


       switch (event.Event.KeyEvent.wVirtualKeyCode) 
       { 
        case VK_ESCAPE: 
         clrscr(); 

         putend(); 

         EXITGAME = TRUE; 
        break; 

        case VK_LEFT: 
         // left key move player left 
         moveleft(); 
        break; 

        case VK_RIGHT: 
         // right key move player right 
         moveright(); 

        break;  

        case VK_UP: 
         // up key move player up 
         moveup(); 

        break; 

        case VK_DOWN: 
         // down key move player down 
         movedown(); 

        break; 

        case VK_A: 
         // left key move player left 
         moveleft(); 

        break; 

        case VK_D: 
         // right key move player right 

         moveright(); 
        break; 

        case VK_W: 
         // up key move player up 

         moveup(); 
        break; 

        case VK_S: 
         // down key move player down 
         movedown(); 
        break; 


       }//switch 

       puttreasure(); 
       puttraps(); 
       putlives(); 
       putplayer();     
       if((player.x == lives.x) && (player.y == lives.y)) 
        { 
        health++; 
        lives.x = 0; 
        lives.y = 0; 
        } 
       if((player.x == traps.x) && (player.y == traps.y)) 
        { 
        health--; 
        traps.x = 0; 
        traps.y = 0; 
        } 

       if((player.x == treasure.x)&& (player.y == treasure.y)) 
        { 
        score += 100; 
        EXITGAME = true; 
        } 
       else if(health == 0) 
        { 
        EXITGAME = true;     
        } 
       if(EXITGAME == true) 
        { 
        score = score + (health * 100); 
        } 

     } 

    } 

    if(EXITGAME == true) 
     { 
     // clear screen 
     clrscr(); 

     } 

     setcolor(10); 
    cout << "Enter your name "; 

    cin >> name; 

    ofstream out("score.txt"); 
    out << name; 
    out << "\n"; 
    out << score; 
    out.close(); 


/* if(file.is_open()) 
     { 
     while(getline (file, line)) 
      cout << line << '\n'; 
     }*/ 

    gotoxy(1,23);cout<<" "; 
    SetConsoleMode(hstdin, mode); 
    return 0; 

} 
+4

什麼是'gotoxy','clrscr','setcolor'?舊時代的專有功能(Turbo C)? – PaulMcKenzie

+1

會更好[MCVE](http://stackoverflow.com/help/mcve)。大部分代碼都是關於不同的情況,並且與輸入/輸出無關。 – user463035818

回答

3
/* Set to no line-buffering, no echo, no special-key-processing */ 

您關閉回聲 - 意味着用戶不會看到他們鍵入

+0

謝謝!我修好了它! – JBo

+1

您能否將其格式化爲代碼塊?我的強迫症踢在... – nonsensickle

+0

@nonsensickle - 那裏你去 - 你現在可以放鬆 – pm100