Hi codecallers,
This is the third and last part of the c++/opengl tutorials series, this one is different because we will use all the stuff we learned in the previous two parts to create a simple 2d game
The game will be like this:
As you can see it will be really simple and fun
First we start by drawing our objects in the “DrawGLScene(GLvoid)” , instead of putting the code directly into the function we will write the code of the drawing in separate functions because we can use these objects more than once,
Now lets draw every object in the output above:
1.The borders of the window:
We have two borders wit different location that’s why we use functions instead of writing the same code and changing only one variable, the function that draws the borders will be like this:
Nothing new in the code, we only put the code in a separate function that takes three arguments with the locationCode:void border(float posx,float posy,float posz) { glLoadIdentity(); glTranslatef(posx,posy,posz); glBegin(GL_QUADS); glColor3f(0.0f,0.0f,1.0f); glVertex3f( 0.5f, 20.0f, 0.0f); //up right corner glVertex3f( -1.0f, 20.0f, 0.0f); //up left corner glVertex3f(-1.0f,-20.0f, 0.0f); //down left glVertex3f( 0.5f,-20.0f, 0.0f);//down right glEnd(); }
2.Player and pc :
These two object are the same but with different locations that’s why we use the same function for them both:
Code:void playerno1(float posx,float posy,float posz) { glLoadIdentity(); glTranslatef(posx,posy,posz); glBegin(GL_QUADS); glColor3f(0.0f,1.0f,1.0f); //red glVertex3f( 6.0f, 2.0f, 0.0f); //up right corner glColor3f(1.0f,1.0f,0.0f); //red glVertex3f( -6.0f, 2.0f, 0.0f); //up left corner glColor3f(1.0f,0.0f,1.0f); //red glVertex3f(-6.0f,0.0f, 0.0f); //down left glColor3f(0.0f,1.0f,0.0f); //red glVertex3f( 6.0f,0.0f, 0.0f);//down right glEnd(); }
3.The ball:
I bet you thought that we used a new shaped called circle or something, but no we used a regular quad but wih this texture:Code:void ball(float posx,float posy,float posz) { glLoadIdentity(); glTranslatef(posx,posy,posz); glColor3f(1.0f,1.0f,1.0f); glBegin(GL_QUADS); glBindTexture(GL_TEXTURE_2D, texture[1]); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); //up right corner glTexCoord2f(1.0f, 1.0f); glVertex3f( -1.0f, 1.0f, 0.0f); //up left corner glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); //down left glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.f,-1.0f, 0.0f);//down right glEnd(); }
When we put these stuff I the “DrawGLScene(GLvoid)” it will look like this:
If you tried this code you will notice that the objects wont move, that’s because the variable with the location is zeroCode:ouble playerpos=0; double pcpos=0; double ballx=0; double bally=0; int DrawGLScene(GLvoid) { //calculate_movements(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //ball ball(ballx,bally,-40); //player playerno1(playerpos,-16.5,-40.0); //pc playerno1(pcpos,14.5,-40.0); //left border border(-22.0,0.0,-40); //right border border(22.5,0.0,-40); glEnd(); return TRUE; }
First the user can control the quad at the bottom using the arrows, but he cant move it all the way to the left, it must stop when it hit any of the borders, we did that using this code in the keyboard input function:
When the playerpos changes the bottom quad will change with the new playerpos value, the same concept aply to the ball and the upper quad but their values will be assigned by the computer not the user, that’s why we created a function called “calculate_movements()” that runs in the beginning of the drawglscene(), here it’s code:Code:void Keyboard_Input() { if((GetKeyState(VK_LEFT) & 0x80)) { if(playerpos>=-15.5)playerpos-=.09; } if((GetKeyState(VK_RIGHT) & 0x80)) { if(playerpos<=15.5)playerpos+=.09; } if(GetKeyState('A')& 0x80) { if(start)start=false; else start=true; } }
I explained everything in the comments, that’s allCode:double ballx=0; double bally=0; double ballspeed=.05; bool updown=false; bool leftright=true; void calculate_movements() { //ball movement if(start) { //these two statements check if the ball have crossed any of the two borders //and reverse the direction of the ball if(ballx>20.45)leftright=false; if(ballx<-20.45)leftright=true; //updown variable is true when the ball is moving up and vice versa if(!updown) { //if its moving down, this mean that it can hit the player quad //we check if it hitted the layer quad here, if it did we revese the //updown bool variable and icrease the ball speed if((bally>-16.5 && bally<-13.5)&&(ballx>playerpos-6 && ballx<playerpos+6)) { updown=true; ballspeed+=.01; } //if the next condition is true, that means that the ball have crossed the //limit under the quad, which means that the user have lost //we reset the position and speed of the ball here else if(bally<-16.5) { //MessageBox(NULL,"you have lost against your stupid pc","sorry!",NULL); bally=0; ballx=0; ballspeed=.05; start=false; } } else {//same as above if((bally<16.5 && bally>13.5)&&(ballx>pcpos-6 && ballx<pcpos+6)) { updown=false; ballspeed+=.01; } else if(bally>16.5) { //MessageBox(NULL,"you have WON against your stupid pc","well done!",NULL); bally=0;ballx=0;ballspeed=.05; start=false; } } //the ball will move, when we set it position to a diffrent location //and thats what happens here, if the ball is moving up, we increase the "y" position of the ball //same with the ball "x" position if(updown)bally+=ballspeed; else bally-=ballspeed; if(leftright)ballx+=ballspeed; else ballx-=ballspeed; } //pc movement // the pc will move according to the ball "x" position //setting, you can move it slower by setting it to something less //than .2 if(start) { if(ballx>pcpos) pcpos+=.2; else pcpos-=.2; } }
This is the last part of the c++/opengl series , thank you for reading
Source code and comiled exe are attached , feel free to ask, rely , or comment or any part of this series
Cheers.
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
Nice job! +rep
Really neat! +rep
nice tutorial .. +rep when it lets me to![]()
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
oops
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
nice job
now i can +rep you .. once again nice tutorial![]()
blowwwwwwwwwww........fantastic.
+ROCK![]()
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
nice job
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks