How do I make a circle jump up and down when I press into space?

Asked

Viewed 81 times

0

I need to make a version of the game "Color Switch" in Visual Studio C++ with Glut but I don’t know how to make a geometric figure move.

Any help is welcome.

Below is the code of my circle

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

#ifndef M_PI
#define M_PI 3.1415926

#endif
#define CIRCLE_STEPS 50

void draw_face(int size)
{
    GLint i;
    glLineWidth(size);
    glColor3f(0, 0, 1);
    glBegin(GL_LINE_LOOP);
    glVertex2f(1.0, 0.0);

for (i = 1; i < CIRCLE_STEPS; i++)
    {
        GLfloat t = i * M_PI * 2 / CIRCLE_STEPS;
        glVertex2f(cos(t), sin(t));
    }
    glEnd();
}

void display(void) {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 0);
    gluOrtho2D(-4, 4, -4, 4);
    glBegin(GL_POLYGON);
    glVertex2f(-0.5, -0.5);
    glVertex2f(-0.5, 0.5);
    glVertex2f(0.5, 0.5);
    glEnd();
    draw_face(3);
    glFlush();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("simple");
    glutDisplayFunc(display);
    glutMainLoop();
}

1 answer

0

GLUT has glutKeyboardFunc() function to detect keyboard events.

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

#ifndef M_PI
#define M_PI 3.1415926

#endif
#define CIRCLE_STEPS 50

float jump_y = 0;
int jump_state = 0;
bool jump = false;

void draw_face(int size)
{
    GLint i;
    glLineWidth(size);
    glColor3f(0, 0, 1);
    glBegin(GL_LINE_LOOP);
    glVertex2f(1.0, 0.0 + jump_y);

    for (i = 1; i < CIRCLE_STEPS; i++)
    {
        GLfloat t = i * M_PI * 2 / CIRCLE_STEPS;
        glVertex2f(cos(t), sin(t) + jump_y);
    }
    glEnd();
}

void display(void) {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-4, 4, -4, 4);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor3f(1, 0, 0);

    glBegin(GL_POLYGON);
    glVertex2f(-0.5, -0.5 + jump_y);
    glVertex2f(-0.5, 0.5 + jump_y);
    glVertex2f(0.5, 0.5 + jump_y);
    glEnd();
    draw_face(3);

    if (jump == true) {
        if (jump_state == 0) {
            jump_y += 0.01;
        }
        else {
            jump_y -= 0.01;
        }
        if (jump_y > 3) {
            jump_state = 1;
        }
        if (jump_state == 1 && jump_y <= 0) {
            jump = false;
            jump_state = 0;
        }
    }

    glFinish();
    glutPostRedisplay();
}

void keyboard_down(unsigned char key, int x, int y) {
    if (key == ' ' && jump == false) {
        jump = true;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("simple");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard_down);
    glutMainLoop();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.