Advertisement
SteelGolem

text adventure game v0.0.3

Aug 26th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. // v0.0.3
  2.  
  3. /*
  4.     NEXT:
  5.     - add new rooms: castle grounds, castle fields, castle town
  6.     - add battling
  7.     - add shopping
  8.     - add item use submenu
  9. */
  10.  
  11. #include <stdio.h> // printf(), getchar(), fflush()
  12. #include <ctype.h> // tolower()
  13.  
  14. #define FALSE 0
  15. #define TRUE 1
  16.  
  17. void pause (void);
  18.  
  19. int game_over = FALSE;
  20. void show_common_commands (void);
  21. void check_common_commands (int command);
  22.  
  23. // room stuff
  24. #define ROOM_ID_THRONE_ROOM 1
  25. int room_id = ROOM_ID_THRONE_ROOM;
  26. int throne_room_chest_empty = FALSE;
  27. void throne_room (void);
  28.  
  29. // item stuff
  30. #define ITEM_ID_TORCH 1
  31.  
  32. // hero stuff
  33. int level = 1;
  34. int hp = 10, max_hp = 10;
  35. int mp = 0, max_mp = 0;
  36. int atk = 1;
  37. int def = 0;
  38. int gold = 0;
  39. int num_items = 0;
  40. int items[8]; // array of IDs representing items
  41.  
  42. int main (void)
  43. {
  44.     while (!game_over)
  45.     {
  46.         switch (room_id)
  47.         {
  48.         case ROOM_ID_THRONE_ROOM:
  49.             throne_room();
  50.             break;
  51.         }
  52.     }
  53.    
  54.     return 0;
  55. }
  56.  
  57. void pause (void)
  58. {
  59.     printf ("=========================== press ENTER to continue ===========================");
  60.     getchar();
  61.     fflush (stdin);
  62. }
  63.  
  64. void show_common_commands (void)
  65. {
  66.     printf ("[m] Magic   [i] Inventory\n");
  67.     printf ("[s] Status  [a] Search area\n");
  68.     printf ("[q] Quit\n");
  69.     printf ("\n");
  70. }
  71.  
  72. void check_common_commands (int command)
  73. {
  74.     int i;
  75.    
  76.     switch (command)
  77.     {
  78.     case 'm':
  79.         printf ("You have no magic spells.\n");
  80.         printf ("\n");
  81.         pause();
  82.         break;
  83.        
  84.     case 'i':
  85.         printf ("You have:\n");
  86.         printf ("\t%d Gold\n", gold);
  87.         if (num_items > 0)
  88.         {
  89.             for (i = 0; i < num_items; i++)
  90.             {
  91.                 switch (items[i])
  92.                 {
  93.                     case ITEM_ID_TORCH: printf ("\tTorch\n"); break;
  94.                     // TODO: more item types
  95.                 }
  96.             }
  97.         }
  98.         else printf ("\tNo items\n");
  99.         printf ("\n");
  100.         pause();
  101.         break;
  102.    
  103.     case 's':
  104.         printf ("Level: %d\n", level);
  105.         printf ("\tHP:  %d/%d\n", hp, max_hp);
  106.         printf ("\tMP:  %d/%d\n", mp, max_mp);
  107.         printf ("\tAtk: %d\n", atk);
  108.         printf ("\tDef: %d\n", def);
  109.         printf ("\n");
  110.         pause();
  111.         break;
  112.        
  113.     case 'q': game_over = TRUE;
  114.     }
  115. }
  116.  
  117. void throne_room (void)
  118. {
  119.     // temporary variables
  120.     int command;
  121.  
  122.     printf ("\n");
  123.    
  124.     // room description
  125.     printf ("You are in the King's throne room.\n");
  126.     printf ("\n");
  127.    
  128.     // room commands
  129.     printf ("[1] Speak to the King\n");
  130.     printf ("[2] Speak to the guards\n");
  131.     printf ("[3] Check treasure chest\n");
  132.     printf ("[4] Leave throne room\n");
  133.     printf ("\n");
  134.    
  135.     // common commands
  136.     show_common_commands();
  137.    
  138.     // user input retrieval
  139.     printf ("Command? ");
  140.     command = tolower (getchar());
  141.     fflush (stdin);
  142.     printf ("\n");
  143.    
  144.     // check room commands
  145.     switch (command)
  146.     {
  147.     case '1':
  148.         printf ("The King says, \"Please, take what we have provided from the treasure chest,\n");
  149.         printf ("and save the kingdom!\"\n");
  150.         printf ("\n");
  151.         pause();
  152.         break;
  153.        
  154.     case '2':
  155.         printf ("One of the guards says, \"The Princess was kidnapped a week ago, and the King\n");
  156.         printf ("is very worried about her... Please! Rescue the princess!\n");
  157.         printf ("\n");
  158.         pause();
  159.         break;
  160.        
  161.     case '3':
  162.         if (throne_room_chest_empty)
  163.         {
  164.             printf ("The chest is empty.\n");
  165.         }
  166.         else
  167.         {
  168.             printf ("You find a Torch and 180 Gold in the chest!\n");
  169.  
  170.             gold += 180;
  171.             items[num_items++] = ITEM_ID_TORCH;
  172.            
  173.             throne_room_chest_empty = TRUE;
  174.         }
  175.         printf ("\n");
  176.         pause();
  177.         break;
  178.    
  179.     case 'a':
  180.         printf ("You don't find anything else.\n");
  181.         printf ("\n");
  182.         pause();
  183.         break;
  184.     }
  185.    
  186.     // check common commands
  187.     check_common_commands (command);
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement