Advertisement
SteelGolem

text adventure game v0.0.2

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