GLUT暂停了键盘的工作

时间:2018-01-12 20:49:41

标签: c++ opengl glut

我正在尝试在GLUT c ++上写“Classic Snake”并遇到一些问题。

例如,当我的蛇向右移动时,我将目的地改为向上,同时将它改为右边蛇进入其中(我禁止有机会从右向左改变目的地)。我想在呼叫一秒后尝试暂停键盘功能,但我找不到任何决定。

我会在这里附上一段代码:

void keyboardSpecial(int k, int x, int y) {
    switch (k) {
        case GLUT_KEY_UP:
            if (snake.dest == UP || snake.dest == DOWN) break;
            else snake.dest = UP;
            break;
        case GLUT_KEY_RIGHT:
            if (snake.dest == LEFT || snake.dest == RIGHT) break;
            else snake.dest = RIGHT;
            break;
        case GLUT_KEY_DOWN:
            if (snake.dest == UP || snake.dest == DOWN) break;
            else snake.dest = DOWN;
            break;
        case GLUT_KEY_LEFT:
            if (snake.dest == LEFT || snake.dest == RIGHT) break;
            else snake.dest = LEFT;
            break;
    }
}

void timer(int) {
    //Eating and growing
    if(snake.sn[snake.size - 1].x == food.pos.x && snake.sn[snake.size - 1].y == food.pos.y) {
        food.eat();
        snake.sn[snake.size].x = snake.sn[snake.size - 1].x;
        snake.sn[snake.size].y = snake.sn[snake.size - 1].y;
        snake.size++;
    }

    //Motion
    for (int i = 1; i < snake.size; i++) {
        snake.sn[i - 1].x = snake.sn[i].x;
        snake.sn[i - 1].y = snake.sn[i].y;
    }
    if (snake.dest == RIGHT) {
        snake.sn[snake.size - 1].x++;
    }
    if (snake.dest == UP) {
        snake.sn[snake.size - 1].y--;
    }
    if (snake.dest == LEFT) {
        snake.sn[snake.size - 1].x--;
    }
    if (snake.dest == DOWN) {
        snake.sn[snake.size - 1].y++;
    }

    //Walls
    if(snake.sn[snake.size - 1].x <= 0) snake.sn[snake.size - 1].x += 640 / dx;
    if(snake.sn[snake.size - 1].x >= 640 / dx) snake.sn[snake.size - 1].x -= 640 / dx;
    if(snake.sn[snake.size - 1].y <= 0) snake.sn[snake.size - 1].y += 480 / dy;
    if(snake.sn[snake.size - 1].y >= 480 / dy) snake.sn[snake.size - 1].y -= 480 / dy;

    //Game Over
    for (int i = 0; i < snake.size - 1; i++) {
        if(snake.sn[snake.size - 1].x == snake.sn[i].x && snake.sn[snake.size - 1].y == snake.sn[i].y) {cout << "Game Over!" << endl;exit(0);}
    }

    display();
    glutTimerFunc(200 - snake.size, timer, 0);
}

1 个答案:

答案 0 :(得分:1)

在键盘处理程序的开头比较一个保存的时间值,如果没有足够的时间,则返回。接下来,当且仅当接受了键操作时才更新该值。