C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,443 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,516 people online right now. Registration is fast and FREE... Join Now!




Creating a game in C++

 
Reply to this topicStart new topic

> Creating a game in C++, game tutorial #2!

Rating  4
aj32
Group Icon



post 9 Feb, 2008 - 02:23 PM
Post #1


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!) biggrin.gif tongue.gif

Attached File  Backdrop.bmp ( 977.62k ) Number of downloads: 3686


Next, the target

Attached File  target.bmp ( 1.44k ) Number of downloads: 2437


the shooter

Attached File  shooter.bmp ( 1.44k ) Number of downloads: 2428


and last but not least, the projectile!

Attached File  PROJ.BMP ( 1.65k ) Number of downloads: 2171


_____________________________________

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:
CODE

#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:
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:
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:
CODE

//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:
CODE

//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:
CODE

//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:
CODE

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 (
CODE
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:
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 smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jacobz_20
*



post 28 Sep, 2008 - 03:43 PM
Post #2
You should probably rename it to say that it uses the DB GDK.
Go to the top of the page
+Quote Post

lucianoefe
*



post 28 Oct, 2008 - 06:27 AM
Post #3
QUOTE(aj32 @ 9 Feb, 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!) biggrin.gif tongue.gif

Attached File  Backdrop.bmp ( 977.62k ) Number of downloads: 3686


Next, the target

Attached File  target.bmp ( 1.44k ) Number of downloads: 2437


the shooter

Attached File  shooter.bmp ( 1.44k ) Number of downloads: 2428


and last but not least, the projectile!

Attached File  PROJ.BMP ( 1.65k ) Number of downloads: 2171


_____________________________________

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:
CODE

#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:
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:
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:
CODE

//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:
CODE

//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:
CODE

//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:
CODE

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 (
CODE
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:
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 smile.gif

Go to the top of the page
+Quote Post

lucianoefe
*



post 28 Oct, 2008 - 06:33 AM
Post #4
hello, saw your program on this game and i am really interested... can u display ur output of the game environment?
Go to the top of the page
+Quote Post

Owen -> A
*



post 2 Dec, 2008 - 11:42 AM
Post #5
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: 2 Dec, 2008 - 12:00 PM
Go to the top of the page
+Quote Post

sas1ni69
*



post 6 Dec, 2008 - 01:07 PM
Post #6
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
Go to the top of the page
+Quote Post

bacNupe10
*



post 3 Mar, 2009 - 10:03 AM
Post #7
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 icon_up.gif
Go to the top of the page
+Quote Post

swoffenden
*



post 10 Apr, 2009 - 07:29 PM
Post #8
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):

CODE
#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
}
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 01:26AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month