0

我一直在用 C++ 开发保龄球游戏。

我只想要一件事,我只想在按下一个键并且碗平稳移动时移动球(不像现在,因为它通过按住 UP-KEY 移动)。

这是代码: -

#include <GL/glut.h>
#include <cmath>

GLfloat posX = 0.07, posY = 0.1, posZ = 0.0,

firstx1 = 0.02, firsty1 = 0.3, firstx2 = 0.07, firsty2 = 0.3, firstx3 = 0.11, firsty3 = 0.3,

secondx1 = -0.16, secondy1 = 0.3, secondx2 = -0.21, secondy2 = 0.3, secondx3 = -0.27, secondy3 = 0.3,

thirdx1 = 0.3, thirdy1 = 0.3, thirdx2 = 0.35, thirdy2 = 0.3, thirdx3 = 0.4, thirdy3 = 0.3;


double x, y, angle;
#define PI 3.1415926535898
GLint circle_points = 50;

void bottle() {
    glColor3f(0.0, 0.0, 1.0);
    glPointSize(9.0);
    glBegin(GL_POINTS);
    glVertex3f(firstx1, firsty1, 0.0);
    glVertex3f(firstx2, firsty2, 0.0);
    glVertex3f(firstx3, firsty3, 0.0);

    glVertex3f(secondx1, secondy1, 0.0);
    glVertex3f(secondx2, secondy2, 0.0);
    glVertex3f(secondx3, secondy3, 0.0);

    glVertex3f(thirdx1, thirdy1, 0.0);
    glVertex3f(thirdx2, thirdy2, 0.0);
    glVertex3f(thirdx3, thirdy3, 0.0);

    glEnd();
    glFlush();
}

void circ() {
    glColor3f(0.0, 0.0, 1.0);
    glBegin(GL_TRIANGLE_FAN);
    for (int i = 0; i <= 300; i++) {
        angle = 2 * PI * i / 300;
        x = cos(angle) / 25;
        y = sin(angle) / 20;
        glVertex2d(x, y);
    }
    glEnd();
}

void display() {
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    bottle();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(posX, posY, posZ);
    circ();
    glPopMatrix();

    glutSwapBuffers();
}

float move_unit = 0.01;
void keyboardown(int key, int x, int y) {
    switch (key) {
    case GLUT_KEY_RIGHT:
        posX += 0.3;
        break;
    case GLUT_KEY_LEFT:
        posX -= 0.3;
        break;
    case GLUT_KEY_UP:
        posY += move_unit;
        break;
    case GLUT_KEY_DOWN:
        posY -= move_unit;
        break;

    default:
        break;
    }

    if ((posX >= firstx1 && posX <= firstx3)
            && (posY == firsty1 && posY == firsty2 && posY == firsty3)) {
        firstx1 += 0.02;
        firsty1 += 0.03;
        firstx2 += -0.06;
        firsty2 += 0.02;
        firstx3 += 0.03;
        firsty3 += 0.05;
    }

    if ((posX <= secondx1 && posX >= secondx3)
                && (posY == secondy1 && posY == secondy2 && posY == secondy3)) {
            secondx1 += 0.02;
            secondy1 += 0.02;
            secondx2 += -0.06;
            secondy2 += 0.02;
            secondx3 += 0.03;
            secondy3 += 0.05;
        }

    if ((posX >= thirdx1 && posX <= thirdx3)
                && (posY == thirdy1 && posY == thirdy2 && posY == thirdy3)) {
            thirdx1 += 0.02;
            thirdy1 += 0.03;
            thirdx2 += -0.07;
            thirdy2 += 0.02;
            thirdx3 += 0.03;
            thirdy3 += 0.05;
        }

    glutPostRedisplay();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 300);
    glutInitWindowPosition(150,250);
    glutCreateWindow("Balling Game");
    glutDisplayFunc(display);
    glutSpecialFunc(keyboardown);
    glutMainLoop();
}
4

1 回答 1

2

您依靠键盘侦听器线程为您完成工作。您将需要创建一个位置更新线程,并缓存您收到的最后一个移动命令。更新线程将需要定期运行(例如每秒 60 次)并每次更新球的当前位置。

最好的方法是让你的程序更加面向对象。然后你会有一个“绘图”线程,它只是查看场景中对象的状态。它会询问 Ball 对象的当前位置。球会根据它的速度(方向加速度)、自上次速度变化以来经过的时间以及最后一次速度变化发生的位置知道它在哪里。

第一步虽然只是存储最后按下的方向并定期更新位置。

于 2013-06-28T14:38:49.803 回答