# Thread: How to add encapsulation, abstraction, polymorphism, inheritance in my pong game?

1. ## How to add encapsulation, abstraction, polymorphism, inheritance in my pong game?

Hello. We were required to apply encapsulation, abstraction, polymorphism and inheritance in our pong game (OpenGL). But our professor did NOT teach us at all on as to how we can apply those concepts in OpenGL. Can someone please help me? It's 60% of our final grade. :/

Code cpp:
```#include <vector>
#include <time.h>
#include "Glut_Setup.h"

using namespace std;

float const origin = 0.0, pointy = 10.0;

float triP1x = 2.0, triP1y = 1.0, triP1z = 0.0,
triP2x = 6.0, triP2y = 1.0, triP2z = 0.0,
triP3x = 4.0, triP3y = 3.0, triP3z = 0.0;

float ballRadius = 0.5, ballX = 0.5, ballY = 0.5, ballZ = 0.0;

float wall1P1x = 14, wall1P1y = 15, wall1P1z = 0.0,
wall1P2x = 14.5, wall1P2y = 15, wall1P2z = 0.0,
wall1P3x = 14.5, wall1P3y = -15, wall1P3z = 0.0,
wall1P4x = 14, wall1P4y = -15, wall1P4z = 0.0;

float wall2P1x = -14, wall2P1y = 15, wall2P1z = 0.0,
wall2P2x = -14.5, wall2P2y = 15, wall2P2z = 0.0,
wall2P3x = -14.5, wall2P3y = -15, wall2P3z = 0.0,
wall2P4x = -14, wall2P4y = -15, wall2P4z = 0.0;

float wall3P1x=-4, wall3P1y = -11.5, wall3P1z = 0.0,
wall3P2x = 4, wall3P2y = -11.5, wall3P2z = 0.0,
wall3P3x = 4, wall3P3y = -11, wall3P3z = 0.0,
wall3P4x = -4, wall3P4y = -11, wall3P4z = 0.0;

float wall4P1x= -4, wall4P1y = 11.5, wall4P1z = 0.0,
wall4P2x = 4, wall4P2y = 11.5, wall4P2z = 0.0,
wall4P3x = 4, wall4P3y = 11, wall4P3z = 0.0,
wall4P4x = -4, wall4P4y = 11, wall4P4z = 0.0;

float wall21P1x= -12, wall21P1y = 15, wall21P1z = 0.0,
wall21P2x = -9, wall21P2y = 15, wall21P2z = 0.0,
wall21P3x = -9, wall21P3y = -15, wall21P3z = 0.0,
wall21P4x = -12, wall21P4y = -15, wall21P4z = 0.0;

float wall22P1x= -1.5, wall22P1y = 15, wall22P1z = 0.0,
wall22P2x = 1.5, wall22P2y = 15, wall22P2z = 0.0,
wall22P3x = 1.5, wall22P3y = -15, wall22P3z = 0.0,
wall22P4x = -1.5, wall22P4y = -15, wall22P4z = 0.0;

float wall23P1x= 9, wall23P1y = 15, wall23P1z = 0.0,
wall23P2x = 12, wall23P2y = 15, wall23P2z = 0.0,
wall23P3x = 12, wall23P3y = -15, wall23P3z = 0.0,
wall23P4x = 9, wall23P4y = -15, wall23P4z = 0.0;

float wall25P1x= -15, wall25P1y = 1.5, wall25P1z = 0.0,
wall25P2x = 15, wall25P2y = 1.5, wall25P2z = 0.0,
wall25P3x = 15, wall25P3y = -1.5, wall25P3z = 0.0,
wall25P4x = -15, wall25P4y = -1.5, wall25P4z = 0.0;

float wall30P1x= -8, wall30P1y = 18, wall30P1z = 0.0,
wall30P2x = -3, wall30P2y = 18, wall30P2z = 0.0,
wall30P3x = -3, wall30P3y = 3, wall30P3z = 0.0,
wall30P4x = -8, wall30P4y = 3, wall30P4z = 0.0;

float wall31P1x= 8, wall31P1y = 18, wall31P1z = 0.0,
wall31P2x = 3, wall31P2y = 18, wall31P2z = 0.0,
wall31P3x = 3, wall31P3y = 3, wall31P3z = 0.0,
wall31P4x = 8, wall31P4y = 3, wall31P4z = 0.0;

float wall10P1x = 20, wall10P1y = 20, wall10P1z = 0.0,
wall10P2x = -20, wall10P2y = 20, wall10P2z = 0.0,
wall10P3x = -20, wall10P3y = -20, wall10P3z = 0.0,
wall10P4x = 20, wall10P4y = -20, wall10P4z = 0.0;

float wall32P1x= 8, wall32P1y = -18, wall32P1z = 0.0,
wall32P2x = 3, wall32P2y = -18, wall32P2z = 0.0,
wall32P3x = 3, wall32P3y = -3, wall32P3z = 0.0,
wall32P4x = 8, wall32P4y = -3, wall32P4z = 0.0;

float wall33P1x= -8, wall33P1y = -18, wall33P1z = 0.0,
wall33P2x = -3, wall33P2y = -18, wall33P2z = 0.0,
wall33P3x = -3, wall33P3y = -3, wall33P3z = 0.0,
wall33P4x = -8, wall33P4y = -3, wall33P4z = 0.0;

float WallLife= 3;
float wallcx= 1.0, wallcy=0.0, wallcz=0.0;

float ballULx,
ballULy,
ballLRx,
ballLRy;

float moveX = 0.1, moveY=0.1;

void GameScene()
{
//_____________________________
// para gumalaw ang ball :D
ballX =ballX+moveX;
ballY =ballY+moveY;
////Diagonally
//_____________________________
//raidus ng bounding box (0.5/-0.5) para sakto sa ball

if (ballLRx>=wall1P1x)
moveX*=-1;

if (ballULx<=wall2P2x)
moveX*=-1;

//if (ballULy>=wall4P3y)
//    moveY*=-1;

//if (ballLRy<=wall3P1y)
//    moveY*=-1;
if ((ballLRx>=wall3P1x) && (ballLRy<=wall3P1y)&& (ballULx<=wall3P2x))
moveY*=-1;

if ((ballULy >= wall4P3y) && (ballLRx >=wall4P4x) && (ballULx <= wall4P3x))
moveY*=-1;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLineWidth(1.0);
glBegin(GL_LINES);

//glBegin(GL_LINE_LOOP);
//glColor3f(0.0, 0.0, 1.0);
//glVertex3f(triP1x, triP1y, triP1z);
//glVertex3f(triP2x, triP2y, triP2z);
//glVertex3f(triP3x, triP3y, triP3z);
//glEnd();

glBegin(GL_LINE_LOOP);
glColor3f(0.1,0.1,0.1);

glVertex3f(ballULx, ballULy, ballZ);
glVertex3f(ballLRx, ballULy, ballZ);
glVertex3f(ballLRx, ballLRy, ballZ);
glVertex3f(ballULx, ballLRy, ballZ);
glEnd();

glColor3f(0.3f, 0.7f, 0.7f);
glVertex3f(wall1P1x, wall1P1y, wall1P1z);
glVertex3f(wall1P2x, wall1P2y, wall1P2z);
glVertex3f(wall1P3x, wall1P3y, wall1P3z);
glVertex3f(wall1P4x, wall1P4y, wall1P4z);

glColor3f(0.3f, 0.7f, 0.7f);
glVertex3f(wall2P1x, wall2P1y, wall2P1z);
glVertex3f(wall2P2x, wall2P2y, wall2P2z);
glVertex3f(wall2P3x, wall2P3y, wall2P3z);
glVertex3f(wall2P4x, wall2P4y, wall2P4z);

glColor3f(0.6f, 0.9f, 0.9f);
glVertex3f(wall3P1x, wall3P1y, wall3P1z);
glVertex3f(wall3P2x, wall3P2y, wall3P2z);
glVertex3f(wall3P3x, wall3P3y, wall3P3z);
glVertex3f(wall3P4x, wall3P4y, wall3P4z);

glColor3f(0.6f, 0.9f, 0.9f);
glVertex3f(wall4P1x, wall4P1y, wall4P1z);
glVertex3f(wall4P2x, wall4P2y, wall4P2z);
glVertex3f(wall4P3x, wall4P3y, wall4P3z);
glVertex3f(wall4P4x, wall4P4y, wall4P4z);

glColor3f(1.0, 1.0, 1.0);
glVertex3f(wall21P1x, wall21P1y, wall21P1z);
glVertex3f(wall21P2x, wall21P2y, wall21P2z);
glVertex3f(wall21P3x, wall21P3y, wall21P3z);
glVertex3f(wall21P4x, wall21P4y, wall21P4z);

glColor3f(1.0, 1.0, 1.0);
glVertex3f(wall22P1x, wall22P1y, wall22P1z);
glVertex3f(wall22P2x, wall22P2y, wall22P2z);
glVertex3f(wall22P3x, wall22P3y, wall22P3z);
glVertex3f(wall22P4x, wall22P4y, wall22P4z);

glColor3f(1.0, 1.0, 1.0);
glVertex3f(wall23P1x, wall23P1y, wall23P1z);
glVertex3f(wall23P2x, wall23P2y, wall23P2z);
glVertex3f(wall23P3x, wall23P3y, wall23P3z);
glVertex3f(wall23P4x, wall23P4y, wall23P4z);

glColor3f(1.0, 1.0, 1.0);
glVertex3f(wall25P1x, wall25P1y, wall25P1z);
glVertex3f(wall25P2x, wall25P2y, wall25P2z);
glVertex3f(wall25P3x, wall25P3y, wall25P3z);
glVertex3f(wall25P4x, wall25P4y, wall25P4z);

glColor3f(0.3f, 0.7f, 0.7f);
glVertex3f(wall30P1x, wall30P1y, wall30P1z);
glVertex3f(wall30P2x, wall30P2y, wall30P2z);
glVertex3f(wall30P3x, wall30P3y, wall30P3z);
glVertex3f(wall30P4x, wall30P4y, wall30P4z);

glColor3f(0.3f, 0.7f, 0.7f);
glVertex3f(wall31P1x, wall31P1y, wall31P1z);
glVertex3f(wall31P2x, wall31P2y, wall31P2z);
glVertex3f(wall31P3x, wall31P3y, wall31P3z);
glVertex3f(wall31P4x, wall31P4y, wall31P4z);

glColor3f(0.3f, 0.7f, 0.7f);
glVertex3f(wall32P1x, wall32P1y, wall32P1z);
glVertex3f(wall32P2x, wall32P2y, wall32P2z);
glVertex3f(wall32P3x, wall32P3y, wall32P3z);
glVertex3f(wall32P4x, wall32P4y, wall32P4z);

glColor3f(0.3f, 0.7f, 0.7f);
glVertex3f(wall33P1x, wall33P1y, wall33P1z);
glVertex3f(wall33P2x, wall33P2y, wall33P2z);
glVertex3f(wall33P3x, wall33P3y, wall33P3z);
glVertex3f(wall33P4x, wall33P4y, wall33P4z);

/* 13.5, wall1P1y = 2.5, wall1P1z = 0.0,
wall1P2x = 15, wall1P2y = 2.5, wall1P2z = 0.0,
wall1P3x = 15, wall1P3y = -2.5, wall1P3z = 0.0,
wall1P4x = 13.5, wall1P4y = -2.5, wall1P4z = 0.0;*/

glColor3f(0.15f, 0.5f, 0.5f);
glVertex3f(wall10P1x, wall10P1y, wall10P1z);
glVertex3f(wall10P2x, wall10P2y, wall10P2z);
glVertex3f(wall10P3x, wall10P3y, wall10P3z);
glVertex3f(wall10P4x, wall10P4y, wall10P4z);

glEnd();

glPushMatrix();
glTranslatef(ballX, ballY, ballZ);
glColor3f(0.9f, 0.9f, 0.9f);
glPopMatrix();

glutSwapBuffers();
}

void Keys(unsigned char key, int x, int y)
{
switch(key)
{
case 27 : exit(0); break;
//______________________________________
// these are for the controls (up and down)
case 'q':
if (wall4P1x<=wall2P2x)
{
wall4P1x= wall4P1x+0;
}

else
{
wall4P1x= wall4P1x-1;
wall4P2x= wall4P2x-1;
wall4P3x= wall4P3x-1;
wall4P4x= wall4P4x-1;

}
break;

case 'e':
if (wall4P2x>=wall1P1x)
{
wall4P2x= wall4P2x+0;
}

else
{
wall4P1x= wall4P1x+1;
wall4P2x= wall4P2x+1;
wall4P3x= wall4P3x+1;
wall4P4x= wall4P4x+1;
}

break;

case 'a':
if (wall3P1x <= wall2P1x)
{
wall3P1x=wall3P1x+0;
}

else
{

wall3P1x= wall3P1x-1;
wall3P2x= wall3P2x-1;
wall3P3x= wall3P3x-1;
wall3P4x= wall3P4x-1;
}
break;

case 'd': if (wall3P2x >= wall1P1x)
{
wall3P2x=wall3P2x+0;
}

else
{
wall3P1x= wall3P1x+1;
wall3P2x= wall3P2x+1;
wall3P3x= wall3P3x+1;
wall3P4x= wall3P4x+1;
}

break;

}
}

void SpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_LEFT: triP1x = triP1x - 0.5;
triP2x = triP2x - 0.5;
triP3x = triP3x - 0.5; break;
case GLUT_KEY_RIGHT: if (triP2x < wall1P1x)
{
triP1x = triP1x + 0.5;
triP2x = triP2x + 0.5;
triP3x = triP3x + 0.5;
}
break;
case GLUT_KEY_UP: triP1y = triP1y + 0.5;
triP2y = triP2y + 0.5;
triP3y = triP3y + 0.5; break;
case GLUT_KEY_DOWN: triP1y = triP1y - 0.5;
triP2y = triP2y - 0.5;
triP3y = triP3y - 0.5; break;

}
}

void Timer(int value)
{
glutTimerFunc(50, Timer, value);
glutPostRedisplay();
}

void changeSize(int w, int h)
{
float ratio = 1.0 * w / h;
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, w, h);
gluPerspective(45, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void main(int argc, char **argv)
{
srand(time(NULL));
GLUTInit(&argc, argv);
}```

2. See question #6 in the Forum Posting Guidelines.

If you've got a "specific" question about OpenGL and how it works, we might be able to help you. But we won't do school homework/projects for you.

Best bet is to ask your prof for more detail on what he wants you to do "and why" (i.e. why is it useful, or what does he want you to learn from this)! Recommendation: Read up on what each of these terms mean (book, websearch, etc.) first, then look back at the program, and then talk to the prof. You should be able to figure out how to apply at least a few of these yourself without any help, assuming you've taken a C++/OO programming course.

3. These concepts are nothing to do with OpenGL; OpenGL is just an API, so you'd add them in the same way as you'd add them to any other program using any other API (or better yet, build your program using them from the outset - retrofitting can be difficult and messy). The fact that you're using OpenGL doesn't make things in any way special or different.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•