问题描述
有没有办法使用 nCurses (C) 将图形及其运动打印到终端中,定义宏如下:
macro.h
#define UP 65
#define DOWN 66
#define RIGHT 67
#define LEFT 68
我的想法是:当我按下方向键时,这个数字会移动。例如,如果我按右方向键,图形会向右移动。
但我不知道该怎么做。也许使用 while(1)
?
解决方法
我已经解决了第一个问题。现在我面临这个问题:
我定义了一个 struct
,其中我定义了矩阵,该矩阵表示我想在终端中移动的图形,但每次移动时,图形都与我定义的图形不同。
这是我的结构
typedef struct {
int pid;
int x;
int y;
char matrix[3][5];
char dir;
} Starship;
我在这里定义了这个数字:
Starship strsh;
strsh.pid=getpid();
strcpy(strsh.matrix[0]," | ");
strcpy(strsh.matrix[1],"|_o_|");
strcpy(strsh.matrix[2]," --- ");
还有移动:
while(true){
int c = getch();
switch(c){
case UP:
if(strsh.y > 0)
strsh.y -= 1;
for (i = 0; i<=1; i++){
printf("\n");
mvaddstr(strsh.y,strsh.x,strsh.matrix[i]);
}
break;
case DOWN:
if(strsh.y < MAXY - 1)
strsh.y += 1;
for (i = 0; i<=1; i++){
printf("\n ");
mvaddstr(strsh.y,strsh.matrix[i]);
}
break;
case LEFT:
if(strsh.x > 0)
strsh.x -= 1;
for (i = 0; i<=1; i++){
printf("\n ");
mvaddstr(strsh.y,strsh.matrix[i]);
}
break;
case RIGHT:
if(strsh.x < MAXX - 1)
strsh.x +=1;
for (i = 0; i<1; i++){
printf("\n ");
mvaddstr(strsh.y,strsh.matrix[i]);
}
break;
case 113:
endwin();
exit(0);
}
clear();
//mvaddch(strsh.y,mvinch(strsh.y,strsh.x));
for (i = 0; i<3; i++){
printf("\n ");
mvaddstr(strsh.y,strsh.matrix[i]);
}
refresh();
}
}
但是,在终端我有这个:
--- --- ---
我做错了什么?