Friday, 13 March 2015

OPENGL BOUNCING BALL GAME

OPENGL BOUNCING BALL GAME : 




source code:


#include <GL/glut.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#define PI 3.1415926535898

GLfloat BXMAX,BXMIN,BYMAX,BYMIN;
GLfloat TBXMAX,TBXMIN;
GLint circle_points=100;
float rd=0.2;
GLfloat x=0.0,y=0.0,xt=0.0,yt=-0.8;
GLdouble l,r,b,t;
GLfloat m = 0.02f;   
GLfloat n= 0.007f;
int p,q;
int flag=0,level=1;
int count=0;
char *d;
char *de;

// fuction to intialz basic window
void initGL(){

       glClearColor(0.0, 0.0, 0.0, 1.0);
       glLoadIdentity();
       glMatrixMode(GL_PROJECTION);  
}

//fuction to print string of characters on the window screen
void print(int x,int y,int z, char *string){
    glRasterPos2f(x,y);
    int i;
    int len=(int)strlen(string);

    for(i=0; i<len;i++){
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,string[i]); 
        //in built func to print the text bit vz
    }
}


/*idle function to show the current picture on the screen while next
one being drawn in processor */
void idle() {
    glutPostRedisplay();
}



// display function
void display() {
       glClear(GL_COLOR_BUFFER_BIT);  
/* in order to draw multiple pics on the screen we use Push matrix to
push each pic on the buffer */
           glPushMatrix();
          glTranslatef(xt,yt,0.0);
              glColor3f(0.0,1.0,0.0);
              glBegin(GL_QUADS);
              glVertex2f(-0.3,0.1);
              glVertex2f(0.3,0.1);
              glVertex2f(0.3,-0.1);
              glVertex2f(-0.3,-0.1);
              glEnd();
// we pop that pic to screen
              glPopMatrix();
  
// we push next pic               
          glPushMatrix();       
          glTranslatef(x, y, 0.00);   
          glBegin(GL_POLYGON);
          int i;
          float angle;
          glColor3f(1.0,0.0,0.0);
// this is to draw a circle on the screen
          for(i=0;i<circle_points;i++)
        {
               angle=2*PI*i/circle_points;
               glVertex2f((cos(angle))*rd,(sin(angle))*rd);
        }
         glEnd();
//pop that circle on the screen
         glPopMatrix();

// push score board to buffer
         glPushMatrix();
    glColor3f(0.0,0.0,1.0);
         glTranslatef(0.6, 0.92, 0.00);
         print(0,0,0,"score=");
//pop that to screen
         glPopMatrix();

// display the score
         glPushMatrix();
    glColor3f(0.0,0.0,1.0);
         glTranslatef(0.98, 0.92, 0.00);
         switch(count){
        case 0: d="00"; break;
        case 1: d="01"; break;
        case 2: d="02"; break;
        case 3: d="03"; break;
        case 4: d="04"; break;
        case 5: d="05"; break;
        case 6: d="06"; break;
        case 7: d="07"; break;
        case 8: d="08"; break;
        case 9: d="09"; break;
        case 10: d="10"; break;
        case 11: d="11"; break;
        case 12: d="12"; break;
        case 13: d="13"; break;
        case 14: d="14";break;
        default: d="00";
        }
       print(0,0,0,d);
       glPopMatrix();

// level display
             glPushMatrix();
    glColor3f(0.0,0.0,1.0);
         glTranslatef(-0.6, 0.92, 0.00);
         print(0,0,0,"level=");
         glPopMatrix();

// value of level
             glPushMatrix();
    glColor3f(0.0,0.0,1.0);
         glTranslatef(-0.3, 0.92, 0.00);
         switch(level){

             case 1: de="01"; break;
             case 2: de="02"; break;
             case 3: de="03"; break;
             case 4: de="04"; break;
             case 5: de="05"; break;
             case 6: de="06"; break;
             case 7: de="07"; break;
             case 8: de="08"; break;
             case 9: de="09"; break;
             case 10: de="10"; break;
             case 11: de="11"; break;
             case 12: de="12"; break;
        default: de="00";

             }
         print(0,0,0,de);
         glPopMatrix();
// next instruction swap the content of display buffer with the next pic to be displayed
glutSwapBuffers();  
   


        if(x > BXMAX){ x=BXMAX; m=-m; }
        else if(x < BXMIN){ x=BXMIN; m=-m; }
        if(y > BYMAX){ y=BYMAX;  n=-n; }
        else if(y < BYMIN){ y=BYMIN; n=-n; }

        float f;
        float a1,b1,a2,b2;
        a1=abs(abs(xt)-abs(x));
        b1==abs(abs(yt)-abs(y));
        a2=pow(a1,2);
        b2=pow(b1,2);
        f=sqrt(a2+b2);

        if(f<=0.424264068&&y<=-0.5&&y>=-0.7){
            n=-1.002*n; //bounce back the ball if it touches the bar
             count++;    // increase the score by 1
        if(count==15) {
            count=0;
            level++; // next level
            m=1.2*m; //incr the speed of ball in x-axis
            n=1.2*n; //incr the speed of ball in y-axis
                              }
        }
        if((y<-0.55&&f<0.5831&&f>0.5)){ x=x; y=y; m=0; n=0; flag=1; // game over
                    }
        else{
        if(y<=-0.6){ x=x; y=y; m=0; n=0; flag=1;  }else {
                      //move the base rect to left
                   p=xt-0.3;
                      //move the base rect to right
                   q=xt+0.3;
             // increase the speed of ball in x axis
               x=x+m;
                      // increase the speed of ball in y axis
               y=y+n;
        }
         }
}
       

void reshape(int width, int height) { 
   if (height == 0) height = 1;            
   GLfloat aspect = (GLfloat)width / (GLfloat)height;

 
   glViewport(0, 0, width, height);


   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (width >= height) {

      l=-1.0*aspect;
    r=1.0*aspect;
    b=-1.0;
    t=    1.0;

   } else {

    l=-1.0;
    r=1.0;
    b=-1.0/aspect;
    t=    1.0/aspect;
   }
gluOrtho2D(l,r,b,t);
BXMAX=r-(rd);
BXMIN=l+(rd);
BYMAX=t-(rd)-0.1;
BYMIN=b+(rd); 
TBXMAX=r-(0.3);
TBXMIN=l+(0.3);

}

void specialKeys(int key, int x, int y){

switch(key) {
 case GLUT_KEY_LEFT: if(flag==0){if(xt<=TBXMIN)
                     {xt=TBXMIN;}
                     else
                     { xt=xt-0.1; }}
                     break;
 case GLUT_KEY_RIGHT: if(flag==0){if(xt>=TBXMAX)
                     {xt=TBXMAX;}
                     else
                     { xt=xt+0.1;}}
                     break;

}


}

int main(int argc, char** argv) {
   glutInit(&argc, argv);         
   glutInitDisplayMode(GLUT_DOUBLE); 
   glutInitWindowSize(640, 480);  
   glutInitWindowPosition(50, 50);
   glutCreateWindow("ball");
   glutReshapeFunc(reshape); 
   glutSpecialFunc(specialKeys);
   glutDisplayFunc(display);       
   glutIdleFunc(idle);            
   initGL();                    
   glutMainLoop();                
   return 0;
}

No comments:

Post a Comment

Total Pageviews