开发者

Is there a way to move two squares in OpenGL simultaneously?

开发者 https://www.devze.com 2022-12-30 04:57 出处:网络
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 on

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;
                }
            }
        }
    glutPost开发者_StackOverflowRedisplay();
}

If you need any more code just ask.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消