Skip to content

Commit

Permalink
Fix new game bug whereby old snakes wouldn't be deleted and cause pro…
Browse files Browse the repository at this point in the history
…blems. Less memory leaks!

git-svn-id: file:///Users/tkieft/code/SVN/snake/trunk@63 2d1a4ccd-f515-0410-a99d-d1a2ee3908d6
  • Loading branch information
tkieft committed Apr 21, 2007
1 parent d33d987 commit 60e803f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
16 changes: 8 additions & 8 deletions Snake/Board.cpp
Expand Up @@ -37,7 +37,7 @@ using std::endl;
const int Board::STARTING_POSITION[] = { ( LEVELSIZE * (LEVELSIZE - 1) ) + LEVELSIZE / 2, LEVELSIZE / 2 };
const int Board::ENDING_POSITION[] = { Board::STARTING_POSITION[1], Board::STARTING_POSITION[0] };

Board::Board() : currentLevel( 0 ) {
Board::Board() : currentLevel( 0 ), deadPlayer( 1 ) {
playerFont[0] = NULL;
playerFont[1] = NULL;
srand( time( NULL ) );
Expand Down Expand Up @@ -263,8 +263,7 @@ void Board::drawSnakeInfo( SDL_Surface* scr, SnakePlayer* snakes[] )

}

void Board::drawHelp( SDL_Surface* scr )
{
void Board::drawHelp( SDL_Surface* scr ) {
SDL_Color helpColor; helpColor.r = helpColor.g = helpColor.b = 0xFF;

char* pauseHelp = "'P' to Pause";
Expand All @@ -282,7 +281,8 @@ void Board::drawHelp( SDL_Surface* scr )

void Board::drawLevelLostInfo( SDL_Surface* scr ) {
drawBorderedRect( scr );
drawSmallText( "Player ? Lost", scr, "middle1" );
char text[] = { 'P', 'l', 'a', 'y', 'e', 'r', ' ', ((char)deadPlayer) + 48, ' ', 'D', 'i', 'e', 'd', '!', '\0' };
drawSmallText( text, scr, "middle1" );
drawSmallText( "Press space to restart", scr, "middle2" );
drawSmallText( "the current level", scr, "middle3" );
}
Expand Down Expand Up @@ -326,7 +326,7 @@ int Board::updatePosition( SnakePlayer* snakes[] )
if( --collectibles) placeCollectible();
}
else
return i+1;
return deadPlayer = i + 1;
}
snakeHeadPosition[i] -= LEVELSIZE;
levelData[snakeHeadPosition[i]] = ++snakeHead[i];
Expand All @@ -342,7 +342,7 @@ int Board::updatePosition( SnakePlayer* snakes[] )
if( --collectibles) placeCollectible();
}
else
return i+1;
return deadPlayer = i + 1;
}
snakeHeadPosition[i] += LEVELSIZE;
levelData[snakeHeadPosition[i]] = ++snakeHead[i];
Expand All @@ -358,7 +358,7 @@ int Board::updatePosition( SnakePlayer* snakes[] )
if( --collectibles) placeCollectible();
}
else
return i+1;
return deadPlayer = i + 1;
}
levelData[--snakeHeadPosition[i]] = ++snakeHead[i];
break;
Expand All @@ -373,7 +373,7 @@ int Board::updatePosition( SnakePlayer* snakes[] )
if( --collectibles) placeCollectible();
}
else
return i+1;
return deadPlayer = i + 1;
}
levelData[++snakeHeadPosition[i]] = ++snakeHead[i];
break;
Expand Down
1 change: 1 addition & 0 deletions Snake/Board.h
Expand Up @@ -40,6 +40,7 @@ class Board
SDL_Color FLOOR_COLOR;

int currentLevel;
int deadPlayer;
int* levelData;
string levelName;
string levelLocation;
Expand Down
24 changes: 16 additions & 8 deletions Snake/SPlayState.cpp
Expand Up @@ -6,6 +6,7 @@
* Copyright 2006 Tyler Kieft. All rights reserved.
*
* CHANGELOG:
* 21Apr07 TDK Fix nasty bug where restarting game would cause errors because snakes hadn't been deleted
* 15Jun06 TDK gameState variable to hold state.
* 24Jun06 TDK Move SDL_Flip below Unlock screen.
* 17Jun06 TDK New Code.
Expand All @@ -20,14 +21,12 @@

SPlayState SPlayState::snakePlayState;

SPlayState::SPlayState() : theBoard(), theTimer()
{
SPlayState::SPlayState() : theBoard(), theTimer() {
theSnakes[0] = NULL;
theSnakes[1] = NULL;
}

void SPlayState::Init( SGameEngine* game )
{
void SPlayState::Init( SGameEngine* game ) {
game->startPlaying();

// load up background
Expand All @@ -36,9 +35,19 @@ void SPlayState::Init( SGameEngine* game )
// create snake(s)
SDL_Color c1 = { 0x00, 0x88, 0x00 };
SDL_Color c2 = { 0x00, 0x33, 0x22 };
theSnakes[0] = new SnakePlayer( c1, "Tyler", SnakePlayer::SNAKE_UP, game->getGameDiff() );

// clean up a possible previous game
if( theSnakes[0] )
delete theSnakes[0];
if( theSnakes[1] ) {
delete theSnakes[1];
theSnakes[1] = NULL;
}

theSnakes[0] = new SnakePlayer( c1, "Tyler", SnakePlayer::SNAKE_UP, game->getGameDiff() );
if( game->getNumPlayers() == 2 )
theSnakes[1] = new SnakePlayer( c2, "Brandon", SnakePlayer::SNAKE_DOWN, game->getGameDiff() );
theSnakes[1] = new SnakePlayer( c2, "Brandon", SnakePlayer::SNAKE_DOWN, game->getGameDiff() );

// set up board and timer
theBoard.Init( game->getFileDirectory(), game->getNumPlayers() );
theBoard.setLevel( 1, game->getNumPlayers() );
Expand All @@ -47,8 +56,7 @@ void SPlayState::Init( SGameEngine* game )
gameState = LEVEL_START;
}

void SPlayState::Cleanup()
{
void SPlayState::Cleanup() {
// cleanup members
theBoard.Cleanup();
theTimer.Cleanup();
Expand Down

0 comments on commit 60e803f

Please sign in to comment.