Jump to content

C++/opengl –part3 : creating simple game

- - - - -

  • Please log in to reply
40 replies to this topic

#1
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
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:
http://forum.codecal...=1&d=1231358977

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:

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:

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:

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:
http://forum.codecal...=1&d=1231359187

When we put these stuff I the “DrawGLScene(GLvoid)” it will look like this:

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:

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:

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 Files

  • Attached File  src.zip   118.45K   1927 downloads
  • Attached File  1.bmp   192.05K   6205 downloads
  • Attached File  output.JPG   17.5K   5828 downloads

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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Nice job! +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Really neat! +rep

#4
Egz0N

Egz0N

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,034 posts
nice tutorial .. +rep when it lets me to :)

#5
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts

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

#6
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
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"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#7
badboyneo

badboyneo

    Newbie

  • Members
  • Pip
  • 1 posts
nice job

#8
Egz0N

Egz0N

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,034 posts
now i can +rep you .. once again nice tutorial :)

#9
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
blowwwwwwwwwww........fantastic.

+ROCK :D

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#10
zanfir.ovidius

zanfir.ovidius

    Newbie

  • Members
  • Pip
  • 2 posts
nice job

#11
tato.mv

tato.mv

    Newbie

  • Members
  • Pip
  • 2 posts
great game!

#12
tato.mv

tato.mv

    Newbie

  • Members
  • Pip
  • 2 posts
where is the source code?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users