2017-10-11 17 views
1

我想跟隨使用C++的蛇教程遊戲。但是,似乎他們正在使用一些Windows庫,在我的情況下,我正在Linux上編譯它。問題出在我的Input()方法上,我嘗試用我在這裏找到的推薦代碼進行更改,但這對我來說並沒有什麼效果。有沒有辦法解決這個或任何建議?謝謝。Linux的輸入鍵是不可管理的,我怎樣才能在C++中使用它?

#include <iostream> 
    #include <stdio.h> 
    #include <ncurses.h> 
    #include <unistd.h> 
    ..... 
    //Get the key input inorder to move the snake around 
    void Input() { 
     //Tried this from the stackoverlow recommendations, did not work for my situation 
     if (getch() == '\033') { 
     getch(); 
     switch(getch()) { // the real value 
      case 'A': 
       // code for arrow up 
       direction = UP; 
       break; 
      case 'B': 
       // code for arrow down 
       direction = DOWN; 
       break; 
      case 'C': 
       // code for arrow right 
       direction = RIGHT; 
       break; 
      case 'D': 
       // code for arrow left 
       direction = LEFT; 
       break; 
      case 'Q': 
       gameOver = true; 
       break; 
      default: 
       break; 
     } 
    } 

    } 
    ..... 

我與遇到的問題是,Linux不接受的kbhit()這是什麼蛇遊戲教程採用的,所以我想與我有什麼上述修改它,但它不動蛇。

+0

什麼「沒有工作」? –

+0

@BradS。我的輸入法,教程設置的方式是使用kbhit(),並且僅適用於Windows。不過,我想找到一種方法來做到這一點在Linux中。 – KonoDDa

+0

所以,你的問題是你必須打回來? –

回答

-2

這個case 'A':期望輸入大寫字母。通常情況並非如此:)。所以:

switch(getch()) { // the real value 
      case 'A': // either capital 
      case 'a': // or low case 
       // code for arrow up 
       direction = UP; 
       break; 
+0

在開關之前可以使用'toupper()'或'tolower()':'switch(toupper(getch()))' –

+1

這不是問題;方向鍵確實發送逃生,然後是大寫字母(在大多數終端上)。 –

相關問題