Hi there,
I have been working hard to make my first 2d game after I started learning DarkGDK 2 months ago. I am trying to produce a game, where rocks fall from cart on the top of the screen and the player has to catch them by cart on the bottom of the screen. I have done everything, there is no errors in the code, although after the title screen, the game goes straight to the summary page with score on it. Could someone have a look and see what is there missing please? I can imagine I am missing a very simple thing, but I just can't figure out what it is!
// Catch the Boulder Game
#include "DarkGDK.h"
// Constants for the image numbers
const int title_screen_image = 1;
const int rock_image = 2;
const int broken_rock_image = 3;
const int stone_bg_image = 4;
const int cart_image = 5;
const int rock_cart_image = 6;
const int tracks_image = 7;
// Constants for the sprite numbers
const int stone_bg_sprite = 1;
const int cart_sprite = 2;
const int rock_sprite = 3;
const int rock_cart_sprite = 4;
const int broken_rock_sprite = 5;
const int tracks_sprite = 6;
// Constants for the sounds numbers
const int crash_sound = 1;
const int metal_hit_sound = 2;
// Constant for the music number
const int intro_music = 1;
// Time delay constants
const int tenth_second = 100;
const int one_second = 1000;
const int four_seconds = 4000;
// Other constants
const int max_rocks = 40; // Total number of rocks
const int cart_move = 4; // Amount to move the cart
const int rock_cart_move = 2; // Amount to move the rock cart
const float ACCELERATION = 0.5; // Gravity acceleration
const int refresh_rate = 60; // Refresh(sync) rate
// Function prototypes
void loadResources();
void createSprites(int &, int &, int &, int &,
int &, int &);
void intro();
void moveCart(int &);
void moveRockCart(int &);
void moveRock(int, int, int &, int &, int &, float);
void checkCollisions(int, int &, int &, int &,int &, int &, float&);
void showBrokenRock();
void showHitCart();
void resetRock(int, int, int &, float&);
void render(int, int, int, int, int, int);
void deleteSprites();
void summaryScreen(int, int);
// Functions
void DarkGDK()
{
//Local variables
int rocks = max_rocks; // Number of rocks in game
int caught = 0; // Number of rocks caught
int broken = 0; // Number of rocks broken
int cartX, cartY; // Cart X & Y coordinates
int rockCartX,rockCartY; // Rock cart X & Y coordinates
int rockX, rockY; // Rock X & Y coordinates
int time = 0; // Time for distance formula
// Rocks acceleration
float acceleration = ACCELERATION;
// Load the game resources
loadResources();
// Display the title screen
intro();
// Create the sprites
createSprites(cartX,cartY,rockX,rockY,rockCartX,rockCartY);
// Loop game
while ( LoopGDK() && rocks > 0 )
{
// Loop the main music
dbLoopMusic(intro_music);
// Move the cart
moveCart(cartX);
// Move the rock cart
moveRockCart(rockX);
// Move the egg at the current rate of acceleration
moveRock(rockCartX,rockCartY,rockX,rockY,
time,acceleration);
// Check for collisions
checkCollisions(rockCartY, rockY, caught,
broken, rocks, time,
acceleration);
// Render the scene
render(cartX,cartY,rockX,rockY,
rockCartX,rockCartY);
}
// Stop the music
dbStopMusic(intro_music);
// Display the summary screen
summaryScreen(caught,broken);
}
// loadResources loads images and sounds
void loadResources()
{
// Set the color key to green
dbSetImageColorKey(0,255,0);
// Load all images
dbLoadImage("TitleScreen.bmp", title_screen_image);
dbLoadImage("Rock.bmp", rock_image);
dbLoadImage("BrokenRock.bmp", broken_rock_image);
dbLoadImage("StoneBackground.bmp",stone_bg_image);
dbLoadImage("Cart.bmp",cart_image);
dbLoadImage("CartWithRock.bmp",rock_cart_image);
dbLoadImage("Tracks.bmp", tracks_image);
// Load the music
dbLoadMusic("DownInAHole.mp3", intro_music);
// Load the sound effects
dbLoadSound("crash.wav",crash_sound);
dbLoadSound("Hit metal.mp3",metal_hit_sound);
}
// Displays a title screen and plays music
void intro()
{
// Set the window title
dbSetWindowTitle("Catch the Boulder");
// Loop the intro music
dbLoopMusic(intro_music);
// Paste the title screen and wait untill a key is pressed
dbPasteImage(title_screen_image,0,0);
dbWaitKey();
// Clear the screen
dbCLS();
// Stop the music
dbStopMusic(intro_music);
// Disable auto refresh and set the refresh rate
dbSyncOn();
dbSyncRate(refresh_rate);
}
// Creates sprites
void createSprites(int &cartX, int &cartY, int &rockX,
int &rockY, int &rockCartX, int &rockCartY)
{
// Variable holds calculated sprite numbers
int spriteNum;
// Create the sprites, positioned at (0, 0)
dbSprite(stone_bg_sprite,0,0,stone_bg_image);
dbSprite(broken_rock_sprite,0,0,broken_rock_image);
// Initially, position rock and cart sprites off screen
dbSprite(cart_sprite,-100,0,cart_image);
dbSprite(rock_sprite,-100,0,rock_image);
// Hide broken rock sprite
dbHideSprite(broken_rock_sprite);
// Set the starting point of the rock cart to the top center of the screen
rockCartX = dbScreenWidth() / 2 -
dbSpriteWidth(rock_cart_sprite) / 2;
rockCartY = 0;
// Set the starting point of the rock so it is at rock carts position
rockX = rockCartX + dbSpriteWidth(rock_cart_sprite) -
dbSpriteWidth(rock_sprite);
rockY = rockCartY +
dbSpriteHeight(rock_cart_sprite) / 2;
// Set the cart's starting X position to a random location on screen
dbRandomize( dbTimer() );
cartX = dbRND( dbScreenWidth() -
dbSpriteWidth(cart_sprite) );
// Set the baskets starting Y position to the bottom of the screen
cartY = dbScreenHeight() -
dbSpriteHeight(cart_sprite);
// Clone the rock sprite to display mini rocks
// at the top of the screen. The cloned mini rocks
// numbers will begin at 100.
for(int count = 0; count < max_rocks; count++)
{
// Calculate a new sprite number
spriteNum = 100 + count;
// Clone the rock sprite
dbCloneSprite(rock_sprite, spriteNum);
// Scale the sprite to 50%
dbScaleSprite(spriteNum,50);
// Place the rock sprites in the status bar
dbSprite(spriteNum,
dbSpriteWidth(rock_sprite) / 2 * count,
0,
rock_image);
}
}
// Takes input from keyboard and moves the cart accordingly
void moveCart(int &cartX)
{
// check if the left key is pressed
if(dbLeftKey())
{
// Stop the cart at the left edge of the screen
if (cartX <= 0)
{
cartX = 0;
}
// Move the cart left
else
{
cartX -= cart_move;
}
}
// Check if the right key is pressed
if(dbRightKey())
{
// Stop the cart at the right edge of the screen
if(cartX + dbSpriteWidth(cart_sprite) >= dbScreenWidth())
{
cartX = dbScreenWidth() - dbSpriteWidth(cart_sprite);
}
// Move the cart right
else
{
cartX += cart_move;
}
}
}
// Moves the rock cart
void moveRockCart(int &rockCartX)
{
// Check the rock cart X position.
// If it is less or equal than 0, mirror the cart sprite
if (rockCartX <= 0)
{
dbMirrorSprite(rock_cart_sprite);
}
// If the rock cart is at the right edge of the screen,
// mirror the rock cart sprite
if (rockCartX + dbSpriteWidth(rock_cart_sprite) >= dbScreenWidth())
{
dbMirrorSprite(rock_cart_sprite);
}
// If the rock cart is mirrored move it to the right
if ( dbSpriteMirrored(rock_cart_sprite))
{
rockCartX += rock_cart_move;
}
// If the rock cart is not mirrored move left
else
{
rockCartX -= rock_cart_move;
}
}
// Moves the rock down and across the screen
void moveRock(int rockCartX, int rockCartY,
int &rockX, int &rockY, int &time,
float accel)
{
// Local variable to hold the falling distance
float distance;
// Set the rocks X position so it is just below the rock cart
if( dbSpriteMirrored(rock_cart_sprite) )
{
rockX = rockCartX +
dbSpriteWidth(rock_cart_sprite) -
dbSpriteWidth(rock_sprite);
}
else
{
rockX = rockCartX;
}
// Calculate the falling distance.
distance = 0.5 * accel * time * time;
// Update the rock position
rockY = distance + rockCartY +
dbSpriteHeight(rock_cart_sprite) / 2;
// Increment the falling time
time++;
}
// Check for the collision between rock and ground / cart
void checkCollisions(int rockCartY, int &rockY, int &caught,
int &broken, int &time, int &rocks,
float &accel)
{
// The rock has hit the cart
if ( dbSpriteCollision(rock_sprite, cart_sprite) )
{
// Increment number of caught rocks
caught++;
// Decrement number of rocks left to play
rocks--;
// Reset the rocks
resetRock(rocks,rockCartY,rockY,accel);
// Reset the falling time
time = 0;
}
// The rock that missed the basket
else if (rockY > dbScreenHeight() )
{
// Increment the number of broken rocks
broken++;
// Decrement the number of rocks left
rocks--;
// Show the broken rock
showBrokenRock();
// Reset the rock
resetRock(rocks,rockCartY,rockY,accel);
// Reset the falling time
time = 0;
}
}
// Plays the caught rock sound
void showHitCart()
{
dbSyncOff();
dbPlaySound(metal_hit_sound);
dbWait(tenth_second);
dbSyncOn();
}
// Displays the broken rock and plays the broken sound
void showBrokenRock()
{
// Temporarily enable auto refresh
dbSyncOff();
// Show the broken rock sprite
dbShowSprite(broken_rock_sprite);
// Play the sound
dbPlaySound(crash_sound);
// Wait for a tenth of a second
dbWait(tenth_second);
// Hide the broken rock sprite
dbHideSprite(broken_rock_sprite);
// Disable auto refresh
dbSyncOn();
}
// Resets the rock after it landed on the ground or in cart
void resetRock(int rocks, int rockCartY, int &rockY, float &accel)
{
// If a clone of this egge sprite exists in the status bar, delete it
if (dbSpriteExist(rocks+100))
{
dbDeleteSprite(rocks+100);
}
// Wait
dbWait(tenth_second);
rockY = rockCartY +
dbSpriteHeight(rock_cart_sprite) / 2;
}
// Render function draws the screen
void render(int cartX, int cartY, int rockX, int rockY,
int rockCartX, int rockCartY)
{
// Draw the sprites
dbSprite(cart_sprite, cartX, cartY, cart_sprite);
dbSprite(rock_sprite, rockX, rockY, rock_sprite);
dbSprite(broken_rock_sprite, rockX,
dbScreenHeight() - dbSpriteHeight(broken_rock_sprite),
broken_rock_image);
dbSprite(rock_cart_sprite, rockCartX, rockCartY, rock_cart_image);
// Refresh the screen
dbSyncOn();
}
// Displays a summary of players performance including points earned
void summaryScreen(int caught, int broken)
{
// Delete the sprites and get them off the screen
deleteSprites();
// Enable auo refresh and clear the screen
dbSyncOff();
dbCLS();
// Set the text size
dbSetTextSize(36);
// Display the text
dbCenterText(dbScreenWidth() / 2, 100, "Game Over!");
dbWait(four_seconds);
// Count the rocks caught
for (int caughtCount = 0; caughtCount <= caught; caughtCount++)
{
// Clear the screen
dbCLS();
dbPasteImage(rock_image, 200, 240);
dbPasteImage(broken_rock_image, 300, 240);
// Display the text
dbCenterText(dbScreenWidth() / 2,
100, "GAME RESULTS");
dbText(240,240,"x");
dbText(380,240,"x");
dbText(260,240,dbStr(caughtCount));
dbText(400,240, dbStr(broken) );
// Play the caught rock sound
dbPlaySound(metal_hit_sound);
dbWait(tenth_second);
// Display score which is 1000 points for a caught rock
dbCenterText(dbScreenWidth() / 2,
400, dbStr(caughtCount * 1000));
dbWaitKey();
}
// If no rocks were broken, display a special message.
if (caught == max_rocks)
{
// Display the perfect score message.
dbCenterText(dbScreenWidth() / 2,
200, "PERFECT SCORE!");
// Wait for one second.
dbWait(tenth_second);
}
// Wait for the player to press a key.
dbWaitKey();
}
//****************************************************
// The deleteSprites function deletes the sprites. *
//****************************************************
void deleteSprites()
{
dbDeleteSprite(stone_bg_sprite);
dbDeleteSprite(cart_sprite);
dbDeleteSprite(rock_sprite);
dbDeleteSprite(broken_rock_sprite);
dbDeleteSprite(rock_cart_sprite);
}
Regards,
SPEED-E