Page 1 of 1

Creating a game in C++ game tutorial #2! Rate Topic: ****- 4 Votes

#1 aj32  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 577
  • Joined: 30-August 07

Post icon  Posted 09 February 2008 - 03:23 PM

In this tutorial, I will show you how to create a simple game.
The objective of the game will be to hit a moving target with a projectile fired from a shooter!

Requirements:
Visual C++ 2008 express
The "Dark GDK" add-in for VC++ 08
_____________________________

First Steps: Create the sprites

You will need to create the sprites that will be used in the program, I am going to use sprites without animations for this tutorial.

First I will design the background of my game, the background image can be anything from a simple colored canvas to a 3D world!

Ok, somehow I managed to come up with this beautiful creation!!! (not really!) :D :P

Attached File  Backdrop.bmp (977.62K)
Number of downloads: 20529

Next, the target

Attached File  target.bmp (1.44K)
Number of downloads: 11282

the shooter

Attached File  shooter.bmp (1.44K)
Number of downloads: 11884

and last but not least, the projectile!

Attached File  PROJ.BMP (1.65K)
Number of downloads: 9735

_____________________________________

Now that we have our sprites ready, we are ready to start on the coding part of our game!

1. Start VC++ 08
2. Click "File->New->Project"
3. Under wizards, select "Dark GDK - GAME"
4. Set the name & storage location
5. Click Ok.

1. Delete all the code in "main.cpp".
2. Insert the following starting code into the main.cpp file:
#include "DarkGDK.h" //Required Header File


void DarkGDK ( void )//The Main Funtion for the Dark GDK
{
	// in this application a backdrop is loaded and then several
	// sprites are loaded, the sprites are targets!

	dbSyncOn   ( ); //Turn on Sync
	dbSyncRate ( 100 ); 
	  // Set Sync Rate for screen:
	  // The lower it is, the faster the game runs but the visual effects will be bad.
	  // The higher the setting - the opposite will occour!

	dbDisableEscapeKey ( ); //disable the "escape" key

	  dbRandomize ( dbTimer ( ) ); //use the clock to obtian random numbers more effieciently

	dbLoadImage ( "backdrop.bmp", 1 ); // this will load our background onto the game
	dbSprite ( 1, 0, 0, 1 ); // sprite used for the background

	dbSetImageColorKey ( 255, 0, 255 );// Set the transparency color of the sprites, in this case, it will be bright pink
	  


	while ( LoopGDK () )
	{

		 if ( dbEscapeKey ( ) ) // if the ecape key is pressed, the program will exit
			 break;

		 dbSync ( );//update the screen contents
	}

	// close the program //
	// delete all the sprites
	for ( int i = 1; i < 30; i++ )
		dbDeleteSprite ( i );

	// delete the backdrop image
	dbDeleteImage ( 1 );

	return;//return back to windows
}




Now, we have a simple game going! No I mean like REALLY Simple!

So let's add in the code that loads and displays the target and the shooter:

between dbSetImageColorKey ( 255, 0, 255 ); & while ( LoopGDK () ) Insert this code:
	int i = 1, T = 2, S = 3, P = 4;
	dbLoadImage("target.bmp", T);//load the target
	dbSprite ( T, 250, 0, T );//Display the target
	


Now the target will be displayed at the top of the screen!

Insert the shooter:

before the while ( LoopGDK () ) line, Insert this code:
	
	i++;
	dbLoadImage("shooter.bmp", S);//Load the shooter
	dbSprite ( S, 200, 460, S );//Display the Shooter



This will load the shooter onto the screen when the game is started!


After that, we are ready to create the part of the game that controls the animation and movement of the target!

Let's add the variables to our project:
beforewhile ( LoopGDK() )
add this:
//begin the animation loop//
	dbLoadImage("PROJ.bmp", P);//load the Projectile sprite

	int D = 0;
	int R = 1;
	int M = 200;
	int AC = 50;
	int BC = 4;



this code will load the projectile sprite and prepare the variables

To make the target move back and forth, add this code after the LoopGDK() statement:
//START the animation of the target//
		int DIF = 5;//The difficulty of the game, *Make sure this number is either 2, 5, 10, or 50!;
			if(R == 1)
			{
				dbSprite(T,D, 1,T);
				D=D+DIF;
				if(D==650)
				{
					R = 0;
				}
			}
			else if(R == 0)
			{
				dbSprite(T,D,1,T);
				D=D-DIF;
				if(D == 0)
				{
					R = 1;
				}
			}

			//END target animation//



Now, if you run the game, the target will bounce back and forth at the top of the screen!

We want the user to be able to move the shooter back and forth at the bottom of the screen. After //END target animation// add:
//START user input controls//

			if( dbRightKey() && !(M > 625))
			{
				dbSprite(S, M, 460, S);
				M = M + 2;
			}
			if( dbLeftKey() && !(M <= 0))
			{
				dbSprite(S, M, 460, S);
				M = M - 2;
			}


Then, after that, add the control that will enable the user to "Fire" the projectile:
if( dbSpaceKey())
			{
				if(AC == 50)
				{
				dbSprite(P, M, 425, P);
				AC = 0;
				}
			}

			if(AC < 50)//If the projectile is active,
			{
			dbMoveSprite(P, 10);//then make it move
			AC++;
			}

			//END User input controls//




This will make the program fire the projectile when the [SPACE] bar is pressed. The seconde part (
if(AC < 50)//If the projectile is active,
			{
			dbMoveSprite(P, 10);//then make it move
			AC++;
			}
) will make the projectile move up untill it hits the top of the screen


Now that that's done, The game is almost done, if you run it, you should be able to move the shooter and fire the projectile. Also, the target will move from back to forth.

Finally, we will add the part of the game where, if the projectile hits the target, the game will complete and exit:

After //END User input controls//
Add this code:
//START Target hit/missed controls//

			if(AC < 50)
			{
				if((dbSpriteY(P) >= 0 && dbSpriteY(P) <= 10))
				{
				if((dbSpriteX(P) <= (dbSpriteX(T) + 20) && (dbSpriteX(P) >= (dbSpriteX(T) - 20))))
				{
					dbDeleteSprite(T);
					MessageBox(NULL, "You Won!, Click OK to exit.", "Congrats!", MB_OK);
					break;
				}
				}
			}
			//END Target hit/missed controls//



Now, the game should be fully functional! You should be able to fire at the target and, if you hit it, the game will display a message box, and exit!

We are now pretty much done, there are just a few things that can be edited: The first thing is the difficulty level, do you see in the code int DIF = 5;//The difficulty of the game, *Make sure this number is either 1, 2, 5, 10, or 50!;
This code controls how fast the target moves, by default, it is set to 5, the higher the number, the harder it is to hit the target. *Remember: If you change the difficulty, it MUST be set to either: 1, 2, 5, 10, or 50, or the game will not work properly!

The second thing is the screen sync rate: dbSyncRate ( 100 ); , I have it set to 100, if this number is lower, the actions of the game will be slower, if higher, the actions of the game will work faster, but the game will require more system resources. In this game, there is not much to worry about, since it is not that complex, but in a very complex 3D game, higher sync rates will greaty affect the system resources required.

And that's about it, enjoy your new game!

-AJ32 :)

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a game in C++

#2 jacobz_20  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 14-September 08

Posted 28 September 2008 - 04:43 PM

You should probably rename it to say that it uses the DB GDK.
Was This Post Helpful? 0
  • +
  • -

#3 lucianoefe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 08

Posted 28 October 2008 - 07:33 AM

hello, saw your program on this game and i am really interested... can u display ur output of the game environment?
Was This Post Helpful? 0
  • +
  • -

#4 Owen -> A  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 28-November 08

Posted 02 December 2008 - 12:42 PM

Thank you very much for your game tutorial Aj. And it taught me alot.

But i have an error. When i debug the project, the sprites dont ,move like you said they would.
Hope you understand?

Please help! I'll try and fix to untill i get a reply.

Kind Regards,

Owen.

This post has been edited by Owen -> A: 02 December 2008 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Posted 06 December 2008 - 02:07 PM

Hey Aj, Awesome tut ! Thanks a lot, it really shows basic outlines of a game and how to put it in Dark GDK, but the game isn't fully functional. The sprite just won't move. Tried to figure it out, but I couldn't. Would be nice if you can see what might be wrong.

Thanks again !

-Sas1ni69
Was This Post Helpful? 0
  • +
  • -

#6 bacNupe10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-August 08

Posted 03 March 2009 - 11:03 AM

YO AJ...thanks for putting up a great tutorial. Everything came out great and I'm going to try and add some additional elements to this project. I look forward to seeing more of these gaming tutorials from you.

~Peace :^:
Was This Post Helpful? 0
  • +
  • -

#7 swoffenden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-April 09

Posted 10 April 2009 - 08:29 PM

Thanks AJ, great tutorial!

There are a couple of things that I wanted to clarify based upon a couple of things I ran into during this tutorial.

First off, the bitmap images (.bmp files) should be saved to C:\Users\Scott\Documents\Visual Studio 2008\Projects\Dark GDK - 3D Game Tutorial\Dark GDK - 3D Game Tutorial\. (FYI - I named the project Dark GDK - 3D Game Tutorial and Create directory for solution was checked during project creation.)

Lastly, the complete tutorial code looks as follows (as I have it):

#include "DarkGDK.h" //Required Header File

void DarkGDK ( void )//The Main Funtion for the Dark GDK
{
	// in this application a backdrop is loaded and then several
	// sprites are loaded, the sprites are targets!

	dbSyncOn   ( ); //Turn on Sync
	dbSyncRate ( 100 );
	  // Set Sync Rate for screen:
	  // The lower it is, the faster the game runs but the visual effects will be bad.
	  // The higher the setting - the opposite will occour!

	dbDisableEscapeKey ( ); //disable the "escape" key

	  dbRandomize ( dbTimer ( ) ); //use the clock to obtian random numbers more effieciently

	dbLoadImage ( "media/Backdrop.bmp", 1 ); // this will load our background onto the game
	dbSprite ( 1, 0, 0, 1 ); // sprite used for the background

	dbSetImageColorKey ( 255, 0, 255 );// Set the transparency color of the sprites, in this case, it will be bright pink
	 

	int i = 1, T = 2, S = 3, P = 4;
	dbLoadImage("media/target.bmp", T);//load the target
	dbSprite ( T, 250, 0, T );//Display the target


	i++;
	dbLoadImage("media/shooter.bmp", S);//Load the shooter
	dbSprite ( S, 200, 460, S );//Display the Shooter


	//begin the animation loop//
	dbLoadImage("media/PROJ.bmp", P);//load the Projectile sprite

	int D = 0;
	int R = 1;
	int M = 200;
	int AC = 50;
	int BC = 4;


	while ( LoopGDK () )
	{

		//START the animation of the target//
		int DIF = 5;//The difficulty of the game, *Make sure this number is either 2, 5, 10, or 50!;
			if(R == 1)
			{
				dbSprite(T,D, 1,T);
				D=D+DIF;
				if(D==650)
				{
					R = 0;
				}
			}
			else if(R == 0)
			{
				dbSprite(T,D,1,T);
				D=D-DIF;
				if(D == 0)
				{
					R = 1;
				}
			}

			//END target animation//

			//START user input controls//

			if( dbRightKey() && !(M > 625))
			{
				dbSprite(S, M, 460, S);
				M = M + 2;
			}
			if( dbLeftKey() && !(M <= 0))
			{
				dbSprite(S, M, 460, S);
				M = M - 2;
			}

		if( dbSpaceKey())
			{
				if(AC == 50)
				{
				dbSprite(P, M, 425, P);
				AC = 0;
				}
			}

			if(AC < 50)//If the projectile is active,
			{
			dbMoveSprite(P, 10);//then make it move
			AC++;
			}

			//END User input controls//

			//START Target hit/missed controls//

			if(AC < 50)
			{
				if((dbSpriteY(P) >= 0 && dbSpriteY(P) <= 10))
				{
				if((dbSpriteX(P) <= (dbSpriteX(T) + 20) && (dbSpriteX(P) >= (dbSpriteX(T) - 20))))
				{
					dbDeleteSprite(T);
					MessageBox(NULL, "You Won!, Click OK to exit.", "Congrats!", MB_OK);
					break;
				}
				}
			}
			//END Target hit/missed controls//


		 if ( dbEscapeKey ( ) ) // if the ecape key is pressed, the program will exit
			 break;

		 dbSync ( );//update the screen contents
	}

	// close the program //
	// delete all the sprites
	for ( int i = 1; i < 30; i++ )
		dbDeleteSprite ( i );

	// delete the backdrop image
	dbDeleteImage ( 1 );

	return;//return back to windows
}


Was This Post Helpful? 0
  • +
  • -

#8 SPARTAN-314  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-July 11

Posted 30 July 2011 - 09:27 PM

Um... I have a problem. when i try and build it says build failed and when i used the output bar it says that i cannot open the file "d3dx9.h" in the directory "c:\program files\the game creators\dark gdk\include\darksdkcamera.h(16)"

Heres the full code:
1>------ Build started: Project: Game1, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:\program files\the game creators\dark gdk\include\darksdkcamera.h(16) : fatal error C1083: Cannot open include file: 'd3dx9.h': No such file or directory
1>Build log was saved at "file://c:\Documents and Settings\Kids\Desktop\game1\Game1\Debug\BuildLog.htm"
1>Game1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Posted 31 July 2011 - 09:15 AM

Do you have the DirectX 9.0c SDK installed as noted in the system requirements?
Was This Post Helpful? 0
  • +
  • -

#10 SPARTAN-314  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-July 11

Posted 31 July 2011 - 10:47 AM

View PostJackOfAllTrades, on 31 July 2011 - 09:15 AM, said:

Do you have the DirectX 9.0c SDK installed as noted in the system requirements?


Yes, but i dont have hardware acceleration on.(it screws up dungeon keeper 2)
Was This Post Helpful? 0
  • +
  • -

#11 jpedersm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-August 11

Posted 04 August 2011 - 03:15 AM

I got everything working so far right but, when I hit the target it wont try to close the program. Anyone have any ideas?
Was This Post Helpful? 0
  • +
  • -

#12 lilsheep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-July 12

Posted 29 July 2012 - 02:48 AM

Note: I am a Noobish java programmer

All it shows is black, I moved the picture files around different places but all I can see is black! Help please, also this tutorial is the bees knees. Thank you =]
Was This Post Helpful? 0
  • +
  • -

#13 lilsheep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-July 12

Posted 29 July 2012 - 02:55 AM

View Postlilsheep, on 29 July 2012 - 02:48 AM, said:

Note: I am a Noobish java programmer

All it shows is black, I moved the picture files around different places but all I can see is black! Help please, also this tutorial is the bees knees. Thank you =]


NVM! Got it :D

YEAH forget hello world. first program up and running ;D

Thank you swoffenden, you ease the pain of the noob!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1