--- froggix/src/froggix.c 2009/03/28 03:48:45 1.1 +++ froggix/src/froggix.c 2009/04/11 13:01:54 1.5 @@ -25,7 +25,7 @@ /* * Set some basic definitions */ -#define VERSION "$Id: froggix.c,v 1.1 2009/03/28 03:48:45 nick Exp $" +#define VER "$Id: froggix.c,v 1.5 2009/04/11 13:01:54 nick Exp $" #define TITLE "Froggix" #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 @@ -60,13 +60,15 @@ #define SCORE_PINK 200 #define SCORE_SECONDS 10 #define HIGH_SCORE 4630 -#define SCORE_FREE_FROG 200 +#define SCORE_FREE_FROG 2000 /* The green game timer */ #define MAX_TIMER 350 #define TIMER_SIZE 150 #define TIMER_COLOR 32, 211, 0 #define TIMER_LOW_COLOR 255, 0, 0 +#define FLY_MAX_TIME 50 +#define GATOR_MAX_TIME 70 /* baddies */ #define VEHICLE 0 @@ -156,6 +158,7 @@ typedef struct { int type; /* SHORT, MEDIUM, or LONG */ int speed; /* What speed does the log move at */ int hasPink; // Is bonus frog riding + int isGator; /* Are we a gator? if > 1 we have an open mouth */ SDL_Rect src; } logObj; @@ -184,6 +187,8 @@ void beginGame( void ); int loadMedia( void ); int heartbeat( void ); int updateGameState( void ); +void checkFly( void ); +void checkGator( void ); void configGameScreen( void ); void drawGameScreen( void ); void drawBackground( void ); @@ -219,6 +224,8 @@ void drawImage(SDL_Surface *srcimg, int void playSound( Mix_Chunk *sound ); void setFullScreenMode( void ); +int flyTimer = 0; +int gatorTimer = 0; int level = 0; int playing = 0; int lives = 0; @@ -331,7 +338,6 @@ int loadMedia( void ) { * Load frogger's textures and sounds */ gfx = IMG_Load( "images/frogger.png" ); - if ( gfx == NULL ) { fprintf( stderr, "Error: 'images/frogger.bmp' could not be open: %s\n", SDL_GetError( ) ); result--; @@ -452,10 +458,12 @@ int keyEvents( SDL_Event event ) { default: break; } +/* Uncomment for positioning debug information printf( "x,y,d => %i,%i,%i,%i\n", frogger.placement[X], frogger.placement[Y], frogger.direction, frogger.currentRow ); +*/ } /* Game over man, game over! */ if ( ! lives ) { @@ -493,9 +501,8 @@ int keyEvents( SDL_Event event ) { } int updateGameState( void ) { -/* - printf( "D: Updating game state\n" ); -*/ + int i; + if ( ! drawBG ) configGameScreen( ); if ( lives <= 0 ) { @@ -507,6 +514,11 @@ int updateGameState( void ) { playing = 0; lives = 0; level = 0; + score = 0; + givenFreeFrog = 0; + drawBG = 0; + for ( i = 0; i < MAX_GOALS; i++ ) { goals[i].occupied = 0; } + } return 500; } @@ -544,6 +556,7 @@ logObj setWood( int type, int speed, int tempWood.src.x = imgPixelSrc; tempWood.src.w = FRAME * tempWood.type; tempWood.src.h = FRAME; + tempWood.isGator = 0; return tempWood; } @@ -699,6 +712,10 @@ void configGameScreen( void ) { rightBorderRect.w = SCREEN_WIDTH - RIGHT_SIDE; rightBorderRect.h = SCREEN_HEIGHT; + /* + * We reset the flyTimer since this may not be the first game played + */ + flyTimer = 0; /* * Draw frogger in starting position @@ -742,6 +759,8 @@ void drawGameScreen( void ) { /* * Update and draw everthing else */ + checkFly( ); + checkGator( ); drawImage( background, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, screen, 0, 0, 255 ); drawScore( 0, score ); drawScore( 1, hScore ); @@ -753,6 +772,7 @@ void drawGameScreen( void ) { drawVehicles( ); if ( frogger.alive == FALSE ) { + frogger.riding = FALSE; if ( ! drawDeathSequence( frogger.deathType ) ) { lives--; if ( lives < 0 ) { drawGameOver( ); } @@ -791,6 +811,72 @@ int getRowPixel ( int row ) { } /* + * Check our fly timers to determine if we need to display or + * remove a fly from the goal area + */ +void checkFly ( void ) { + int i; + + for ( i = 0; i < MAX_GOALS; i++ ) { + if ( goals[i].fly ) { + goals[i].fly++; + if ( goals[i].fly > FLY_MAX_TIME ) { + goals[i].fly = 0; + flyTimer = 0; + } + + return; + } + } + + flyTimer++; + + if ( flyTimer > ( FLY_MAX_TIME ) ) { + int randGoal = ( ( int ) timeLeft % 10 ); + if ( ( goals[randGoal].gator == 0 ) && + ( goals[randGoal].occupied == 0 ) ) { + printf( "Displaying fly in goal %i\n", randGoal ); + goals[randGoal].fly = 1; + } + } + + return; +} + +/* + * Check our gator timers. Similiar to fly timers above, however, the gator + * has an extra stage as it enters in to the goal area. + */ +void checkGator ( void ) { + int i; + + for( i = 0; i < MAX_GOALS; i++ ) { + if ( goals[i].gator ) { + goals[i].gator++; + if ( goals[i].gator > GATOR_MAX_TIME ) { + goals[i].gator = 0; + gatorTimer = 0; + } + + return; + } + } + + gatorTimer++; + + if ( gatorTimer > GATOR_MAX_TIME ) { + int randGoal = ( ( int ) ( timeLeft * 2 ) % 10 ); + if ( ( goals[randGoal].fly == 0 ) && + ( goals[randGoal].occupied == 0 ) ) { + printf( "Displaying gator in goal %i\n", randGoal ); + goals[randGoal].gator = 1; + } + } + + return; +} + +/* * This does collision detection based on the row frogger * is in to help reduce overhead */ @@ -847,6 +933,11 @@ int collisionRow ( void ) { if ( collideFrogger( goals[i].x, goals[i].y, goals[i].w, goals[i].h ) ) { if ( goals[i].occupied ) return 1; goals[i].occupied++; + if ( goals[i].fly ) { + goals[i].fly = 0; + flyTimer = 0; + score += SCORE_FLY; + } /* playSound( s_goal ); */ score += SCORE_GOAL; score += ( ( int ) ( timeLeft / 10 ) ) * 10; @@ -972,36 +1063,29 @@ void froggerReset ( void ) { * algorithm for calculating the distance and time */ void moveFrogger( void ) { - int currentFrame = 0; int x = 0; int y = 0; int h = FRAME; int w = FRAME; - int frameLow = HOP_SPEED / 3; - int frameHigh = frameLow * 2; - - /* Determine which frame of frogger to display */ - if ( ( frogger.hopCount >= frameLow ) && ( frogger.hopCount <= frameHigh ) ) - currentFrame = FRAME; frogger.oldPlacement[Y] = frogger.placement[Y]; frogger.oldPlacement[X] = frogger.placement[X]; switch( frogger.direction ) { case UP: - x = currentFrame; + x = FRAME; frogger.placement[Y] -= ( HOP_DISTANCE / HOP_SPEED ); break; case DOWN: - x = currentFrame + ( 4 * FRAME ); + x = ( 5 * FRAME ); frogger.placement[Y] += ( HOP_DISTANCE / HOP_SPEED ); break; case LEFT: - x = currentFrame + ( 6 * FRAME ); + x = ( 7 * FRAME ); frogger.placement[X] -= ( HOP_DISTANCE / HOP_SPEED ); break; case RIGHT: - x = currentFrame + ( 2 * FRAME ); + x = ( 3 * FRAME ); frogger.placement[X] += ( HOP_DISTANCE / HOP_SPEED ); break; } @@ -1031,6 +1115,7 @@ void moveFrogger( void ) { frogger.direction = FALSE; score += SCORE_HOP; lives += freeFrog( score ); + frogger.src.x -= FRAME; } } @@ -1186,7 +1271,7 @@ void drawNumbers( int num, int x, int y void drawGoals( void ) { int drawDebugRect = 0; int i; - + for ( i = 0; i < MAX_GOALS; i++ ) { if ( drawDebugRect ) { SDL_Rect d; @@ -1201,6 +1286,19 @@ void drawGoals( void ) { if ( goals[i].occupied ) drawImage( gfx, FRAME * 15, 0, FRAME, FRAME, screen, goals[i].x + 13, goals[i].y + 5, 255 ); + if( goals[i].fly ) { + drawImage( gfx, FRAME * 16, 0, FRAME, FRAME, + screen, goals[i].x + 13, goals[i].y + 5, 255 ); + } + if( goals[i].gator ) { + int frame = 17; + + if ( goals[i].gator > ( ( int ) GATOR_MAX_TIME / 2 ) ) + frame++; + + drawImage( gfx, FRAME * frame, 0, FRAME, FRAME, + screen, goals[i].x + 13, goals[i].y + 5, 255 ); + } } }