有没有办法在OpenGL中同时移动两个方块?
所以我有一个函数可以处理我正在 OpenGL 中开发的游戏中的按键操作。但是,问题是,即使我制作了两个方块,并且当按下正确的键时它们都会移动,只有一个方块移动。有什么办法可以让两个方块移动吗?这是我实现的 glutKeyboardFunc 函数:
void handleKeypress(unsigned char key, int x, int y)
{
if (key == 'w')
{
for (int i = 0; i < 12; i++)
{
if (i == 1 || i == 7 || i == 10 || i == 4)
{
square[i] = square[i] + 0.1;
}
}
}
if (key == 'd')
{
for (int i = 0; i < 12; i++)
{
if (i == 0 || i % 3 == 0)
{
square[i] = square[i] + 0.1;
}
}
}
if (key == 's')
{
for (int i = 0; i < 12; i++)
{
if (i == 1 || i == 7 || i == 10 || i == 4)
{
square[i] = square[i] - 0.1;
}
}
}
if (key == 'a')
{
for (int i = 0; i < 12; i++)
{
if (i == 0 || i % 3 == 0)
{
square[i] = square[i] - 0.1;
}
}
}
glutPostRedisplay();
}
如果您需要更多代码,请询问。
so I have a function that handles key presses in a game I'm working on in OpenGL. But, the thing is that even though I have made two squares and they both move when the correct key is pressed only one square is moved. Is there a way I can make the two squares move. This is the glutKeyboardFunc function I implimented:
void handleKeypress(unsigned char key, int x, int y)
{
if (key == 'w')
{
for (int i = 0; i < 12; i++)
{
if (i == 1 || i == 7 || i == 10 || i == 4)
{
square[i] = square[i] + 0.1;
}
}
}
if (key == 'd')
{
for (int i = 0; i < 12; i++)
{
if (i == 0 || i % 3 == 0)
{
square[i] = square[i] + 0.1;
}
}
}
if (key == 's')
{
for (int i = 0; i < 12; i++)
{
if (i == 1 || i == 7 || i == 10 || i == 4)
{
square[i] = square[i] - 0.1;
}
}
}
if (key == 'a')
{
for (int i = 0; i < 12; i++)
{
if (i == 0 || i % 3 == 0)
{
square[i] = square[i] - 0.1;
}
}
}
glutPostRedisplay();
}
If you need any more code just ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑了下面的评论。
您需要在游戏逻辑中的某个位置处理键盘事件(主循环或来自
glutKeyboardFunc()
的回调),并调用所需的行为。这有一些优点:if
而不是switch
允许使用多个键。Edited for comments below.
You need to handle your keyboard events somewhere in your game logic (the main loop, or a callback from
glutKeyboardFunc()
), and call the desired behaviours. This has some advantages:if
instead ofswitch
allows multiple keys being used.