Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,035 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,005 people online right now. Registration is fast and FREE... Join Now!




Creating a simple game that responds off of user input

 
Reply to this topicStart new topic

> Creating a simple game that responds off of user input, Using the new Dark GDK

Rating  5
aj32
Group Icon



post 2 Feb, 2008 - 10:24 PM
Post #1


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:

Attached File  bitmap.bmp ( 991.74k ) Number of downloads: 650


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

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



This post has been edited by aj32: 9 Feb, 2008 - 09:42 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

brandon99337
**



post 21 Feb, 2008 - 04:29 PM
Post #2
Awesome, No Errors And I Finally UnderStand It... Thanks!
Go to the top of the page
+Quote Post

Racso_the_great
*



post 17 Aug, 2008 - 11:45 AM
Post #3

How do you make the sprite go right? blink.gif
Go to the top of the page
+Quote Post


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

 

Lo-Fi Version Time is now: 11/21/08 05:42AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month