Thread: Text Game in C.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Text Game in C.

    So I want to learn C and continue going up to higher level programming languages. I thought the best way to do this would be to start game development because it's very difficult and you know you know the language when you have a fully functional game. I'm starting off just doing a text game. Just to get the basics and the jist of it all. My question is am I coding the options and cases correctly. I'm not going to post the in depth text but here is the way I did it. If it helps you can input your own variables into the 1,2,3 spots.
    Code:
    #include <stdio.h>
    
    
    void 1(){
         void 1(){
              }
         void 2(){
              }
         void 3(){
              }
             int input2;
             printf("Text");
             scanf("%d", &input2);
             switch(input2){
                           case 1:
                                11();
                                break;
                           case 2:
                                12();
                                break;
                           case 3:
                                13();
                                break;
                           }
             }
    void 2(){
         printf( "Text");
         }
    void 3(){
         printf( "Text");
         }
    int main(){
        int input1;
        printf( "Text");
        scanf( "%d", &input1);
        switch(input1){
                      case 1:
                           1();
                           break;
                      case 2:
                           2();
                           break;
                      case 3:
                           3();
                           break;
                      case 4:
                           printf( "Text");
                           break;
                      default:
                              printf( "Text");
                              1();
                              break;
                      }
                      scanf("%d");
                      getchar();
        }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't get it. It's difficult to understand what you're getting at with this weird pseudocode. Why don't you post something that compiles?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Start. Smaller.

    Can you write a "Hello World" program that compiles and runs?

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Well I'm already farther in than the "Hello World" program. I just wrote it in pseudocode because I'm a very paranoid person so I don't like to give any code. But seeing how it's hard to do it otherwise I guess I'll have to get over it. So here is the code.
    Code:
    #include <stdio.h>
    
    
    void play(){
         void assaultrifle(){
              }
         void sniperrifle(){
              }
         void submachinegun(){
              }
             int input2;
             printf("Select your weapon\n");
             printf("1. Assault Rifle\n");
             printf("2. Sniper Rifle\n");
             printf("3. Sub-Machine Gun\n");
             printf("Enter selection number\n");
             scanf("%d", &input2);
             switch(input2){
                           case 1:
                                assaultrifle();
                                break;
                           case 2:
                                sniperrifle();
                                break;
                           case 3:
                                submachinegun();
                                break;
                           }
             }
    void load(){
         printf( "Select which game save to load\n");
         }
    void multiplayer(){
         printf( "Select multiplayer game type\n");
         }
    int main(){
        int input1;
        printf( "1. Play Game\n");
        printf( "2. Load Game\n");
        printf( "3. Multiplayer\n");
        printf( "4. Exit\n");
        printf( "Enter selection number\n");
        scanf( "%d", &input1);
        switch(input1){
                      case 1:
                           play();
                           break;
                      case 2:
                           load();
                           break;
                      case 3:
                           multiplayer();
                           break;
                      case 4:
                           printf( "Goodbye\n");
                           break;
                      default:
                              printf( "Default selection...Play Game...\n");
                              play();
                              break;
                      }
                      scanf("%d");
                      getchar();
        }
    I'm just wondering if I'm getting at it correctly...I know it compiles and runs fine, I just want to know if there is a more efficient way.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When I'm making a game, I have main() call a 1st menu, which is the most basic options, and a quit. That will (perhaps with other submenu's being called depending on the game), eventually reach a play1game() function. In play1game, there will be a large do while(game_Not_over && quit == 0) type loop. Everything to play one game will be handled inside that loop (which will typically call several other functions itself).

    main() really isn't used for much in a well designed game, except calling the first outer menu, and returning an int to the operating system, imo.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    I understand main() calling the 1st menu, but after that you lost me...could you give me an example in code? It would help out a lot more.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ^ Something like this:
    Code:
    int main(void)
    {
        int status;
       switch(main_menu())
       {
           case 1: status = play1game();break;
           //etc
       }
       return status;
    }
    ......
    int play1game()
    {
         int quit_condition;
         int status;
         do
         {
          //game loop
         }while(!quit_condition);
         return status;
    }

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Where does main_menu come from in that?

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I think he means like this:
    Code:
    void PlayGame() {
        int keepPlaying = 1;
        while (keepPlaying) {
            DisplayWorld();
            GetInput();
            if (keepPlaying)
                UpdateWorld();
        }
    }
    
    void MainMenu() {
        // display menu
        // get option
        if (option == PLAY_THE_GAME)
            PlayGame();
        // etc.
    }
    
    int main(int argc, char *argv[]) {
        // handle command-line options
        // general setup
        MainMenu();
        // general teardown
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Anthony Brown View Post
    Where does main_menu come from in that?
    In my example, it'd be a function that displays the choices, and returns the one selected by the user.
    (But that was for you to figure out. :P)

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    So far this is what I've updated my game to, with the help of you guys.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void Exit(){
         printf(" Now Closing\n ");
         }
    void Multiplayer() {
         printf(" Loading Multiplayer Option\n ");
         }
    void Load() {
         printf(" Load\n ");
         }
    void Play() {
         printf(" Play\n ");
         }
    void MainMenu() {
         int gameselectioninput;
         int playgame = 1;
         int loadgame = 2;
         int loadmultiplayeroption = 3;
         int exit = 4;
         printf("1. Play Game\n");
         printf("2. Load Game\n");
         printf("3. Load Multiplayer Option\n");
         printf("4. Exit\n");
         printf(" Enter Selection Number \n");
         scanf("%d", &gameselectioninput);
         if( gameselectioninput == playgame){
             Play();
             }
         else if ( gameselectioninput == loadgame){
              Load();
              }
         else if ( gameselectioninput == loadmultiplayeroption){
              Multiplayer();
              }
         else if ( gameselectioninput == exit){
              Exit();
              }
         }
    int main(int argc, char *argv[])
    {
        MainMenu();
        system("PAUSE");    
        return 0;
    }
    And manasij7479 I was just confused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text-based game in C++
    By Nectarios in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2008, 07:12 PM
  2. Text adventure game GUI
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 11-07-2007, 06:34 PM
  3. Text Based Game Help!
    By TylerMoyer in forum Game Programming
    Replies: 10
    Last Post: 07-01-2007, 12:02 AM
  4. First Game (text trivia)
    By NewbieVB in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2002, 07:13 AM
  5. Game Text
    By Kuplex in forum C++ Programming
    Replies: 10
    Last Post: 03-07-2002, 12:42 PM