+ Reply to Thread
Page 1 of 5 123 ... LastLast
Results 1 to 10 of 41

Thread: C++/opengl –part3 : creating simple game

  1. #1
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    C++/opengl –part3 : creating simple game

    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:
    Code:
    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();
    }
    Nothing new in the code, we only put the code in a separate function that takes three arguments with the location


    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:
    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();
    }
    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:


    When we put these stuff I the “DrawGLScene(GLvoid)” it will look like this:
    Code:
    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; 
    }
    If you tried this code you will notice that the objects wont move, that’s because the variable with the location is zero
    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:
    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;
    	}
    }
    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:
    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;
    }
    }
    I explained everything in the comments, that’s all
    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.
    Attached Thumbnails Attached Thumbnails C++/opengl –part3 : creating simple game-output.jpg  
    Attached Images Attached Images
    Attached Files Attached Files
    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: C++/opengl –part3 : creating simple game

    Nice job! +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Jordan Guest

    Re: C++/opengl –part3 : creating simple game

    Really neat! +rep

  5. #4
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: C++/opengl –part3 : creating simple game

    nice tutorial .. +rep when it lets me to

  6. #5
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: C++/opengl –part3 : creating simple game

    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  7. #6
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: C++/opengl –part3 : creating simple game

    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  8. #7
    badboyneo is offline Newbie
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Re: C++/opengl –part3 : creating simple game

    nice job

  9. #8
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: C++/opengl –part3 : creating simple game

    now i can +rep you .. once again nice tutorial

  10. #9
    Join Date
    Oct 2008
    Location
    Istog, Kosova
    Posts
    4,001
    Blog Entries
    1
    Rep Power
    40

    Re: C++/opengl –part3 : creating simple game

    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!

  11. #10
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Re: C++/opengl –part3 : creating simple game

    nice job

+ Reply to Thread
Page 1 of 5 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 51
    Last Post: 06-20-2011, 06:11 AM
  2. My first 3d opengl game
    By sdanyal in forum Community Projects
    Replies: 0
    Last Post: 11-24-2010, 01:02 AM
  3. Board Game with OpenGL and C++ 2
    By ashino in forum Games
    Replies: 0
    Last Post: 03-31-2010, 09:55 PM
  4. Board Game with OpenGL and C++
    By ashino in forum C and C++
    Replies: 2
    Last Post: 03-30-2010, 09:42 PM
  5. OpenGL 2D Game
    By erica_82 in forum C and C++
    Replies: 1
    Last Post: 05-02-2009, 02:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

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