2017-09-29 12 views
-1

저는 C++과 ncurses를 처음 사용합니다. 사용자 입력이 플레이어를 제어 할 수 있도록 initscr()을 코드에 추가하면 시작될 때 디스플레이가 표시되지 않고 디스플레이가 표시하는 버튼을 누르면 모든 이상한 상태가됩니다. 이는 내 main() 메소드에 initscr()을 추가하는 경우에만 발생합니다. 왜 이렇게하고 어떻게 고칠 수 있습니까?initscr()이 엉망진창입니다.

#include <iostream> 
#include <curses.h> 
using namespace std; 

bool gameOver; 
bool pressed; 
const int width = 20; 
const int height = 20; 
int x, y, fruitX, fruitY, score; 
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; 
eDirection dir; 

void Setup() { 
    gameOver = false; 
    dir = STOP; 
    x = width/2; 
    y = height/2; 
    fruitX = rand() % width; 
    fruitY = rand() % height; 
    score = 0; 
} 

void Draw() { 
    system("clear"); 
    for (int i = 0; i < width + 2; i++) 
     cout << "#"; 
    cout << endl; 

    for (int i = 0; i < height; i++) { 
     for (int j = 0; j < width; j++) { 
      if (j == 0) 
       cout << "#"; 
      if (i == y && j == x) 
       cout << "O"; 
      else if (i == fruitY && j == fruitX) 
       cout << "F"; 
      else 
       cout << " "; 

      if (j == width - 1) 
       cout << "#"; 
     } 
     cout << endl; 
    } 

    for (int i = 0; i < width + 2; i++) 
     cout << "#"; 
    cout << endl; 
} 

void Input() { 
    switch (getch()) { 
     case 'a': 
      dir = LEFT; 
      break; 
     case 'd': 
      dir = RIGHT; 
      break; 
     case 'w': 
      dir = UP; 
      break; 
     case 's': 
      dir = DOWN; 
      break; 
     case 'x': 
      gameOver = true; 
      break; 
    } 
} 

void Logic() { 
    switch (dir) { 
     case LEFT: 
      x--; 
      break; 
     case RIGHT: 
      x++; 
      break; 
     case UP: 
      y--; 
      break; 
     case DOWN: 
      y++; 
      break; 
     default: 
      break; 
    } 
} 

int main() { 
    //initscr(); 
    Setup(); 
    while(!gameOver) { 
     Draw(); 
     Input(); 
     Logic(); 
    } 
    //endwin(); 
    return 0; 
} 

답변

2

curses를 비 curses 화면 제어로 혼합하여 라이브러리 간의 충돌 결과를 확인합니다.

curses를 사용하려면 그냥 사용하십시오. 올바르게 수행하는 방법은 NCURSES Programming HOWTO을 숙독하십시오. (문서뿐만 아니라 PDCurses의 대부분이 적용됩니다.)

TL을, 박사 : system() 통화 없애하면 화면에 물건을 수행하고 출력을 인쇄 할 *addstr()*printw() 기능을 사용할 수 있습니다. [w]refresh()을 잊지 마세요.