Well I'm back again, to give programming another shot, but I'm already confused as to why my code isn't working.
//*********************************************************************
//*********************************************************************
// Space Invaders
// Written by Scarface
// in C++ using Dark Basic GDK
// 16th January 2009
//*********************************************************************
//*********************************************************************
#include <DarkGDK.h>
// boundry constants ( setup for 800x600 resolution )
const int LEFT_BOUNDRY = 20;
const int RIGHT_BOUNDRY = 495;
// game speed constants
const float SPEED_ALIEN = 1.2;
const float SPEED_BULLET = 4.0;
const float SPEED_SHIP = 5.0;
// function declarations
void move_ship( );
bool move_aliens( int xdir );
void move_bullet ( );
// main
void DarkGDK( void )
{
// display setup
dbSyncOn( );
dbSyncRate( 60 );
dbSetWindowSize( 800, 600 );
// initialize variables
int x = 260;
int y = 380;
int x_dir = 0;
// load sound effects
dbLoadSound( "lazer.wav", 1 );
dbLoadSound( "explode.wav", 2 );
// create background
dbLoadImage ( "backdrop.bmp", 1 );
dbSprite ( 1, 0, 0, 1 );
// create player controlled ship
dbCreateAnimatedSprite( 2, "ship.png", 1, 1, 2);
dbSprite( 2, x, y, 2 );
// create the aliens and position them
for ( int obj = 3; obj <= 11; obj ++ )
{
switch ( obj )
{
// positions
case 1: x = 20; y = 20; break;
case 2: x = 180; y = 20; break;
case 3: x = 340; y = 20; break;
case 4: x = 20; y = 90; break;
case 5: x = 180; y = 90; break;
case 6: x = 340; y = 90; break;
case 7: x = 20; y = 160; break;
case 8: x = 180; y = 160; break;
case 9: x = 340; y = 160; break;
}
dbCreateAnimatedSprite( obj, "alien.png", 1, 1, obj );
dbSprite( obj, x, y, obj );
}
// main loop
while( LoopGDK( ) )
{
// function calls
x_dir = move_aliens( x_dir );
move_ship( );
move_bullet( );
// sync display
dbSync( );
// if esc is press, exit loop
if ( dbEscapeKey ( ) )
break;
}
// cleanup memory
for ( int obj = 1; obj <= 12; obj ++ )
dbDeleteSprite( obj );
// end program
return;
}
// function to move the player controlled ship
void move_ship( )
{
// setup vars for ship movement
int x = dbSpriteX( 2 );
int y = dbSpriteY( 2 );
// left key is being pressed
if ( dbLeftKey( ) )
{
// if the ship has neeed exceeded the allowed boundry, then move ship to the left
if ( x > LEFT_BOUNDRY )
dbSprite( 2, x - SPEED_SHIP, y, 2 );
}
else if ( dbRightKey( ) ) // right key is being pressed
{
// if the ship has neeed exceeded the allowed boundry, then move ship to the right
if ( x < RIGHT_BOUNDRY )
dbSprite( 2, x + SPEED_SHIP, y, 2 );
}
}
// this function will move the aliens
// returns the current movement direction: 0 = right, 1 = left
bool move_aliens( int x_dir )
{
// initialize variables
int x = 0;
int y = 0;
int x_offset = 320;
int move_y = false;
// if alien is moving left
if ( x_dir == 1 )
{
// loop through and reposition aliens
for ( int obj = 3; obj <= 11; obj ++ )
{
// get alien position
x = dbSpriteX( obj );
y = dbSpriteY( obj );
// alien is at boundry
if ( obj = 3 && x <= LEFT_BOUNDRY )
{
x = LEFT_BOUNDRY;
move_y = true;
}
// alien should be moved along down it's y xoordinates
if ( move_y )
{
y += SPEED_ALIEN * 4;
x_dir = 0;
}
else // move alien along x coordinate
{
x -= SPEED_ALIEN;
}
// redraw alien
dbSprite( obj, x, y, obj );
}
}
else if ( x_dir == 0 ) // if alien is moving right
{
// loop through and reposition aliens
for ( int obj = 3; obj <= 11; obj ++ )
{
// get alien position
x = dbSpriteX( obj );
y = dbSpriteY( obj );
// alien is at boundry
if ( obj = 3 && x >= RIGHT_BOUNDRY - x_offset )
{
x = RIGHT_BOUNDRY - x_offset;
move_y = true;
}
// alien is to be moved down the y coordinate
if ( move_y )
{
y += SPEED_ALIEN * 4;
x_dir = 0;
}
else // move alien along x coordinate
{
x += SPEED_ALIEN;
}
// redraw alien
dbSprite( obj, x, y, obj );
}
}
return x_dir;
}
void move_bullet ( )
{
// initialize vars
int x = 0;
int y = 0;
int x_offset = 46;
// bullet already exists ( only 1 bullet at a time ^^ )
if ( dbSpriteExist( 12 ) && dbSpriteVisible ( 12 ) )
{
// define vars for bullet movement
x = dbSpriteX( 12 );
y = dbSpriteY( 12 ) - SPEED_BULLET;
// if the bullet has gone out of boundries, hide it and put it into void position
if ( y < 20 )
{
dbHideSprite( 12 );
y = 450;
}
// loop through aliens and see if one was hit
for ( int obj = 3; obj <= 11; obj ++ )
{
// if an active alien is hit
if ( dbSpriteCollision( 12, obj ) && dbSpriteVisible ( obj ) )
{
// hide the bullet & the hit alien
dbHideSprite( 12 );
dbHideSprite( obj );
dbPlaySound( 2 );
y = 450;
}
}
}
else if ( dbSpaceKey( ) ) // new bullet fired!
{
// set bullet's starting y position
y = 380;
// check if the bullet has already been created
if ( dbSpriteExist( 12 ) )
{
// bullet already exists, just make it visible
dbShowSprite( 12 );
}
else
{
// create the bullet
dbCreateAnimatedSprite( 12, "bullet.png", 1, 1, 12 );
// declare position vars
x = dbSpriteX( 2 ) + x_offset;
y = dbSpriteY( 2 );
}
// sound effects
dbPlaySound( 1 );
}
else
return; // nothing, end function
// set bullet's position
dbSprite( 12, x, y, 12 );
}
Basically I've come to the conclusion that the game crashes when it gets to alien movement function call in the main loop:
x_dir = move_aliens( x_dir );
But I've looked over and over the function many times and don't see any reason for it to crash the game, the loop only reiterates 11 times...
Any advice would be helpful.
- Scarface