Thread: Haunted Forest *Example of text game*

  1. #1
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105

    Haunted Forest *Example of text game*

    Code:
    //-------------------------------------------------------------------------
    //  Kirdra 2002     Haunted Forest v0.1
    //-------------------------------------------------------------------------
    
    #include <conio.h>                                                //getch()             
    #include <iostream>                                          //cin/cout etc
    #include <stdlib.h>                     //Clear the screen - system("cls");            
    
    //-------------------------------------------------------------------------
    //  Variable names pretty much decribe what they do
    //-------------------------------------------------------------------------
    
    int Win_Condition = 0;
    int Treasure = 0;
    int Weapon = 0;                                    
    int User_Location_X = 0;
    int User_Location_Y = 0;                             //Position on the grid               
    int User_Input, Q = 0;            //I like to use Q as a Quit Game variable               
    int Game_Over = 100;                               //Turns before game over
                                    
    int Display_Room()                //Function - Include for example purposes 
        {
    cout << "X position = "<<User_Location_X<<"\n";
    cout << "Y position = "<<User_Location_Y<<"\n\n";
        }
    
    //-------------------------------------------------------------------------
    //  The program begins
    //-------------------------------------------------------------------------
    
    int main()
    {
    
        cout << "This game is designed to be used on the number pad.\n";                      
        cout << "To quit the game press 0 then Enter, to see your\n";
        cout << "current co-ordinates press 5 then Enter. To use the\n";
        cout << "directions simple press the number besides the\n";  
        cout << "direction name.";                      //Information for users
        getch();                      //Waits for a key press before continuing
        
    //-------------------------------------------------------------------------
    //  The loop begins
    //-------------------------------------------------------------------------
    
    do
    {
        
    //-------------------------------------------------------------------------
    //  Define our barriers/walls
    //-------------------------------------------------------------------------
    
    if (User_Location_X  == 6)
            {
        cout << "You can not continue in this direction.\n";
        User_Location_X--;
        getch();
            }
        
    else if(User_Location_X == -6)
        {
        cout << "You can not continue in this direction.\n";
        User_Location_X++;
        getch();
        } 
    
    else if(User_Location_Y == 6)
        {
        cout << "You can not continue in this direction.\n";
        User_Location_Y--;
        getch();
        }   
    
    else if(User_Location_Y == -6)
        {
        cout << "You can not continue in this direction.\n";
        User_Location_Y++;
        getch();
        }
    
    //-------------------------------------------------------------------------
    //  Game over if 100 turns expire
    //-------------------------------------------------------------------------
    
    Game_Over = Game_Over--;         //Game_Over is reduced by 1 each loop when    
    if(Game_Over == 0)                              //it reaches 0 the game ends
        {
        system("cls");
        cout << "Night falls, the monsters of the Forest come out to play.\n\n";
        cout << "Game Over.";
        getch();
        return 0;                                   //This teminates the program
        }
    
    //-------------------------------------------------------------------------
    //  This text will display every loop
    //-------------------------------------------------------------------------
    
        system("cls");
    
        cout << "Haunted Forest     v0.1\n\n";
        cout << "8)North\n6)East\n2)South\n4)West\n\n"; 
        cin >> User_Input;  
        cout << "\n"; 
    
    //-------------------------------------------------------------------------
    //  If user wants to quit
    //-------------------------------------------------------------------------
    
    if(User_Input == 0)
    {
        system("cls");
        cout << "Night falls, the monsters of the Forest come out to play.\n\n";
        cout << "Game Over.";
        getch();
        return 0;   //This teminates the program
    }
    
    //-------------------------------------------------------------------------
    //  Updating Co-ordinates and outputing relavent text to screen
    //-------------------------------------------------------------------------
    
    switch(User_Input)
        {
    case 8: cout << "You venture north.\n\n";
        User_Location_Y++;  break;                   
    case 6: cout << "You venture east.\n\n";            
        User_Location_X++; break;     
    case 2: cout << "You venture south.\n\n";           
        User_Location_Y--; break;     
    case 4: cout << "You venture west.\n\n";            
        User_Location_X--; break;
    case 5: Display_Room(); 
        }
        
    //-------------------------------------------------------------------------
    //  Now to include special areas (rooms that do something)
    //-------------------------------------------------------------------------
    
    //-------------------------------------------------------------------------
    //  Treasure room
    //-------------------------------------------------------------------------
    if(User_Location_X == 0 && User_Location_Y == 5)
    {
        if(Treasure == 1)           //If the user has already been here
        {
        cout << "You see an empty treasure chest.\n\n";
        }
        else if(Treasure == 0)      //If the user has not been to this area
        {
        cout << "You have found the treasure of the Haunted Forest.\n\n";
        Treasure = 1;               //Telling the program the user has been here
        }                           //for the next loop for the relevant message
        
    }
    
    //-------------------------------------------------------------------------
    //  Weapon room
    //-------------------------------------------------------------------------
    if(User_Location_X == 5 && User_Location_Y == 0)
    {
        if(Weapon == 1)
        {
        cout << "You see marks in a tree made from a knife.\n\n";
        }
        else if(Weapon == 0)
        {
        cout << "You have found the Haunted Dagger.\n\n";
        Weapon = 1;
        }  
    }
    
        
    //-------------------------------------------------------------------------
    //  Output for a winner
    //-------------------------------------------------------------------------
    
    if(Win_Condition == 1)
    {
        system("cls");
        cout << "You have completed Haunted Forest v0.1\n\n";
        getch();
        return 0;    //This teminates the program   
    }
    
    //-------------------------------------------------------------------------
    //  The message after a turn expires
    //-------------------------------------------------------------------------
    
            cout << "The Sun begins to fade it is getting darker."; 
            getch();
            system("cls");
    
    //-------------------------------------------------------------------------
    //  End the do loop
    //-------------------------------------------------------------------------
    
    }               //This Bracket ends the "do" that was opened for the loop 
    
    while(Q!=1);    //Because the loop has ended the program will read to the 
                    //end of the code thus quiting 
    
    //-------------------------------------------------------------------------
    //  End the program
    //-------------------------------------------------------------------------     
    
    return 0;       //This teminates the program
    
    }               //int main()
    I was so happy when I finished this game. Short but I learned a lot through it, more so through ideas than accual code. It took a bit of thinking to come up with the barrier/wall system. although it is very simple

    This source code has been stiriped down from the original btw. Just want to show the basic structure of the program.


    *Edit* Quick bug fixed
    Last edited by Kirdra; 09-29-2002 at 05:05 PM.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Very fancy commenting
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    I like to keep my code very neat/spaced out and easy to read so it's easy to update/modify and to copy into other in other programs.

    I will move on to windows programming now I think so I can add images to my creations, maybe even animations!

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I used to try commenting like that; unfortunately, I gave up after a while, leaving my code splotchy and ugly
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM