有没有办法在OpenGL中同时移动两个方块?

发布于 2024-08-31 21:42:36 字数 1232 浏览 1 评论 0原文

所以我有一个函数可以处理我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-09-07 21:42:36

编辑了下面的评论。

// I'm making some assumptions about your functions here. Make adjustments.
// You can handle both players' key inputs here.
void handleKeypress(unsigned char key, int x, int y) 
{
    if (key == 27)
        exit(0);

    // Player 1
    if (key == 'w')
    {
        A.moveSquareUp();
    }
    if (key == 'd')
    {
        A.moveSquareRight();
    }
    if (key == 's')
    {
        A.moveSquareDown();
    }
    if (key == 'a')
    {
        A.moveSquareLeft();
    }
}

void handleSpecialKeypress(int key, int x, int y) 
{
    // Player 2
    if (key == GLUT_KEY_UP)
    {
        B.moveSquareUp();
    }
    if (key == GLUT_KEY_RIGHT)
    {
        B.moveSquareRight();
    }
    if (key == GLUT_KEY_DOWN)
    {
        B.moveSquareDown();
    }
    if (key == GLUT_KEY_LEFT)
    {
        B.moveSquareLeft();
    }
}

您需要在游戏逻辑中的某个位置处理键盘事件(主循环或来自 glutKeyboardFunc() 的回调),并调用所需的行为。这有一些优点:

  1. 您的键盘输入处理统一在一处。
  2. 使用 if 而不是 switch 允许使用多个键。
  3. 您可以在主循环内组织此事件处理,而不是依赖于计时器。

Edited for comments below.

// I'm making some assumptions about your functions here. Make adjustments.
// You can handle both players' key inputs here.
void handleKeypress(unsigned char key, int x, int y) 
{
    if (key == 27)
        exit(0);

    // Player 1
    if (key == 'w')
    {
        A.moveSquareUp();
    }
    if (key == 'd')
    {
        A.moveSquareRight();
    }
    if (key == 's')
    {
        A.moveSquareDown();
    }
    if (key == 'a')
    {
        A.moveSquareLeft();
    }
}

void handleSpecialKeypress(int key, int x, int y) 
{
    // Player 2
    if (key == GLUT_KEY_UP)
    {
        B.moveSquareUp();
    }
    if (key == GLUT_KEY_RIGHT)
    {
        B.moveSquareRight();
    }
    if (key == GLUT_KEY_DOWN)
    {
        B.moveSquareDown();
    }
    if (key == GLUT_KEY_LEFT)
    {
        B.moveSquareLeft();
    }
}

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:

  1. Your keyboard input handling is unified in one place.
  2. Using if instead of switch allows multiple keys being used.
  3. You can organise this event handling inside your main loop, instead of depending on the timer.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文