Full Version: Creating a simple game that responds off of user input
Dream.In.Code > Programming Tutorials > C++ Tutorials
aj32
This is a simple tutorial on how to build a simple animation using the new "Dark GDK" for Visual C++ 2008 express

Required Programs:
1. Visual Studio C++ 2008 Express
2. The "Dark GDK" add-on for VC++ 08

=============================================

After downloading and installing the dark GDK, open VC++ 08 and click "File->New->Project" (or "ctrl+shift+N").
Under project types, click "Wizards".
Select "Dark GDK - 2D Game", name the project something like 'Dark GDK - 2D Game1", select the folder in which you want your project to be stored in, and click "OK".
Now, under solution explorer, click "main.cpp".
There will already be a neat little bit of code there and if you run it (ctrl+F5) a neat animation will be displayed!

Now for the editing:

1. Let's edit the code that places the object at a random location, find this code
CODE
    for ( int i = 2; i < 30; i++ )
    {
        // create an animated sprite and give it the ID number from the
        // variable i, next is the filename, now we come to how many frames
        // across and down, in our case this is 4, finally we come to the image
        // ID that the sprite will use, again we use i
        dbCreateAnimatedSprite ( i, "sprite.bmp", 4, 4, i );

        // position our sprite at a random location
        dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
    }

and change it to:
CODE
int i = 2;
    
        dbCreateAnimatedSprite ( i, "sprite.bmp", 4, 4, i );

        // position our sprite
        dbSprite ( i, 100 , 100, i );


The code will now display 1 object and it will fall to the bottom of the screen.

2. Now, we will make the object work off of user input.

-Find this code:
CODE

    // now we come to our main loop, we call LoopGDK so some internal
    // work can be carried out by the GDK
    while ( LoopGDK ( ) )
    {
        // run a loop through all our sprites
        for ( int i = 2; i < 30; i++ )
        {
            // move the sprite down and play its animation
            // moving from frame 1 to 16 with a delay of 60 ms
            dbMoveSprite ( i, -2 );
            dbPlaySprite ( i, 1, 16, 60 );

            // check the position of the sprite, if it has gone off scren
            // then reposition it back to the top
            if ( dbSpriteY ( i ) > 500 )
                dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
        }

        // here we check if the escape key has been pressed, when it has
        // we will break out of the loop
        if ( dbEscapeKey ( ) )
            break;

        // here we make a call to update the contents of the screen
        dbSync ( );
    }


and replace it with this code to make it work off of user input:
CODE

        //NOW: let's make our sprite move on user input!//
        int SDS = 1;
    while ( LoopGDK () )
    {
          if(dbControlKey()) //Toggle constant animation on\off//
          {
              if(SDS == 1)
              {
                  SDS = 2;//animation is off
              }
              else
              {
                  SDS = 1;//animation is on
              }
          }

          if(SDS == 1)//check to see if animation is toggled
          {
              dbPlaySprite ( i, 1, 16, 60 );
          }

          if(dbDownKey())//do something on user input
          {
            dbMoveSprite ( i, -2 );
            dbPlaySprite ( i, 1, 16, 60 );
          }
          if(dbUpKey())//do something different on different user input
          {
            dbMoveSprite ( i, 2 );
            dbPlaySprite ( i, 1, 16, 60 );
          }
    
        // here we check if the escape key has been pressed, when it has
        // we will break out of the loop
        if ( dbEscapeKey ( ) )
            break;

        // here we make a call to update the contents of the screen
        dbSync ( );
          }





3. Test the application:

press [Ctrl]+[F5] to run the program.

Now, the program should display something like this:

Click to view attachment

=============================================

Congrats! You can now control what the object does!
Pressing the up and down arrows will move it on the screen and pressing [Ctrl] will toggle the animation!

Other controls can be added, such as useing dbLeftKey & dbRightKey.


=============================================

I hope this tutorial is helpful, it's the first one I have done. There are probably some errors in it, let me know if there are.

---------------------------------------------------------------------------------

-aj32 smile.gif

brandon99337
Awesome, No Errors And I Finally UnderStand It... Thanks!
Racso_the_great

How do you make the sprite go right? blink.gif
zGoDLinGz
I have Visual C++ 2008 Express Edition
(does it work with express edition? if not could u plz tell me wat does?)
Please help me i have downloaded everything but when i am installing the GDK download it says
"Error attempting to read from the source install database:
C:\WINDOWS\Installer\65efb8.msi."

thats what it says and pease reply me as soon as possible i really want to get started on making games
it looks really fun

please please help me if ANY help at all i will appreciate it very very much
!!!!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.