콘솔 버전의 pong을 만들려고합니다. 나는 대부분의 일을 끝내었지만 성가신 문제에 부딪쳤다. 플레이어는 화살표 키로 제어되지만, 길게 누르면 반복되기 전에 지연됩니다. 키를 놓을 때 키를 누를 때 등록 방법을 찾고 있으므로이 문제를 없앨 수 있습니다.키 누름을 별도로 등록하십시오. C++
#include <ncurses.h>
#define FX 101
#define FY 30
void clearmid();
void drawplayer(int playerpos);
int fieldx = FX;
int fieldy = FY;
char field[FY][FX];
int main(int argc, const char *argv[])
{
initscr();
//raw();
noecho();
keypad(stdscr, TRUE);
int i, j;
/* Clear Playing Field */
for(i = 0; i != fieldy; i++){
for(j = 0; j != fieldx; j++){
field[i][j] = '#';
}
}
/* Add in Newlines so field can be printed as one string */
for(i = 0; i != fieldy - 1; i++){
field[i][fieldx - 1] = '\n';
}
field[fieldy - 1][fieldx - 1] = '\0'; // Null terminator
int player1pos, player2pos;
int ballx, bally, ballxv, ballyv;
player1pos = fieldy/2;
player2pos = player1pos;
ballx = (fieldx - 1)/2;
bally = player1pos;
/* Ball velocity in given axis */
ballxv = 1;
ballyv = 1;
int c;
timeout(0);
while(1){
c = 0;
c = getch();
switch(c){
case KEY_UP:{
if(player1pos > 2){
player1pos--;
}
break;
}
case KEY_DOWN:{
if(player1pos < fieldy - 3){
player1pos++;
}
break;
}
case 'q':{
endwin();
return 0;
}
default:{
break;
}
}
clearmid();
drawplayer(player1pos);
if((field[bally - 1][ballx] != ' ') || (field[bally + 1][ballx] != ' ')){
if(ballyv == 1) ballyv = -1;
else ballyv = 1;
}
if((field[bally][ballx - 1] != ' ') || (field[bally][ballx + 1] != ' ')){
if(ballxv == 1) ballxv = -1;
else ballxv = 1;
}
ballx += ballxv;
bally += ballyv;
field[bally][ballx] = '*';
move(0,0);
printw("%s", field);
refresh();
usleep(30000);
}
return 0;
}
void drawplayer(int playerpos){
field[playerpos-1][2] = '|';
field[playerpos][2] = '|';
field[playerpos+1][2] = '|';
}
void clearmid(){
int i, j;
for(i = 1; i != fieldy -1; i++){
for(j = 1; j != fieldx - 2; j++){
field[i][j] = ' ';
}
}
}
그의 코드는 이미 ncurses를 사용합니다. –