I am using darkGDK to complete an asignment where I am to create a sprite with a moon background and to make a rocket ship move up with the spacebar pressed and come back down agian when it is released. I can't figure out what I don't have in my code to keep my rocket from floating off the bottom of the screen. Here is what I have so far.

#include "DarkGDK.h"

// set the refresh rate.
const int REFRESH_RATE = 60;

// assign image numbers 
const int ROCKET_IMAGE_NUMBER  = 1;
const int MOON_IMAGE_NUMBER  = 2;

// Rocket sprite number
const int ROCKET_SPRITE_NUMBER   = 1;

// Rocket starting coordinates

const int ROCKET_STARTING_X = 300;
const int ROCKET_STARTING_Y = 300;



// text coordinates
const int TEXT_X = 319;
const int TEXT_Y = 20;



// set prototypes
void moveUp();
void getROCKETcoordinates(int &, int &);


void DarkGDK()
{
    // Variables for the Rocket's x and y coordinates.
    int rocketX = ROCKET_STARTING_X;
    int rocketY = ROCKET_STARTING_Y;


    moveUp();


    // Game loop
    while ( LoopGDK() )
    {

        dbPasteImage(MOON_IMAGE_NUMBER, 0, 0);


        dbCenterText(TEXT_X, TEXT_Y, 
            "Use the spacebar to move the Rocket");


        getROCKETcoordinates(rocketX, rocketY);


        dbSprite(ROCKET_SPRITE_NUMBER, rocketX, rocketY,ROCKET_IMAGE_NUMBER);

        // Refresh the screen.
        dbSync();
    }
}


void moveUp()
{
   // Set the key color to green.
   dbSetImageColorKey(0, 255, 0);

   // Load images.
   dbLoadImage("moon.bmp", MOON_IMAGE_NUMBER);
   dbLoadImage("rocket.bmp",   ROCKET_IMAGE_NUMBER);

   // Create the Rocket sprite and display it at its
   // starting coordinates.
   dbSprite(ROCKET_SPRITE_NUMBER, ROCKET_STARTING_X, 
            ROCKET_STARTING_Y, ROCKET_IMAGE_NUMBER);


   dbSyncOn();               
   dbSyncRate(REFRESH_RATE); 


}

void getROCKETcoordinates(int &x, int &y)
{

    if ( dbSpaceKey() )
    {
        y--;
    }

    if (!dbSpaceKey() )
    {
        y++;
    }

}
if (!dbSpaceKey() && RocketYpositionHolder < 600)
{
y++;
}

You will need to change it to fit the rocket and screen size, but something like that should work.

You also should be able to replace the "600" in Unseen Machine's example with a call to dbScreenHeight() for readability and so that you don't have to change that in the case that the screen size changes, but you still should adjust that value to the rocket's size by subtracting the size of the rocket from that value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.