Thread: my game over function is not working

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    my game over function is not working

    hey guys, I have been all over the net trying to find why my game over fuction is not being called. here is the main bulk of the code, and under it is the game over function which is a seprate source file, but they are both compiled and linked together ok without any errors. When the int health reaches 0, it should call gameover(), but it just carrys on regardless. please take a look and let me know of anyway i can fix it: the code is pretty long, the gameover() call is in main(). thanks in advance!

    Code:
    /* Main File for Wizadora
    
       Written by Peter Watts
       DO NOT EDIT OR DELETE THIS CODE!!!*/
    
    
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <cctype>  // files used
    #include <windows.h>
    #include <algorithm>
    #include <conio.h>
    
    
    using namespace std; // for cout and endl;
    
    void AltEnter(); // TO QUIT GAME HOLD CONTROL THEN PRESS C //
    void intro();
    void level1();
    void plea();
    void wizavillage();
    void signpost();    // functions are delcared
    void magicshop();
    void oldchurch();
    void creepywoods();
    void woodenhut();
    void endingANDcredits();
    extern void gameover();
    extern void stats();
    extern void magics(); // external functions to call when needed
    extern void spells();
    
    /* Player Varibles*/
    string pname; // player name
    int health = 100; // player health;
    int MAXhealth = 500; // player maximum health
    int MINhealth = 1; // player minimum health
    int gold = 0; // gold player has gathered
    int Exp = 0; // experience gained
    int powers = 0; // number of magic spells learned
    string spellname; // string fof spell name
    int level = 1; // player level
    int items = 0; // items found
    
    /* Enemy Varibles*/
    string enemyname; // string for enemy name
    int ehealth; // enemy health
    int edamage; // enemy damage
    int elevel; // enemy level;
    bool edead; // booleen varible to decide is enemy defeated or active
    bool ealive; // "" 
    
    /* Misc Vsribles*/
    int size;
    int x; // special varibles used in text scroll
    char attack; // used in battle
    char choice; // used in selection
    char magic; // used in magic
    
    int main(int argc, char *argv[])
    {
    
    SetConsoleTitle("W I Z A D O R A - BY PETER WATTS -"); // console Title
    AltEnter();
    intro();
    level1();
    plea();
    wizavillage();
    signpost();  // Set functions to MAIN() 
    magicshop();
    oldchurch();
    creepywoods();
    woodenhut();
    endingANDcredits();
    
    if(health<=0) gameover(); // call gameover FROM main
    if(powers=+1) spells();   // call spells FROM main
    
    
    return 0;
    
    } // end main
    
    void AltEnter() // call keyboard events
    {
    keybd_event(VK_MENU, 0x38, 0, 0);
    keybd_event(VK_RETURN, 0x1c, 0, 0);
    keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0); // this brill function makes the
    keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);   // console window fill the screen
    intro();                                          // just like in a real time game!!
    return;
    }
    void intro()
    {
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
    GetConsoleScreenBufferInfo(h, &csbiInfo);
    wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
    SetConsoleTextAttribute ( h, FOREGROUND_GREEN | FOREGROUND_INTENSITY );
      
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED| FOREGROUND_GREEN | FOREGROUND_INTENSITY );
    cout << "\n\n";
    cout <<"\t\t=============================================\n";       
    printf("\t\t#   # # #####     #### ####  #### ####   ####\n"
           "\t\t#   # #     #     #  # #   # #  # #   #  #  #\n"
           "\t\t#   # #     #     #  # #   # #  # ####   #  #\n"
           "\t\t# # # #     #     #### #   # #  # # #    ####\n"
           "\t\t# # # #     #     #  # #   # #  # #  #   #  #\n"
           "\t\t##### #     ##### #  # ####  #### #   #  #  #\n"
           "\t\t=============================================\n"
           "\t\t|                                           | \n"
           "\t\t|        Press P to Play A New Game         | \n"
           "\t\t|                                           | \n"
           "\t\t|        Press L to Load A Saved Game       | \n"
           "\t\t|                                           | \n"
           "\t\t|        Press Q to Quit the Game           | \n"
           "\t\t--------------------------------------------- \n");
    cout << "\n\n";
    cout << "\tSelection >";
    cin >> choice;  
    
    if (choice == 'l')
    {
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
    GetConsoleScreenBufferInfo(h, &csbiInfo);
    wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
    SetConsoleTextAttribute ( h, FOREGROUND_RED|FOREGROUND_INTENSITY );
      
    system("cls");
    printf("The load feature is not yet avalible...");
    Sleep(2000);
    system("cls");
    intro();
    } 
    
    else if (choice == 'q')
    {
    system("cls");
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
    GetConsoleScreenBufferInfo(h, &csbiInfo);
    wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
    SetConsoleTextAttribute ( h, FOREGROUND_BLUE|FOREGROUND_INTENSITY );
    
    printf("Goodbye then, please come back to save the kingdom soon...\n");
    Sleep(3000);
    exit(0);
    } 
    
    else if (choice == 'p')
    {
    system("cls");
    SetConsoleTextAttribute ( h, wOldColorAttrs);
    cin.ignore();
    level1();
    }
    
    else {
    system("cls");
    printf("You must enter one of the three commands!\n");
    Sleep(2000);
    system("cls");
    intro();
    }
    } // end intro
    
    
    
    void level1()
    {
    system("cls");
    char senta[]="Hail adventurer!\nDo you seek glory, fame, fortune\nand even gold?\n"
                 "Then I welcome you to my land of magic and fantasy...\nI am Wizadora\n"
                 "and I need your help....\n";
                 
                 size=strlen(senta);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senta[x]);
                 }
                 Sleep(2000);
    
    system("cls");
    char sentb[]="But before you can start, I need to know your name.\nAs I cannot keep\n"
                 "calling you adventurer now can I?\n"
                 "\n\n"
                 "My name is: ";
                 
                 size=strlen(sentb);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentb[x]);
                 }
                 getline ( cin, pname );
                 
    char sentc[]="\n\n"
                 "So, your name is ";
                 
                 size=strlen(sentc);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentc[x]);
                 }
                 cout<<""<< pname <<""<<endl;             
                 
    char sentd[]="Thats is a nice name!\nI hope we can get on well together...\n"
                 "Would you like to hear my story of why I have come to ask for your\n"
                 "aid ";
                 
                 size=strlen(sentd);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentd[x]);
                 }
                 cout<<""<< pname <<"?"<<endl;
                 printf("\n\n"
                        "Press Y to hear her plea or N to continue anyway\n"
                        "\n\n");
                 cout << "Will you? ";
                 cin >> choice;
                
    
    if (choice == 'n')
    {
    system("cls");
    wizavillage();
    }
    
    else if (choice == 'y')
    {
    system("cls");
    plea();
    }
    
    else {
    printf("\n\n"
           "You must enter yes or no...");
           Sleep(2000);
           level1(); // loop back to start of game to make sure question is aswered
           }
           } // end of level1
           
    void plea()
    {
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
    GetConsoleScreenBufferInfo(h, &csbiInfo);
    wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
    SetConsoleTextAttribute ( h, FOREGROUND_GREEN|FOREGROUND_INTENSITY );
    system("cls");
    char sentmp[]="The land of Wiza, which is basicly a land of child fantasy\n"
                  "has always been a peaceful land, where young trainee wizards\n"
                  "and witches roam, and practice their magic in the hope that\n"
                  "one day, they can use it to help bring peace to the world...\n"
                  "\n\n";
                  
                  size=strlen(sentmp);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentmp[x]);
                 }
                 Sleep(2000);
                 system("cls");
    
    char sentmpa[]="But one day, the evil witch DRUCILA cast an evil spell\n"
                   "on all the people who live in Wiza.\n"
                   "Her plan is to make all the people her slaves, so she can\n"
                   "have enough power to conquer the WHITE WILLOW, which is the\n"
                   "source of all our powers, and turn it evil...\n"
                   "\n\n";
                   
                   size=strlen(sentmpa);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentmpa[x]);
                 }
                 Sleep(2000);
                 system("cls");
                 
    char sentmpb[]="But hope is not lost....\n"
                   "\n\n"
                   "I have been instructed by the WHITE WILLOW to challenge\n"
                   "DRUCIA and break her spell that she has over us.\n"
                   "I am all but an amatuer witch at the moment, that is why\n"
                   "I need your help to battle the monsters that DRUCILA has\n"
                   "crafted into Wiza...\n"
                   "I know if we work as a team, we can defeat DRUCILA and restore\n"
                   "peice to Wiza!\n"
                   "\n\n";
                   
                   size=strlen(sentmpb);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentmpb[x]);
                 }
                 Sleep(2000);
                 system("cls");
                 SetConsoleTextAttribute ( h, wOldColorAttrs);
                 wizavillage();
                 } // end of plea
    
    void wizavillage()
    {
    char sente[]="\tWIZA VILLAGE\n"
                 "\n\n"
                 "Welcome to my village.... Hmm, I guess it would be a good\n"
                 "Idea to stock up on magic or supplies before we venture into\n"
                 "The woods ahead, Ok then, were to first ";
                     
                 size=strlen(sente);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sente[x]);
                 }
                 cout<<""<< pname <<"?"<<endl;
                 Sleep(2000);
                 system("cls");
                 signpost();
                 } // end function WizaVillage()
                 
    void signpost()
    {
    
    system("cls");
    char sentf[]="Please Select a destination then hit ENTER\n"
                 "\n\n"
                 "Magic Shop ==  Press M\n"
                 "\n\n"
                 "Go to Wiza Woods == Press W\n"
                 "\n\n"
                 "Old Church == Press C\n"
                 "\n\n"
                 "View Stats == Press S\n"
                 "\n\n";
                 
                  size=strlen(sentf);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentf[x]);
                 }
                 
                 cout << "\tSelection: ";
                 cin >> choice;
                             
    if (choice == 's')
                 {
                 stats(); // call STATS()
                 return signpost(); // return to this point after system("pause")
                 }
                 
    else if (choice == 'w')
    {
    system("cls");
    creepywoods();
    }
    
    else if (choice == 'm')
    { 
    system("cls");
    magicshop();
    }
    
    else if (choice == 'c')
    {
    system("cls");
    oldchurch();
    }
    
    else {
    system("cls");
    signpost();
    }
    } // end signpost 
    
    int purchase; // varible used to shop
    
    void magicshop()
    {
    system("cls");
    if (items <=0)
    {
    system("cls");
    char sentmsl[]="It appears that the magic shop is locked, Maybe we need a key\n"
                    "I am sure there is one somwhere....\n";
                    
                    size=strlen(sentmsl);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentmsl[x]);
                 }
                 Sleep(2000);
                 system("cls");
                 signpost();
                 }
    
    else if (items == 1)
    {
    items=items-1; // lose key from stats
    system("cls");
    char sentmso[]="\t\tM A G I C  S H O P\n"
                   "\n\n"
                   "A cheery looking woman  looks at you from behind the counter.\n"
                   "She seems a little young to be running a magic shop,\n"
                   "but you do not let this bother you as you aproach her.\n"
                   "\n\n";
                   
                   size=strlen(sentmso);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentmso[x]);
                 }
                 Sleep(2000);
                 system("cls");
                 
    cout<<""<< pname <<""<<endl;
    
    char sentta[]="\n\n"
                  "Erm, hello there....\n"
                  "I am traveling on an adventure to save the kingdom from Drucila's\n"
                  "evil spell, and Wizadora and myself would like to purchase some\n"
                  "magical supplies if that is ok....\n"
                  "\n\n";
                  
                  size=strlen(sentta);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentta[x]);
                 }
                 Sleep(1000);
                 
    printf("ASSISTANT:");
    char senttb[]="\n\n"
                  "Of course you can!\n"
                  "Providing you have the correct gold on you.\n"
                  "Here is a list of what I sell, and how much gold it will cost...\n"
                  "\n\n"
                  "NAME OF ITEM   ------       COST  ------          TO PURCHASE\n"
                  "\n\n"
                  "Complete Heal                20 gold              Press 1\n"
                  "\n\n"
                  "Magical Mushrooms            10 gold              Press 2\n"
                  "\n\n"
                  "Stone of Wonder              50 gold              Press 3\n"
                  "\n\n"
                  "Invisibilty Cloak            100 gold             Press 4\n"
                  "\n\n"
                  "TO QUIT SHOPPING PRESS 5\n"
                  "\n\n";
                  
                  size=strlen(senttb);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senttb[x]);
                 }
                 
                 cout << "\tWhat will it be? ";
                 cin >> purchase;
                 }
                 
    switch (purchase) { // I use a switch loop here as it is easier to control
    
    case 1:
    if (gold <=19)
    {
    system("cls");
    printf("You cannot afford that Item yet!");
    Sleep(2000);
    system("cls");
    magicshop();
    }
    else if (gold >=20)
    {
    gold=gold-20;
    powers=+1;
    system("cls");
    printf("You have got a new spell!");
    Sleep(2000);
    system("cls");
    magicshop();
    }
    
    else {
    system("cls"); // ELSE is used as a catch fall to avoid errors
    magicshop();
    }
    break;
    
    case 2:
    if (gold <=9)
    {
    system("cls");
    printf("You cannot afford this yet!");
    Sleep(2000);
    system("cls");
    magicshop();
    }
    
    else if (gold >=10)
    {
    items=+1;
    gold=gold-10;
    system("cls");
    printf("You have gained a new item:  MAGIC MUSHROOMS!! (maybe sombody could\n"
           "cook them...)");
    Sleep(2000);
    system("cls");
    magicshop();
    }   
    
    else {
    system("cls"); // ELSE is used as a catch fall to avoid errors
    magicshop();
    }
    break;
    
    case 3:
    if (gold <=49)
    {
    system("cls");
    printf("Sorry, not enough gold...");
    Sleep(2000);
    system("cls");
    magicshop();
    }
    
    else if (gold >=50)
    {
    items=+1;
    gold=gold-50;
    system("cls");
    printf("You have found the Stone of Wonder!!!  What a glowing delight!");
    Sleep(2000);
    system("cls");
    magicshop();
    }
    
    else {
    system("cls"); // ELSE is used as a catch fall to avoid errors
    magicshop();
    }
    break;
    
    case 4:
    if (gold <=99)
    {
    system("cls");
    printf("This does not come cheap, I can't sell it you for less than it states...");
    Sleep(2000);
    system("cls");
    magicshop();
    }
    
    else if (gold >=100)
    {
    items=+1;
    gold=gold-100;
    system("cls");
    printf("Hey, BIG spender!  You have aquired the invisabilty cloak!");
    Sleep(2000);
    system("cls");
    magicshop();
    }
    
    else {
    system("cls"); // ELSE is used as a catch fall to avoid errors
    magicshop();
    }
    break;
    
    case 5: // if player wants no items then exit to previous function
    system("cls");
    printf("Please come back and shop again soon...");
    Sleep(2000);
    system("cls");
    signpost();
    break;
    
    default: // makes sure questions are answered correctly
    system("cls");
    printf("Please choose from 1 to 5 ...");
    Sleep(2000);
    system("cls");
    magicshop();
    break;
    } // end switch loop                                     
    } // end magicshop
    
    void oldchurch()
    {
    } // end oldchurch
                
    void creepywoods()
    {
    
    system("cls");
    char sentg[]="\t\tTHE CREEPY WOODS.....\n"
                 "\n\n"
                 "You enter the creepy woods....\n"
                 "As you venture forward, you hear a rustling sound behind you...\n"
                 "You turn around and .....  \n"
                 "\n\n";
                 
                  size=strlen(sentg);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentg[x]);
                 }
                 Sleep(2000);
                 system("cls");
    
    char senth[]="An old woman is suddenly walking towards you....\n"            
                 "She seems friendly enough, so you try to engage in some light\n"
                 "convasation with her...\n";
                 
                  size=strlen(senth);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senth[x]);
                 }
                 Sleep(2000);
                 system("cls");
                 
    cout<<""<< pname <<""<<endl;
    char senttc[]="\n\n"
                    "Hi there, can you help me and Wizdora through the forest,\n"
                  "Or at least the location of a safe passage out?\n"
                  "\n\n";
                  
                  size=strlen(senttc);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senttc[x]);
                 }
    printf("OLD WOMAN:");
    char senttd[]="\n\n"
                  "Yes, I do know a safe passage out, and I  would advise against\n"
                  "not taking my advise.  But, my advise does not come cheap...\n"
                  "\n\n"
                  "Do you have any EXPERIENCE in anything yet?\n"
                  "\n\n"
                  "If you do, I will help you progress further....\n"
                  "\n\n";
                  
                  size=strlen(senttd);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senttd[x]);
                 }
    printf("WIZADORA:");
    char sentte[]="\n\n"
                "Do we have any Experience yet ";
                
                size=strlen(sentte);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentte[x]);
                 }
                 cout<<""<< pname <<""<<endl;
    printf("\n\n"
           "If you have experience Press Y, if you do not then Press N...");
           cout << "\n\n";
    cout << "Do you? ";
    cin >> choice;
    
    if ((choice == 'y') && (Exp == 1))
    {
    system("cls");
    cout<<""<< pname <<""<<endl;
    char senttf[]="\n\n"
                  "Yes, I do have Experience, Here is the proof....\n"
                  "Now Please help us on our travels...\n"
                  "\n\n";
                  
                   size=strlen(senttf);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senttf[x]);
                 }
    
    printf("OLD WOMAN:");
    char senttg[]="\n\n"
                    "Oh yes, so you do.. Follow me....\n"              
                    "\n\n";
                    
                     size=strlen(senttg);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senttg[x]);
                 }
                 
                 Sleep(1000);
    char senti[]="The old woman shows you a way through to woods, and you soon\n"
                 "come across an old wooden hut...\n"
                 "\n\n";
                 
                 size=strlen(senti);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senti[x]);
                 }
                 Sleep(2000);
                 system("cls");
                 woodenhut();
                 }
                 
    else if ((choice == 'y') && (Exp <=0))
    {
    health=health-20;
    system("cls");
    printf("OLD WOMAN\n"
    "\n\n");
    char sentth[]="I can see it in your eyes, you are lying to me!\n"
                  "For this punishment.... Back you go, and I will take\n"
                  "Some of your life force too, and use it to create my own!\n"
                  "NEVER LIE TO A WISE ONE!\n"
                  "\n\n";
                  
                  size=strlen(sentth);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentth[x]);
                 }
                 Sleep(1000);
                 
    char sentj[]="You feel dizzy and close your eyes, when you open them, you\n"
                 "find yourself back at the signpost...\n";
                 
                   size=strlen(sentj);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentj[x]);
                 }
                 Sleep(2000);
                 system("cls");
                 signpost();
                 }
                 
    else if (choice == 'n')
    {
    system("cls");
    cout<<""<< pname <<""<<endl;
    char sentti[]="\n\n"
                  "No, I have not got any yet, but I will venture on and return\n"
                  "when I am more experienced.\n"
                  "\n\n";
                  
                  size=strlen(sentti);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentti[x]);
                 }
                 
    printf("OLD WOMAN:");
    char senttj[]="\n\n"
                  "I like people who tell the truth, for a reward, I will give you a\n"
                  "clue...\n"
                  "\n\n"
                  "Go to the OLD CHURCH.  The minester should be able to help you\n"
                  "gain some experience in battle...\n"
                  "\n\n"
                  "Now, I shall return you to the signpost you past, it is the nearest I can\n"
                  "get you to the church....\n"
                  "\n\n";
                  
                   size=strlen(senttj);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senttj[x]);
                 }
                 
                 Sleep(2000);
                 system("cls");
                 
    printf("WIZADORA");
    char sentk[]="\n\n"
                 "Hey, who was that woman, some sort of witch?\n"
                 "And look, we are back at the signpost....\n"
                 "\n\n";
                 
                 size=strlen(sentk);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentk[x]);
                 }
                 
    cout<<""<< pname <<""<<endl;
    char sentl[]="\n\n"
             "I do not know who she was Wizadora, but we need that Experience.\n"
             "We'd better head to the old church.  Maybe we can find some answera there.\n"
             "\n\n";
             
             size=strlen(sentl);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentl[x]);
                 }
                 
                 Sleep(2000);
                 system("cls");
                 signpost();
                 }
                 
    else {
    system("cls");
    printf("You must press yes or no");
    Sleep(2000);
    system("cls");
    creepywoods();
    }
    } // end function Creepy Woods
    
    void woodenhut()
    {
    } // end function woodenhut
    
    
    void endingANDcredits()
    { 
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
    GetConsoleScreenBufferInfo(h, &csbiInfo);
    wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
    SetConsoleTextAttribute ( h, FOREGROUND_GREEN|FOREGROUND_INTENSITY );
    system("cls");
    char senteog[]="Drucila Falls to the floor and trys to grab you\n"
                   "as you take a step back from her...\n"
                   "\n\n"
                   "The chapel begins to tumble to the floor, you manage to\n"
                   "escape through the large window on your left hand side.\n"
                   "\n\n"
                   "You run and run as the cathedral tumbles down behind you.\n"
                   "\n\n"
                   "The sun breaks through the clouds as they turn from dark\n"
                   "to white\n"
                   "In the faint rays of the sun, the monsters that littered the\n"
                   "ground turn back to the once happy people of Wiza...\n"
                   "\n\n"
                   "You then arrive back in the town square, and recive three\n"
                   "huge cheers as the entire town turns out to give their thanks!\n"
                   "\n\n"
                   "The WHITE WILLOW begins to glow and sprout with joy once more...\n"
                   "\n\n"
                   "You can see the light flowing from it's glistening bark...\n"
                   "\n\n"
                   "\n\n"
                   "With Drucila now defeated, and the land of Wiza finaly free, you can\n"
                   "now take a well earned break....\n"
                   "\n\n"
                   "\n\n"
                   "\n\n"
                   "\n\n"
                   "CONGATULATIONS!!!!!\n"
                   "\n\n"
                   "\n\n"
                   "\n\n"
                   "\n\n"
                   "\n\n"
                   "\n\n";
                   
                   size=strlen(senteog);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senteog[x]);
                 }
                 
                 Sleep(2000);
                 system("cls");
                 
    char senteogc[]="\tW  I  Z  A  D  O  R  A\n"
                    "\t\n\n"
                    "\tProgramed by - Peter Watts\n"
                    "\t\n\n"
                    "\tWritten and Developed by - Peter Watts\n"
                    "\t\n\n"
                    "\tSound and Music - Steve THE MAN Childs\n"
                    "\t\n\n"
                    "\tSprite Design - Alan Pink\n"
                    "\t\n\n"
                    "\tText Color Program - Peter Watts\n"
                    "\t\n\n"
                    "\tMain dcpp Concept - Archwright Limited\n"
                    "\t\n\n"
                    "\tSpecial Thanks - Alan Pink\n"
                    "\t\n\n"
                    "\t                 Steve Childs\n"
                    "\t\n\n"
                    "\tLook out for the sequal soon!!!\n"
                    "\t\n\n"
                    "\n\n"
                    "\n\n"
                    "\n\n"
                    "\n\n"
                    "\t\tT  H  E      E  N  D\n"
                    "\n\n"
                    "\n\n"
                    "\n\n"
                    "\n\n"
                    "\n\n"
                    "\n\n";
                    
                     size=strlen(senteogc);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",senteogc[x]);
                 }
                 Sleep(3000);
                 exit(0);
                 } // end of game
    here is the gsameover function source:

    Code:
    // Game Over file for Wizadora
    // Written by Peter Watts
    
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    extern int health;
    extern int x;
    extern int size;
    void gameover();
    
    int gameover(int argc, char *argv[])
    {
    gameover();
    }
    
    void gameover()
    {
    
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
    GetConsoleScreenBufferInfo(h, &csbiInfo);
    wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
    SetConsoleTextAttribute ( h, FOREGROUND_RED|FOREGROUND_INTENSITY );
    char sentgo[]="\n\n"
                  "\n\n"
                  "\tThe final blow to your fragile body makes you collapse\n"
                  "\n\n"
                  "\tto your knees.... \n"
                  "\n\n"
                  "\tYou look to the hevans and can see Drucila's warth of evil\n"
                  "\n\n"
                  "Covering the land.....\n"
                  "\n\n"
                  "\n\n"
                  "\n\n"
                  "\n\n"
                  "\n\n"
                  "\t\tG  A  M  E    O  V  E  R\n";
                  
                  
                  size=strlen(sentgo);
                 for(x=0;x<size;x++)
                 {   
                 Sleep(40); 
                 printf("%c",sentgo[x]);
                 }
                 Sleep(3000);
                 exit(0);
                 } // end function

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Your main code does not compile:

    error C2562: 'signpost' : 'void' function returning a value

    and your gameover code is obviously incomplete (it certainly won't compile!).

    Since you are obviously using a C++ compiler, why not use classes and do away with all those global variables? A little thought now will make maintenance and expansion orders of magnitude easier. You should always strive to minimize the scope of all variables and you should also consider naming conventions, particularlly for module wide and global variables. You may also want to consider extracting the prose from your code into a file as it will enable you to make changes easier (won't have to recompile), not to mention with some clever setup of state variables, enable code reuse.

    As for your particular complaint, since gameover is only called once, in main, at the end, how sure are you that the program ever gets to that point? Either run the program in a debugger or put debug output statements so you know the state of your program at critical steps.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    ok thanks alot for the advise.. it's odd that my code will not compile on your compiler. I just ran the debug and will serously concider using classes, might cut the functions down a little too, I think I will deal with the game over error after I re-organised it a little better.

    ONE MORE THING:

    If I put the gameover function into the main source file and call it from main and not an external source, would it work better or worse?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    One possible approach would be to create a player class and a game class. In main() you'd declare a game object. In setting up the attributes of the game object you can declare a player object. During a given game, if the player's health decreases to zero you can call the gameOver() function. In this scenario I think you could have the gameOver() function be a member of either the player or the game class.

    As somewhat of an aside, you could declare one or more player objects for any given game. In this scenario I'd have a removePlayer() function if a given player's health decreases to zero, and a gameOver() if all players have been removed. However, setting up a single player game is probably preferable to setting up a multi-player game.
    You're only born perfect.

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    It seems to me that your gameover function should be called whenever the number of points is <= 0, yet in your main() you only call it once, yet exit the game (at least I presume you exit after spells()) anyway.

    Regarding the compilation problems, what compiler are you using? It seems to have very serious standards issues if it doesn't flag that at least as a warning (perhaps you have warnings turned off? (always a bad idea, most warnings are actually errors)).

    Think of the game as a state machine. Each action causes reactions that may cascade (if the player gets shot and looses all their points, the game is over) requiring that the state be updated (perhaps recursively). Though many programs appear to be linear, they need not be implemented that way (yours is very linear, which gives the user very little freedom to explore). So called 'event driven' programs (Windows programs are the archetype) allows the user to choose to do whatever they want to do (subject to some contraints) and the programmer must always maintain the state of the program so it can be examined from any part in the program. It may sound tedious, like you have to have logic/error checking code everywhere, but with a little thought you can encapsulate much of that into function/methods and just call them with various flags.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Some general comments.

    1. All your functions return void, and take no parameters.
    2. A consequence of 1 is that you have lots of global variables instread.
    The whole thing is basically 'structured goto' programming.

    3. Indentation.
    Code:
    int main(int argc, char *argv[])
    {
        SetConsoleTitle("W I Z A D O R A - BY PETER WATTS -"); // console Title
        AltEnter();
    	intro();
    	level1();
    	plea();
    	wizavillage();
    	signpost();  // Set functions to MAIN() 
    	magicshop();
    	oldchurch();
    	creepywoods();
    	woodenhut();
    	endingANDcredits();
    
    	if(health<=0) gameover(); // call gameover FROM main
    	if(powers=+1) spells();   // call spells FROM main
    
    	return 0;
    } // end main
    You can spot so many problems from well indented code, simply because you can see how the code is going to execute from the way the code is indented.
    Also, if(powers=+1) isn't going to be doing what you want it to.

    If for no other reason, consider that most people who could answer your questions will simply take one look at that mass of poorly indented code, say "ugh" and move on to another question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    it's odd that my code will not compile on your compiler
    Hmmm.... I agree there, it seems to work for me, and I'm using GNU C++ Compiler. Though your code looks kind of odd, too:

    return signpost(); // return to this point after system("pause")
    I'm not very clear about what you want this code to do; and your comment is strange. Why the return anyway? If you want recursion, you could just do it like:

    Code:
    signpost();

    If I put the gameover function into the main source file and call it from main and not an external source, would it work better or worse?
    That has really nothing to say. In both cases your gameover function will be present in the game and called from the main(). That is, as long as it is correct and compiles without errors.


    The location of it hasn't any significance. Though it's a good idea to split your code in several files so that it becomes more readable. That will help you later when you are going to debug and for other people who are going to look at your code.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  4. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM
  5. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM