Note that there are some explanatory texts on larger screens.

plurals
  1. POMove a ball in bowling game once a key is pressed
    text
    copied!<p>I have been working with bowling game in C++.</p> <p>I want only one thing that I want to move the ball only once a key is pressed and the bowl moves smoothly(not like now as it moves by pressing and holding down UP-KEY).</p> <p>Here is the code: -</p> <pre><code>#include &lt;GL/glut.h&gt; #include &lt;cmath&gt; 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 &lt;= 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 &gt;= firstx1 &amp;&amp; posX &lt;= firstx3) &amp;&amp; (posY == firsty1 &amp;&amp; posY == firsty2 &amp;&amp; posY == firsty3)) { firstx1 += 0.02; firsty1 += 0.03; firstx2 += -0.06; firsty2 += 0.02; firstx3 += 0.03; firsty3 += 0.05; } if ((posX &lt;= secondx1 &amp;&amp; posX &gt;= secondx3) &amp;&amp; (posY == secondy1 &amp;&amp; posY == secondy2 &amp;&amp; posY == secondy3)) { secondx1 += 0.02; secondy1 += 0.02; secondx2 += -0.06; secondy2 += 0.02; secondx3 += 0.03; secondy3 += 0.05; } if ((posX &gt;= thirdx1 &amp;&amp; posX &lt;= thirdx3) &amp;&amp; (posY == thirdy1 &amp;&amp; posY == thirdy2 &amp;&amp; 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(&amp;argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 300); glutInitWindowPosition(150,250); glutCreateWindow("Balling Game"); glutDisplayFunc(display); glutSpecialFunc(keyboardown); glutMainLoop(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload