+ Reply to Thread
Page 1 of 4
1 2 3 ... LastLast
Results 1 to 10 of 33

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

  1. #1
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

    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 C++/opengl –part3 : creating simple game-output.jpg  
    Attached Images
    • File Type: bmp 1.bmp (192.1 KB, 1076 views)
    Attached Files

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57

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

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

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

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

    Really neat! +rep

  4. #4
    Code Warrior Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N's Avatar
    Join Date
    Sep 2008
    Location
    Kosovo
    Age
    18
    Posts
    4,034

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

    nice tutorial .. +rep when it lets me to

  5. #5
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

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


  6. #6
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

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

    oops

  7. #7
    Newbie badboyneo is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    1

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

    nice job

  8. #8
    Code Warrior Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N's Avatar
    Join Date
    Sep 2008
    Location
    Kosovo
    Age
    18
    Posts
    4,034

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

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

  9. #9
    Guru MathX has a spectacular aura about MathX has a spectacular aura about MathX's Avatar
    Join Date
    Oct 2008
    Location
    Kosovo
    Age
    19
    Posts
    4,006

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

    blowwwwwwwwwww........fantastic.

    +ROCK

  10. #10
    Newbie zanfir.ovidius is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    2

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

    nice job

+ Reply to Thread
Page 1 of 4
1 2 3 ... LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Creating simple Notition store program
    By Coldhearth in forum Java Help
    Replies: 1
    Last Post: 12-10-2008, 03:44 PM
  2. A simple TicTacToe game
    By Zunone in forum C and C++
    Replies: 1
    Last Post: 08-16-2007, 11:01 AM
  3. need help with simple C++ TicTacToe game with AI
    By flupke1 in forum C and C++
    Replies: 11
    Last Post: 08-14-2007, 10:27 AM
  4. Replies: 1
    Last Post: 07-19-2007, 06:46 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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