gpt4 book ai didi

c++ - GLUT 暂停键盘的工作

转载 作者:行者123 更新时间:2023-11-28 04:51:06 25 4
gpt4 key购买 nike

我正在尝试在 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);
}

最佳答案

有一个保存的时间值,您可以在键盘处理程序的开头与它进行比较,如果没有足够的时间过去,只需返回。接下来,当且仅当接受键操作时更新该值。

关于c++ - GLUT 暂停键盘的工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com