0

我正在尝试使用 C/ncurses 实现生命游戏。我希望我的游戏具有的一个功能是从用户那里获取 X、Y 坐标,并在这些坐标处在板上绘制一个形状。我有一个可以在板上绘制形状并且工作正常的功能。该函数的标题是:

void draw_shape(int x, int y, int shape[3][3])

绘制形状的按键是在我的游戏的标题状态下处理的。绘制形状函数用于绘制任何形状。我唯一的问题是从用户那里获取 X、Y 值。

我的游戏的标题状态:case TITLE: test = 0; 整数 i, j;

        for(i = 0; i < (WELL_WIDTH-1); i++)
            for(j = 0; j < (WELL_HEIGHT-1); j++)
                cells[i][j] = create_cell(x_offset + i, y_offset + j);


        w = init_well(x_offset - 1, y_offset - 1, well_w, well_h);
        draw_well(w);
        mvprintw(y_offset + 5, x_offset + 10, "Welcome to the Game of Life!");
        mvprintw(y_offset + 7, x_offset + 9, "Press 'S' to start a new game.");
        mvprintw(y_offset + 8, x_offset + 9, "Press 'L' to load a saved game.");
        mvprintw(y_offset + 12, x_offset + 20, "-OR-");
        mvprintw(y_offset + 16, x_offset + 8, "Pick a shape to draw on the screen:");
        mvprintw(y_offset + 18, x_offset + 9, "(A) Still life   (B) Glider"); 
        mvprintw(y_offset + 19, x_offset + 9, "(C) Oscillator   (D) Random");


        keystrokes = read_keys();
        if (keystrokes == START_SAVE) {
            state = INIT; }
        if (keystrokes == LOADBOARD) {
            state = LOADGAME;
        }
        if (keystrokes == STILL_LIFE) {
            state = STILLIFE;

        }

当按下“A”时,状态切换到 STILLLIFE。这是我想从用户那里获取 X、Y 值并将它们传递给 drawshape 函数的地方。

case STILLLIFE:

        w = init_well(x_offset - 1, y_offset - 1, well_w, well_h);
        draw_well(w);
        echo();

        int x_cord, y_cord;

        mvprintw(y_offset + 21, x_offset + 7, "Enter X, Y coordinates     for your shape: ");

理想情况下,我会做类似的事情: scanw("%d, %d", &x_cord, &y_cord); 然后 draw_shape(x_cord, y_cord, shape); 但是当我尝试这个时,它似乎不起作用。我会尝试像这样显示 x_cord 和 y_cord 的值: mvprintw(y_offset + 23, x_offset + 7, "%d, %d", x_cord, y_cord); 但是,当我打开游戏时,在我输入任何内容之前,x_cord 和 y_cord 已经存在虚假整数值。我尝试了许多不同的方法,我真的只是在寻找与scanf等效的东西。任何建议或指导将不胜感激。谢谢你。

4

1 回答 1

0

如果您还没有解决这个问题,您需要fflush输入或处理 EOF 数据,否则有多种方法scanf(或scanw等效方法)可能会失败。scanf 老实说有点烂。 sscanf并且fscanf没问题,但是 . 有很多问题scanf。常见的建议是使用fread整个字符串,然后用sscanf. 或者,如果您只需要单字母输入,请考虑使用getchor wgetch

于 2018-12-14T17:56:08.420 回答