BREAKOUT GAME IN OPENGL code is in C.

Use the 's' and 'd' to move the paddle. Bounce the ball off the paddle to keep it from falling off the bottom of the screen. Break all the bricks to win.

So far the program is running. but the right brick does not disappears when hit by the ball? can u check out my code and tell me whats wrong? what should I do?

and also the angles when the ball bounces are not right? what do u thinks causes? what should I do?

#include <GL/glut.h>
#include <stdlib.h>      // LIBRARIES
#include <stdio.h>
#include <math.h>

#define BALL_SIDE 12        //SIDE PIXES PARA D MAG LAPAW
#define BALL_SPEED 0.8      // BALL SPEED
#define PLAYER_SIDE 40      //PLAYER SIDE
#define PLAYER_SPEED 12     //PLAYER SPEED

#define ANGLE 45;       //ANGLE

int angles[2] = {45,-45};   // ANGLE

#define RED 0           //COLORS
#define GREEN 1
#define BLUE 2
#define YELLOW 3
#define BLACK 4
#define WHITE 5
#define GRAY 6
#define PINK 7
#define PURPLE 8
#define DARKGREEN 9
GLfloat colors[10][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, // Red, Green
                        {0.0, 0.0, 1.0}, {1.0, 1.0, 0.0}, // Blue, Yellow
                        {0.0, 0.0, 0.0}, {1.0, 1.0, 1.0}, // Black, White 
                        {0.5, 0.5, 0.5}, {1.0, 0.0, 1.0}, // Gray, Pink
                        {0.29, 0.0, 0.5}, {0.0, 0.39, 0.0} };


GLfloat pixel_color[3];     //for pixel comparison

typedef struct {        // STRUCTURE FOR COORDINATES
    double x;
    double y;
} coords;           // coords datatype

int width, height ;     // WIDTH & HEIGHT
int cw, ch;         //current


coords ball;            // we have 1 ball having x and y values
coords ball_direction;      // ball x and y directions

coords player;          //PLAYER
coords player_direction;    //PLAYER only X direction will be used
int player_moving;      //?

coords brick;           //bricks

int numlines = 4;
int bricksperline = 10;
int brickspace = 50;

int showbrick[40];          // show brick 1 - true , 0 - false

/*---------------------------------------------------------------------------------*/

/*START OF BRICKS*/

void DrawBricks(){

int i,j;
int x1,y1,x2,y2;
    for (j=0; j<numlines; j++)
        { 
           for (i=0; i<bricksperline; i++)
                {

                if (showbrick[j*bricksperline+i]){
                x1 = brick.x;
                y1 = brick.y;
                x2 = x1 + PLAYER_SIDE;
                y2 = y1 + PLAYER_SIDE;
                glColor3fv(colors[BLACK]);
                //glRectf(x1 + brickspace*i,y1,x2 + brickspace*i ,y2);              
                glRectf(x1 + brickspace*i,y1 - brickspace*j,x2 + brickspace*i,y2-brickspace*j);
                }

            }
        }

}

void InitBricks()
{
    int i;
    for (i=0; i<numlines*bricksperline; i++)
      showbrick[i]=1;   // 1 as true
}


void CheckBricks()
{
    int i,j,x,y;
    for (j=0; j<numlines; j++)
    {
      for (i=0; i<bricksperline; i++)
      {
        if (showbrick[j*bricksperline+i])
        {
          y= 5 + j*(brickspace+PLAYER_SIDE);
          x= 5 +i*(brickspace+PLAYER_SIDE);
          if (ball.y>=(y-BALL_SIDE) && ball.y<(y+PLAYER_SIDE) &&
              ball.x>=(x-BALL_SIDE) && ball.x<(x+PLAYER_SIDE))
          {
            showbrick[j*bricksperline+i]= 0;
          }
        }
      }
    }
}


/*END OF BRICKS*/


/* START OF BALL*/
int reverse(int i){
    return i * -1;
}

double radian(double deg){
    return deg * (3.14159265/ 180);
}

void bounce_x(){

    int angle = angles[rand() % 2];

  //  int angle = ANGLE;
    float x = sqrt(2) * sin(radian(90 - angle));
    float y = sqrt(2) * sin(radian(angle));
    if(ball_direction.x > 0){
        x *= -1;
    }
    /*if(ball_direction.y > 0){
        y *= -1;
    }*/
    ball_direction.x = x;
    ball_direction.y = y;
}
void bounce_y(){


    int angle = angles[rand() % 2];

    //int angle = ANGLE;
    float x = sqrt(2) * sin(radian(90 - angle));
    float y = sqrt(2) * sin(radian(angle));
    /*if(ball_direction.x > 0){
        x *= -1;
    }*/
    if(ball_direction.y > 0){
        y *= -1;
    }
    ball_direction.x = x;
    ball_direction.y = y;
}

void bounce(){ //BOUNCES BALL BACK?

    int angle = angles[rand() % 2];

   // int angle = ANGLE;
    float x = sqrt(2) * sin(radian(90 - angle));
    float y = sqrt(2) * sin(radian(angle));
    if(ball_direction.x > 0){
        x *= -1;
    }
    if(ball_direction.y > 0){
        y *= -1;
    }
    ball_direction.x = x;
    ball_direction.y = y;
}

void updateBallPosition(){
    coords bd = ball_direction;
    ball.x += BALL_SPEED * bd.x;
    ball.y += BALL_SPEED * bd.y;
    if(ball.x < 0){
        ball.x = 0;
        //ball_direction.x = reverse(bd.x);
        bounce_x();
    }
    if(ball.x + BALL_SIDE > width){
        ball.x = width -  BALL_SIDE;
        //ball_direction.x = reverse(bd.x);
        bounce_x();
    }
    if(ball.y < 0){
        ball.y = 0;
        //ball_direction.y = reverse(bd.y);
        bounce_y();
    }
    if(ball.y + BALL_SIDE > height){
        ball.y = height - BALL_SIDE;
        //ball_direction.y = reverse(bd.y);
        bounce_y();
    }
    glReadPixels(ball.x-1,ball.y-1,1,1,GL_RGB,GL_FLOAT,pixel_color);
    //if(pixel_color[0] != colors[WHITE][0] && pixel_color[1] != colors[WHITE][1] && pixel_color[2] != colors[WHITE][2])        //{

    if(pixel_color[0] != colors[WHITE][0]){
    if(pixel_color != colors[WHITE]){
        bounce();
        //showbrick[j*bricksperline+i]= 0;
        //int s = ball.x;       
        //showbrick[s] = 0;
        CheckBricks();
        glutPostRedisplay();
    }}

}

void displayBall(){
    int i;
    int x1,y1,x2,y2;
        x1 = ball.x;    //struct
        y1 = ball.y;
        x2 = x1 + BALL_SIDE;
        y2 = y1 + BALL_SIDE;
        glColor3fv(colors[RED]);
        //glColor3f(0.0,0.0,0.0);
        glRectf(x1,y1,x2,y2);
}
/* END OF BALL*/


/*START OF PLAYER*/
void movePlayer(){

    coords pd = player_direction;
    player.x += BALL_SPEED * pd.x; //used to be PLAYER_SPEED, but too fast

    if(player.x <= 0){
        player.x = 0;
        player_moving = 0;
    }
    if(player.x + PLAYER_SIDE >= width){
        player.x = width - PLAYER_SIDE;
        player_moving = 0;
    }

    if(player_moving == 0){
    player_direction.x = 0;     //para kun ga stack up sa gilid pwede pa ka balik
    }
}

void displayPlayer(){
    int x1,y1,x2,y2;
    x1 = player.x;
    y1 = player.y;
    x2 = x1 + PLAYER_SIDE;
    y2 = y1 + BALL_SIDE;
    glColor3fv(colors[BLACK]);
    glRectf(x1,y1,x2,y2);
}
/*END OF PLAYER*/



void myDisplay(){   //DISPLAY FUNC
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);

    glRectf(0,0,width,height);

    displayBall();//display ball

    displayPlayer(); //display Player

    DrawBricks();

    glFlush();
}

void myReshape(int w,int h){    // RESHAPE FUNC
    cw = w;
    ch = h;
    glViewport((cw-width)/2,(ch-height)/2,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,width,0,height);
    glMatrixMode(GL_MODELVIEW);
}

void myIdle(){  //IDLE

    updateBallPosition();

    /*if(player_moving){
        movePlayer();
    }*/

    if(player_moving){
        movePlayer();
    }

    glutPostRedisplay();
}

void myKeyboard(unsigned char key, int x, int y){   //KEYBOARD
    coords move_direction;
    int change_direction = 1;

    if(key == 'a' || key == 'A'){
        //left
        player.x += PLAYER_SPEED * -1;
        move_direction.x = -1;
        if(player_direction.x == 1){
                change_direction = 1;
            }
    }
    else if(key == 'd' || key == 'D'){
        //right
        player.x += PLAYER_SPEED * 1;
        move_direction.x = 1;
            if(player_direction.x == -1){
                change_direction = 1;
            }
    }

    if((player.x != 0 )){
        player_moving = 1;  

    if((player.x != width - PLAYER_SIDE)){
        player_moving = 1;  
    }

    if(change_direction == 1){
                player_direction = move_direction;
            }
    }
    glutPostRedisplay();
}

void init(){    // INITIALIZE

    glClearColor(0.0,0.0,0.0,1.0);



    //player.x = (width/2)-(BALL_SIDE/2);
    player.x = 250;
    player.y = 0;

    player_direction.x = 0;
    player_direction.y = 0;
    player_moving = 0;


    ball.x = 250;   //set ball to center    // not yet so much centered
    ball.y = 30;

    ball_direction.x = 1;
    ball_direction.y = -1;

    InitBricks();       //INITIALIZE BRICKS SHOW
    brick.x = 5 ;
    brick.y = 450 ;

}

int main(int argc, char** argv){    //MAIN
    //srand(time(0));
    glutInit(&argc,argv);
    width = 500;
    height = 500;
    glutInitWindowSize(width,height);
    glutInitWindowPosition(100,100);
    glutCreateWindow("BRicK-OUT");
    glutDisplayFunc(myDisplay);
    glutReshapeFunc(myReshape);
    glutKeyboardFunc(myKeyboard);
    glutIdleFunc(myIdle);
    init();//initBricks();  

    glutMainLoop();
}

Perhaps you are using the angle of 45 you have defined, instead of calculating the real angle on the bounce off the bricks, or the wall.

If you Google for that equation, you'll find it easily. I don't have it on this system, but it's simple geometry.

Changing angle to 50 it moves upward bt it doesn't come back to ground level.what should I do?Give me solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.