在保龄球比赛中,一旦按下一个键,就移动一个球
Move a ball in bowling game once a key is pressed
我一直在用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();
}
您正依靠键盘侦听器线程来完成您的工作。您需要创建一个位置更新线程,并缓存您收到的最后一个移动命令。更新线程将需要定期运行(例如每秒60次),并每次更新球的当前位置。
最好的方法是使程序更加面向对象。然后你会有一个"绘制"线程,它只是查看场景中对象的状态。它会询问Ball对象的当前位置。球将根据其速度(方向加速度)、自上次速度变化以来经过的时间以及上次速度变化发生的位置来知道它在哪里。
不过,对于第一步,只需存储最后按下的方向,并定期更新位置。
相关文章:
- 如果需要转换,我可以在读取参数的同时将其移动到另一个参数吗?
- 如何知道是否移动一个物体
- 当在SFML C 中移动一个圆圈时,球后面会有口吃的束缚小故障
- 从另一个函数的"if loop"中调用/移动一个函数 - Qt,C++
- 我应该移动一个不再使用的值吗?
- 移动一个结构数组C++
- 在OpenGL中移动一个简单的形状(形状在数据结构中)
- 如果我移动一个值进行注册和编辑,它将有所作为
- 在保龄球比赛中,一旦按下一个键,就移动一个球
- 想要移动一个2d对象而不是另一个
- 关于移动一个const对象
- 在现代opengl中移动一个形状/对象
- 移动一个矩阵
- 沿着二维网格移动一个值
- 为什么编译器需要一个复制构造函数,需要并移动一个并且不使用它们中的任何一个?
- 如何从数组中删除一个元素并将所有元素向上移动一个位置
- 我有一个字符数组,我想把它的二进制位向右移动一个
- 在c++ 11中从std::deque中移动一个元素
- 如何在c++中操纵/移动一个网格中的对象
- 如何在不移动整个场景的情况下移动一个2d对象?