2017-09-29 8 views
-1

C++로 스네이크 게임을 만들려고합니다. 키 입력을 위해 ncurses 패키지를 사용하고 있는데 키 입력을 가능하게하기 위해 initscr()을 넣어야한다는 것을 알고 있지만 어디에 있는지 모른다. 나는 main 메소드의 맨 위에서 시도했으나 디스플레이에 예기치 않은 문제가 발생했습니다. 또한 Input() 메서드에 넣으려고했지만 디스플레이에도 문제가 발생했습니다. 나는 어디에 넣을 지 모르겠다. 그렇지 않으면 내가 작동하도록 다른 것을 추가 할 필요가 있지만 누군가 나를 도울 수 있는가?어디에 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() { 
    Setup(); 
    while(!gameOver) { 
     Draw(); 
     Input(); 
     Logic(); 
    } 
    return 0; 
} 

답변

0

initscr은 (어떤 I/O 전) "초"라고해야합니다. 그러나 귀하의 예는

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

을 (당신이 플러시 출력, 저주로 다시/앞뒤로 전환하지 않는 한 작동하지 않을 것이다)을 getch의 혼합물 (아마도 저주, conio 수)과 표준 입출력을 표시하는 방법 코드를 실행에 대한과 당신이 알아 차린 것을 말해주십시오. (또는 당신이 Cba 인 경우 : 질문에서 말했듯이 디스플레이가 깨집니다.). – user8694810