Click here to Skip to main content
15,867,968 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
// C program to illustrate OpenGL game

#include<stdio.h>
#include<gl glut.h="">
#include<math.h>
#define pi 3.142857

int c = 0, d = 0, left = 0, right = 0;
int m = 0, j = 1, flag1 = 0, l = 1, flag2 = 0, n = 0, score = 0, count = 1;

void myInit (void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glColor3f(1.0f, 0.0f, 0.0f);
    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-620.0, 620.0, -340.0, 340.0);
}

void keyboard(unsigned char key, int x, int y)
{
    left = -200 + 200 * (d - c);//left is the point the rect is drawn in left side
    right = 200 + 200 * (d - c);//right is the point the rectangle is drown from right

	//if the rectangle is in the middle so the right is = 200 and left = -200
//else if u keep on pressing the b the c will increment and the left equation will be left = -200+200*(0-1) that the left side -400 till it reaches max -600
//and same for right

	if (left == -600)//indicats the part between rectangle and the leftmost side
    {
        if (key == 110)//n  moves right 
            d++;//if the left reaches -600 so the d increment only letting u move right  
    }
    else if (right == 600)//indicates part between rectangle and right most.
    {
        if (key == 98)//b moves left
            c++;//if right reaches 600 so the c increments letting u only move left
//
    }
    else
    {
        if (key == 98)//u could press b 
            c++;    
        if (key == 110)//u could press n
            d++;
    }
    glutPostRedisplay();
}

void myDisplay(void)
{
    int x, y, k;
    for (k = 0; k <= 400; k += 5)//makes ball moves ,just to make ball recreating in display motion
    {    
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_LINE_STRIP);
        float i = 0;
        m = m + 6;//indicates motion of ball virtical ,move 6 points virtical in each cycle
        n = n + 6;//indicates motion of ball horizonta,move 4 points horizontally in each cycle
        while (i <= 2 * pi)//draws the ball cincomfrunce
        {
            y = 12 + 20 * cos(i);//center of ball equation along y axis //y start from 12 and radius 20 
            x = 20 * sin(i);//center of ball equation alon x axis//i is the angle //x start from 0 and radius 20
            i = i + 0.1;//increment i with 0.1 till it reaches 360,0.1 till it draws the hole ball
            if (m == 288 && flag1 == 0)//vertical,if the ball reaches the top increment the score ,
            {                          // flag1 0 means incremnt movment up with equation (y-j*m) ,so if it reaches the top switch flag0 with flag 1
                 j = -1;
                m = -288;
                flag1 = 1;
                score++;
            }
            if (m == 288 && flag1 == 1) //if flag = 1 means  start moving1 down
            {
                j = 1;
                m = -288;
                flag1 = 0;
            }
            if (n == 580 && flag2 == 0)//horizontal
            {                          // if flag2==0 means increment ball movemnt horizontally 
                                       // if it reaches the edge switch flags 
                l = -1;
                n = -580;
                flag2 = 1;
            }
            if (n == 580 && flag2 == 1)//if flag 2 =1 do not move horizontally 
                                       //if and start moving down  then switch flags
            {
                l = 1;
                n = -580;
                flag2 = 0;
            }
            // equation for desired motion of ball
            glVertex2i((x - l * n), (y - j * m));
        }
        glEnd();

        glBegin(GL_LINE_LOOP);//frame off the window
            glVertex2i(-600, -320);
            glVertex2i(-600, 320);
            glVertex2i(600, 320);
            glVertex2i(600, -320);
        glEnd();

        glBegin(GL_LINE_LOOP); //shape of rectangle
        left = -200 + 200 * (d - c);
        right = 200 + 200 * (d - c);
            glVertex2i(left, -315);
            glVertex2i(left, -295);
            glVertex2i(right, -295);
            glVertex2i(right, -315);
        glEnd();
        if ((j * m) == 276) //if (j*m)=288-radius(12 is the radius of circle on y axis if it is in the buttom right part
        {            if ((left > ((-1 * l * n) + 20)) || (right < (-1 * l * n) - 20))//if left point of the rectangle say is in the - part of x asix so the equation will be (left>(-1*1*-588)+20=608 so the left pont will not include the x axis + radius of ball so the ball falls and the game ends)
            {
                printf("Game Over !!!\nYour Score is :\score\n", score);
                exit(0);
            }
        }
        glutSwapBuffers();
    }
}

int main (int argc, char** argv)
{
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(1100, 600);
    glutInitWindowPosition(0, 0); 
    glutCreateWindow("Game");   
    glutKeyboardFunc(keyboard);    
    myInit();
    glutDisplayFunc(myDisplay);
    glutMainLoop();
}


What I have tried:

It's a simple project but I can't understand the could ,so if someone could help.
Posted
Updated 9-Jun-21 8:03am
v3
Comments
Rageh11111 6-Jun-21 14:06pm    
Do not read the comments in code it's mostly wrong.

1 solution

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

If you don't understand code, then don't play with it: learn the language and framework on which it works, and write your own. "Copy'n'paste'n'hope" is not a viable design strategy.
 
Share this answer
 
Comments
Rageh11111 9-Jun-21 14:04pm    
So don't jump to your conclusion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900