저는 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;
}