Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Unnamed
Wednesday, June 13th, 2012 at 11:47:56pm MDT 

  1. #include <curses.h>
  2.  
  3. // Map dimensions
  4. #define MAP_WIDTH  40
  5. #define MAP_HEIGHT 24
  6.  
  7. // Global variables
  8. int PlayerX = 9;
  9. int PlayerY = 5;
  10. int PlayerTurn;
  11.  
  12. // Message containers
  13. const char* MsgArray[7];
  14. const char* Msg = "";
  15.  
  16. // Map container
  17. char MapArray[MAP_HEIGHT][MAP_WIDTH + 1] = {
  18.  
  19. "^^*^*^^^********************************",
  20. "^*^^^^**********************Y***********",
  21. "^^^***************Y******Y**************",
  22. "*^********************************Y*****",
  23. "^^*****#######*********Y****************",
  24. "^******#.....#**************Y***********",
  25. "*******#.....#**************************",
  26. "*******#.....#**************************",
  27. "**Y****###+###*******~~~~~*********Y****",
  28. "*******************~~~~~~~~*************",
  29. "*******************~~~~~~~~*************",
  30. "*********************~~~~~***Y**********",
  31. "****Y***********************************",
  32. "****************************************",
  33. "***********************************Y****",
  34. "****************************************",
  35. "*********************####+#####*********",
  36. "***************Y*****#........#*********",
  37. "****Y****************#........#*********",
  38. "*********************##+###...#**Y******",
  39. "************Y********#....#...#*********",
  40. "*********************#..>.#...#*******Y*",
  41. "*******Y*************##########*********",
  42. "****************************************"
  43.  
  44. };
  45.  
  46. // Map drawing function
  47. void DrawMap(void) {
  48.   for(int y = 0; y < MAP_HEIGHT; y++) {
  49.     for(int x = 0; x < MAP_WIDTH; x++) {
  50.  
  51.       // Colorizer
  52.       switch(MapArray[y][x]) {
  53.         case '*':
  54.           attron(COLOR_PAIR(3));
  55.           break;
  56.         case '~':
  57.           attron(COLOR_PAIR(4));
  58.           break;
  59.         case 'Y':
  60.           attron(COLOR_PAIR(2));
  61.           break;
  62.         case '^':
  63.           attron(COLOR_PAIR(1));
  64.           break;
  65.         case '+':
  66.           attron(COLOR_PAIR(2));
  67.           break;
  68.         case '/':
  69.           attron(COLOR_PAIR(2));
  70.           break;
  71.       }
  72.  
  73.       // Draw characters and reset colors
  74.       mvaddch(y, x, MapArray[y][x]);
  75.       attroff(COLOR_PAIR(2));
  76.     }
  77.   }
  78. }
  79.  
  80. // Collision detect function
  81. bool IsPassable(int MapX, int MapY) {
  82.  
  83.   // Make area outside map impassible
  84.   if( MapX < 0 || MapX >= MAP_WIDTH || MapY < 0 || MapY >= MAP_HEIGHT )
  85.   return false;
  86.  
  87.   int Ch = MapArray[MapY][MapX];
  88.  
  89.   // Check which characters are passible.
  90.   if(Ch == '.' || Ch == '*' || Ch == '/' || Ch == '>' || Ch == '<')
  91.   return true;
  92.  
  93.   // Otherwise not passible
  94.   return false;
  95. }
  96.  
  97. // Info screen function
  98. void DrawInfo(void) {
  99.  
  100.   // Status
  101.   mvprintw(0, 41, "Player the Nobody");
  102.   mvprintw(0, 66, "%d", PlayerTurn);
  103.  
  104.   attron( COLOR_PAIR(1) );
  105.   mvprintw(2, 41, "Life: xxx/xxx");
  106.   attroff( COLOR_PAIR(1) );
  107.  
  108.   attron( COLOR_PAIR(2) );
  109.   mvprintw(3, 41, "Level: xx");
  110.   attroff( COLOR_PAIR(2) );
  111.  
  112.   attron( COLOR_PAIR(3) );
  113.   mvprintw(2, 66, "Mana: xxx/xxx");
  114.   attroff( COLOR_PAIR(3) );
  115.  
  116.   attron( COLOR_PAIR(5) );
  117.   mvprintw(3, 66, "Exp: xxxxxxxx");
  118.   attroff( COLOR_PAIR(5) );
  119.  
  120.   // Inventory
  121.   mvprintw(5, 41, "---------------------------- Inventory");
  122.  
  123.   // Messages
  124.   mvprintw(16,41, "----------------------------- Messages");
  125.  
  126.   // Check for message and shift array
  127.   if(Msg != "") {
  128.     for(int x = 6; x > 0; x--) {
  129.       MsgArray[x] = MsgArray[x - 1];
  130.     }
  131.  
  132.   // Push current message into message array
  133.   MsgArray[0] = Msg;
  134.   Msg = "";
  135.  
  136.   }
  137.  
  138.   // Print the message array
  139.   for(int x = 0; x < 7; x++) {
  140.     int ypos = 23 - x;
  141.     mvaddstr(ypos, 41, MsgArray[x]);
  142.   }
  143. }
  144.  
  145. // Door close function
  146. bool CloseDoor(void) {
  147.   int Key = getch();
  148.   int DeltaX, DeltaY = 0;
  149.  
  150.   switch(Key) {
  151.     // Which direction?
  152.     case 'k':
  153.       DeltaX = 0;
  154.       DeltaY = -1;
  155.       break;
  156.  
  157.     case 'h':
  158.       DeltaX = -1;
  159.       DeltaY = 0;
  160.       break;
  161.  
  162.     case 'l':
  163.       DeltaX = 1;
  164.       DeltaY = 0;
  165.       break;
  166.  
  167.     case 'j':
  168.       DeltaX = 0;
  169.       DeltaY = 1;
  170.       break;
  171.  
  172.     default:
  173.       Msg = "Invalid direction.";
  174.       return false;
  175.   }
  176.   if(MapArray[PlayerY + DeltaY][PlayerX + DeltaX] == '/') {
  177.     MapArray[PlayerY + DeltaY][PlayerX + DeltaX] = '+';
  178.     return true;
  179.   }
  180.   else {
  181.     Msg = "You can't close that.";
  182.     return false;
  183.   }
  184. }
  185.  
  186. // Main function
  187. int main( void ) {
  188.  
  189.   // Init curses
  190.   keypad( initscr(), 1 );
  191.   curs_set( 0 );
  192.   noecho();
  193.  
  194.   // Init colors
  195.   start_color();
  196.   init_pair( 1, COLOR_RED, COLOR_BLACK );
  197.   init_pair( 2, COLOR_YELLOW, COLOR_BLACK );
  198.   init_pair( 3, COLOR_GREEN, COLOR_BLACK );
  199.   init_pair( 4, COLOR_BLUE, COLOR_BLACK );
  200.   init_pair( 5, COLOR_CYAN, COLOR_BLACK );
  201.   init_pair( 6, COLOR_MAGENTA, COLOR_BLACK );
  202.  
  203.   // Game Loop
  204.   while( true ) {
  205.  
  206.     int Key = 0;
  207.     int DeltaX = 0;
  208.     int DeltaY = 0;
  209.  
  210.     // Refresh screen
  211.     erase();
  212.  
  213.     // Draw map
  214.     DrawMap();
  215.    
  216.     // Draw player
  217.     attron( COLOR_PAIR(5) );
  218.     mvaddch( PlayerY, PlayerX, '@' );
  219.     attroff( COLOR_PAIR(5) );
  220.  
  221.     // Info screen
  222.     DrawInfo();
  223.  
  224.     // Wait for input
  225.     Key = getch();
  226.  
  227.     switch(Key) {
  228.  
  229.       // Move up
  230.       case 'k':
  231.         DeltaX = 0;
  232.         DeltaY = -1;
  233.         break;
  234.  
  235.       // Move left
  236.       case 'h':
  237.         DeltaX = -1;
  238.         DeltaY = 0;
  239.         break;
  240.  
  241.       // Move right
  242.       case 'l':
  243.         DeltaX = 1;
  244.         DeltaY = 0;
  245.         break;
  246.  
  247.       // Move down
  248.       case 'j':
  249.         DeltaX = 0;
  250.         DeltaY = 1;
  251.         break;
  252.  
  253.       // Move Up-left
  254.       case 'y':
  255.         DeltaX = -1;
  256.         DeltaY = -1;
  257.         break;
  258.  
  259.       // Move Up-right
  260.       case 'u':
  261.         DeltaX = 1;
  262.         DeltaY = -1;
  263.         break;
  264.  
  265.       // Move Down-left
  266.       case 'b':
  267.         DeltaX = -1;
  268.         DeltaY = 1;
  269.         break;
  270.  
  271.       // Move Down-right
  272.       case 'n':
  273.         DeltaX = 1;
  274.         DeltaY = 1;
  275.         break;
  276.  
  277.       // Wait one turn
  278.       case '.':
  279.         break;
  280.  
  281.       // Quit
  282.       case 'q':
  283.         return endwin();
  284.         break;
  285.  
  286.       // Close door
  287.       case 'c':
  288.         if(CloseDoor()) {
  289.           break;
  290.         }
  291.         else {
  292.           continue;
  293.         }
  294.  
  295.       // Else
  296.       default:
  297.         Msg = "Invalid command.";
  298.         continue;
  299.     }
  300.  
  301.     // Open doors
  302.     if( MapArray[PlayerY + DeltaY][PlayerX + DeltaX] == '+' ) {
  303.       MapArray[PlayerY + DeltaY][PlayerX + DeltaX] = '/';
  304.       DeltaX, DeltaY = 0; // Prevent walking through door
  305.     }
  306.  
  307.     // Check if direction is passable and move
  308.     if( IsPassable(PlayerX + DeltaX, PlayerY + DeltaY) ) {
  309.       PlayerX += DeltaX;
  310.       PlayerY += DeltaY;
  311.     }
  312.     else {
  313.       Msg = "You can't go that way.";
  314.       continue;
  315.     }
  316.  
  317.     // Increment turn count
  318.     PlayerTurn++;
  319.   }
  320. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation
fantasy-obligation
fantasy-obligation