-3

I am experiencing troubles with what I suspect to be while loops. I am working on building a simple game, and I think my two while loops are interfering with eachother somehow. here's the main function! Thanks in Advance!:

int main( int argc, char* argv[])
{
SDL_Startup();

while(Playing == false && quit == false)
{
    StartingScreen.input();
    StartingScreen.render();
    character.input();
    SDL_Flip( screen );
    SDL_Delay(1000/FPS);
}

while(Playing == true && quit == false)
{
    CAMERAGUY.Camera();
    character.input();
    character.adjust();
    SuperSnail.move();
    SuperSnail.attack();
    TheWall.boundaries();
    TheWall.render();
    SuperSnail.render();
    character.render();
    character.reset();
    HUD.render();
    SDL_Flip(screen);
    SDL_Delay(1000/FPS);
    cout << StartingScreen.x << endl;
}
if(Playing == false)
    cout << "Playing == false" << endl;
if(quit == true)  
return 0;
}

So the bool playing is set to false to begin with, and is set to true when my character runs out of lives. so when Playing is set to false in the second loop, it doesn't repeat the first loop. I JUST thought, I think maybe it would work if I put the two loops in a separate loop.

2 Answers2

1

I've edited your code with comments to explain what it is doing (and why it is not behaving as you expect):

int main( int argc, char* argv[])
{
SDL_Startup();

// Set `Playing` to false

while(Playing == false && quit == false)
{
   // Do stuff which applies when `Playing` is false
   //
   // At some point, set `Playing` to true, so that
   // the loop ends
}

while(Playing == true && quit == false)
{
   // Now do stuff which applies when `Playing` is true
   //
   // At some point, set `Playing` to false, so that
   // the loop ends
}

// Both loops have come to an end, and there
// is no code to return to the first loop
// so execution continues below:

// This line always executes because `Playing` is always false
// by this point:
if(Playing == false)
    cout << "Playing == false" << endl;

// This conditional statement doesn't do what you think, because
// in the case that `quit == false`, the end of your `main` function
// is reached and returns 0 by default, meaning that the outcome
// is the same no matter what the value of `quit`

if(quit == true)  
return 0;
}

The solution: enclose the both loops in a while(quit == false) loop. This means that both when the second loop completes, the first loop will be evaluated again, until you are ready to stop execution by setting quit to true.

A few other tips:

  1. A neater way of expressing quit == false is !quit.

  2. A neater way of expressing Playing == true is Playing.

  3. Using global variables in the way that you are is almost certainly a bad idea, and you should probably rethink your design.

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72
  • lol, thanks. but in your 3rd tip, it says "Using global variables in the way that you are is almost certainly a bad idea". I do not display any global variables with exception of bools. – user2949299 Nov 18 '13 at 02:56
0

It is pretty simple. In your current implementation you will never return to your first while loop if your second loop is finished, because you reach the return of your main routine.

I guess you should always use only one main loop for your game.