Next time please use the code tags (press the "code" button before and after pasting the program code) to collapse your code under a nice pushbutton. It shortens the post and also preserves the format of the program text.
I have a suggestion how to land your spaceship. The first problem is that, when the collision is detected, you have a condition which can never be true. The x value will never be equal to 50 and 255 at the same time. If you want to check if X is between certain values, then do this:
if (x >= 50 && x <= 255 && y > 200)
The second problem is that you don't stop moving the spaceship when a collision happens. When this "if" condition is corrected, the "congratulations" text will scroll over the screen but the spaceship can still move and, as you said, slide through the platform. I suggest to introduce a status variable which shows whether the spaceship landed or not. If it landed, then don't move it any more. You can modify the collision detection function to return a boolean value which means whether the two sprites collided or not (with or without an extra coordinate check). Example:
bool detectCollision(int x, int y)
{
if (dbSpriteCollision(SPACESHIP_SPRITE_NUMBER, LANDING_PLATFORM_SPRITE_NUMBER) > 0) {
if (x >= 50 && x <= 255 && y > 200) return true;
}
return false;
}
Then, in the main loop, use the status variable to decide whether the spaceship can still move or not. If it has not landed yet, then move it. If it has landed, then don't move but display the congratulations text. I have also changed dbPrint to dbText because dbPrint changes the cursor position so the message will be printed on the next line every loop. With dbText you can specify the position where the text should be. Example:
bool Landed = false;
while ( LoopGDK() )
{
if (! Landed) {
getSPACESHIPcoordinates(spaceshipX, spaceshipY);
dbSprite(SPACESHIP_SPRITE_NUMBER, spaceshipX, spaceshipY, SPACESHIP_IMAGE_NUMBER);
Landed = detectCollision(spaceshipX, spaceshipY);
} else {
dbText(10, 10, "Congratulations!");
}
dbSync();
}
This will solve the problem. I have one more advice: the function which sets the coordinates of the moving spaceship is a little overcomplicated. It can be written shorter:
void getSPACESHIPcoordinates(int &x, int &y)
{
if (dbSpaceKey()) {
y--;
if (dbRightKey()) x++;
if (dbLeftKey()) x--;
} else if (y < 350) y++;
}