按下一个键后,在保龄球游戏中移动一个球

时间:2013-06-28 14:29:07

标签: c++ eclipse opengl graphic 2d-games

我一直在用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();
}

1 个答案:

答案 0 :(得分:2)

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

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

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