Thread: "try again" loop not working

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    "try again" loop not working

    I can't seem to make a loop that will work if the user wants to try the game again. Maybe I'm just having a brain fart. Here is what I have. I'm using n as my check value, or the value to stop the game when the player wants to quit. If they don't want to quit, I want it to loop back to the beginning.

    Code:
    int main(){
    
        allegro_init();
        install_keyboard();
        set_color_depth(16);
        set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
        
        while(!key[KEY_ESC] && n == 0){
        
        while(!key[KEY_ESC] && n == 0){
                                     
        clear_keybuf();
        
        acquire_screen();
        
        textout_ex(screen, font, "   ", x2, y2, makecol( 0, 0, 0), makecol( 0, 0, 0));
        
        if(key[KEY_W]) --y2;
        if(key[KEY_S]) ++y2;
        if(key[KEY_D]) ++x2;
        if(key[KEY_A]) --x2;
        
        textout_ex(screen, font, "<^>", x2, y2, makecol(225,0,0), makecol(0,0,0));
        
        release_screen();
        
        rest(10);
         
        moveCircle();
        
        distant = distance(x2, y2, x, y);
        
        if(distant < 10 ) n = 1;
       
        }
        
        while(!key[KEY_SPACE]){
        
            textout_ex(screen, font, "Good job, you caught the circle! (hit space)", 100, 
            100, makecol(255, 0, 0), makecol( 0, 0, 0));
        }
        
        if(n == 1) {
             while(!key[KEY_A]) {
                 textout_ex(screen, font, "Press 'a' to play again", 100, 100,
                                makecol(255, 0, 0), makecol( 0, 0, 0));
             n = 0;
             
             }
             while(!key[KEY_ESC] || !key[KEY_SPACE]) {
                 textout_ex(screen, font, "Press ESC to exit, or space to play", 100, 100,
                                    makecol(255, 0, 0), makecol( 0, 0, 0));
                 
                 }
             
             }
       
      }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Ok I got it, but with some flaws. I want it to start over from the very beginning where the circle object starts in one corner and the arrows start in another. Oppose to having it just continue itself from where it left off. I also want to get rid of the lettering that shows up once the game is won.

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
             while(!key[KEY_A]) {
                 textout_ex(screen, font, "Press 'a' to play again", 100, 100,
                                makecol(255, 0, 0), makecol( 0, 0, 0));
             n = 0;
             
             }
             while(!key[KEY_ESC] || !key[KEY_SPACE]) {
                 textout_ex(screen, font, "Press ESC to exit, or space to play", 100, 100,
                                    makecol(255, 0, 0), makecol( 0, 0, 0));
                 
                 }
    Besides being indented poorly, you need to clearly provide one key to quit and one key to play again.
    Code:
    //Assuming Y to play again, N to quit     
    textout_ex(screen, font, "Press 'y' to play again or 'n' to quit", 100, 100,
         makecol(255, 0, 0), makecol( 0, 0, 0));
    while (true)
    {
         if (key[KEY_Y])
         {
              n = 1;
              break;
         }
         if (key[KEY_N)
         {
              n = 0;
              break;
         }
    }
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    My indenting isn't the greatest, but I'm not that worried about it right now. I made a case similar to the one you gave, although your's is easier. It restarts and quits just fine now, but I want it to restart from the beginning, and not where it left off from the last game. I read some other threads about redrawing the screen over again, how is that done?

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Scratch that. I just moved the cursor to a random area when a new game starts.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by scwizzo View Post
    My indenting isn't the greatest, but I'm not that worried about it right now.
    If you make your code easier to read, it's easier to figure out where you've messed up.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my do while loop not working
    By rodrigorules in forum C Programming
    Replies: 12
    Last Post: 09-07-2005, 06:52 PM
  2. Replies: 6
    Last Post: 07-19-2005, 01:03 PM
  3. EOF not working in a loop
    By Malabux in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2003, 06:28 PM
  4. while() loop isn't working right..
    By Captain Penguin in forum C++ Programming
    Replies: 20
    Last Post: 10-03-2002, 10:29 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM