SteelGolem

01 basic rogue setup

Mar 29th, 2021 (edited)
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.07 KB | None | 0 0
  1. // 01 basic setup
  2.  
  3. // rogue at its simplest and most readable
  4.  
  5. // player can move around dungeon, bump into walls,
  6. // pick up gold and items, kill monsters, and
  7. // use the stairs
  8.  
  9. // random bits of level, monsters, gold, and items
  10. // placed randomly in the dungeon
  11.  
  12. // help screen shows how to deal with fullscreen
  13. // commands like inventory and discoveries
  14.  
  15. using System;
  16.  
  17. class Program
  18. {
  19.     static THING player = new THING();
  20.  
  21.     static bool found_amulet = false;
  22.  
  23.     static Random random = new Random();
  24.  
  25.     static void Main(string[] args)
  26.     {
  27.         Console.WindowWidth = Console.BufferWidth = 80;
  28.         Console.WindowHeight = Console.BufferHeight = 25;
  29.         Console.Title = "steelgolem rogue - 01 basic setup";
  30.  
  31.         CONSOLE.clear();
  32.  
  33.         dungeon_level = 1;
  34.         new_level();
  35.  
  36.         update_status();
  37.  
  38.         while (true)
  39.         {
  40.             // put cursor under player to highlight
  41.             CONSOLE.move_cursor(player.x, player.y);
  42.  
  43.             char key_ch = CONSOLE.read_key();
  44.  
  45.             switch(key_ch)
  46.             {
  47.                 case '?': show_help(); break;
  48.  
  49.                 case 'h': move_player(-1, 0); break;
  50.                 case 'j': move_player(0, +1); break;
  51.                 case 'k': move_player(0, -1); break;
  52.                 case 'l': move_player(+1, 0); break;
  53.                 case 'y': move_player(-1, -1); break;
  54.                 case 'u': move_player(+1, -1); break;
  55.                 case 'b': move_player(-1, +1); break;
  56.                 case 'n': move_player(+1, +1); break;
  57.  
  58.                 case '>':
  59.                     if (player.under_ch == '%')
  60.                     {
  61.                         if (found_amulet && dungeon_level == 1)
  62.                             win_game();
  63.  
  64.                         if (found_amulet) dungeon_level--;
  65.                         else dungeon_level++;
  66.  
  67.                         new_level();
  68.                         update_status();
  69.                     }
  70.                     else
  71.                     {
  72.                         if (found_amulet) set_message("there is no way up");
  73.                         else set_message("there is no way down");
  74.                     }
  75.                     break;
  76.  
  77.                 case (char)18: // ^R
  78.                     set_message(last_message);
  79.                     break;
  80.  
  81.                 case 'Q':
  82.                     Environment.Exit(0);
  83.                     break;
  84.             }
  85.         }
  86.     }
  87.  
  88.     #region dungeon level
  89.  
  90.     static int dungeon_level;
  91.  
  92.     static void new_level()
  93.     {
  94.         CONSOLE.clear();
  95.  
  96.         // add some random level bits
  97.         for (int c = 0; c < 80; c++)
  98.         {
  99.             int x = random.Next(80);
  100.             int y = 1 + random.Next(23);
  101.             char ch = "-|.+#"[random.Next(5)];
  102.             CONSOLE.put_char(x, y, ch);
  103.         }
  104.  
  105.         // add some gold
  106.         for (int c = 0; c < 9; c++)
  107.         {
  108.             int x = random.Next(80);
  109.             int y = 1 + random.Next(23);
  110.             CONSOLE.put_char(x, y, '*');
  111.         }
  112.  
  113.         // add some items
  114.         for (int c = 0; c < 9; c++)
  115.         {
  116.             int x = random.Next(80);
  117.             int y = 1 + random.Next(23);
  118.             char ch = ":!?)]=/"[random.Next(7)];
  119.             CONSOLE.put_char(x, y, ch);
  120.         }
  121.  
  122.         // add some monsters
  123.         for (int c = 0; c < 9; c++)
  124.         {
  125.             int x = random.Next(80);
  126.             int y = 1 + random.Next(23);
  127.             char ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[random.Next(26)];
  128.             CONSOLE.put_char(x, y, ch);
  129.         }
  130.  
  131.         // add the amulet
  132.         if (dungeon_level >= 26)
  133.         {
  134.             int x = random.Next(80);
  135.             int y = 1 + random.Next(23);
  136.             CONSOLE.put_char(x, y, ',');
  137.         }
  138.  
  139.         // add the stairs
  140.         int stairs_x = random.Next(80);
  141.         int stairs_y = 1 + random.Next(23);
  142.         CONSOLE.put_char(stairs_x, stairs_y, '%');
  143.  
  144.         // place the player
  145.         player.x = random.Next(80);
  146.         player.y = 1 + random.Next(23);
  147.         player.under_ch = ' ';
  148.         player.draw();
  149.     }
  150.  
  151.     #endregion // dungeon level
  152.  
  153.     #region commands
  154.  
  155.     static void show_help()
  156.     {
  157.         string[] help_text = new string[]
  158.         {
  159.             "?    show help",
  160.             "h    move left",
  161.             "j    move down",
  162.             "k    move up",
  163.             "l    move right",
  164.             "y    move up & left",
  165.             "u    move up & right",
  166.             "b    move down & left",
  167.             "n    move down & right",
  168.             ">    use stairs",
  169.             "^R   repeat last message",
  170.             "Q    quit game",
  171.         };
  172.  
  173.         CONSOLE.back_up_buffer();
  174.  
  175.         CONSOLE.clear();
  176.  
  177.         for (int h = 0; h < help_text.Length; h++)
  178.             CONSOLE.write_string(0, h, help_text[h]);
  179.  
  180.         CONSOLE.write_string(0, 24, "[press any key]");
  181.  
  182.         CONSOLE.read_key();
  183.  
  184.         CONSOLE.restore_buffer();
  185.     }
  186.  
  187.     static void move_player(int dx, int dy)
  188.     {
  189.         clear_message();
  190.  
  191.         int dest_x = player.x + dx;
  192.         int dest_y = player.y + dy;
  193.         char dest_ch = CONSOLE.get_char(dest_x, dest_y);
  194.  
  195.         // walls stop movement
  196.         if (is_wall(dest_ch))
  197.             return;
  198.  
  199.         // fight monster
  200.         if (is_monster(dest_ch))
  201.         {
  202.             CONSOLE.put_char(dest_x, dest_y, ' ');
  203.  
  204.             set_message("defeated " + get_name(dest_ch));
  205.  
  206.             return;
  207.         }
  208.  
  209.         player.erase();
  210.  
  211.         player.get_eraser(dest_x, dest_y);
  212.  
  213.         player.x = dest_x;
  214.         player.y = dest_y;
  215.  
  216.         player.draw();
  217.  
  218.         // pick up gold
  219.         if (dest_ch == '*')
  220.         {
  221.             player.under_ch = ' ';
  222.  
  223.             set_message("picked up gold");
  224.         }
  225.  
  226.         // pick up item
  227.         if (is_item(dest_ch))
  228.         {
  229.             player.under_ch = ' ';
  230.  
  231.             set_message("picked up " + get_name(dest_ch));
  232.  
  233.             if (dest_ch == ',')
  234.                 found_amulet = true;
  235.         }
  236.     }
  237.  
  238.     #endregion // commands
  239.  
  240.     #region helper functions
  241.  
  242.     static bool is_wall(char ch)
  243.     {
  244.         return "-|".Contains(ch.ToString());
  245.     }
  246.  
  247.     static bool is_monster(char ch)
  248.     {
  249.         return char.IsLetter(ch);
  250.     }
  251.  
  252.     static bool is_item(char ch)
  253.     {
  254.         return ":!?)]=/,".Contains(ch.ToString());
  255.     }
  256.  
  257.     static string get_name(char dest_ch)
  258.     {
  259.         switch (dest_ch)
  260.         {
  261.             case 'A': return "giant ant";
  262.             case 'B': return "bat";
  263.             case 'C': return "centaur";
  264.             case 'D': return "dragon";
  265.             case 'E': return "floating eye";
  266.             case 'F': return "violet fungi";
  267.             case 'G': return "gnome";
  268.             case 'H': return "hobgoblin";
  269.             case 'I': return "invisible stalker";
  270.             case 'J': return "jackal";
  271.             case 'K': return "kobold";
  272.             case 'L': return "leprechaun";
  273.             case 'M': return "mimic";
  274.             case 'N': return "nymph";
  275.             case 'O': return "orc";
  276.             case 'P': return "purple worm";
  277.             case 'Q': return "quasit";
  278.             case 'R': return "rust monster";
  279.             case 'S': return "snake";
  280.             case 'T': return "troll";
  281.             case 'U': return "umber hulk";
  282.             case 'V': return "vampire";
  283.             case 'W': return "wraith";
  284.             case 'X': return "xorn";
  285.             case 'Y': return "yeti";
  286.             case 'Z': return "zombie";
  287.  
  288.             case ':': return "food";
  289.             case '!': return "potion";
  290.             case '?': return "scroll";
  291.             case ')': return "weapon";
  292.             case ']': return "armor";
  293.             case '=': return "ring";
  294.             case '/': return "stick";
  295.             case ',': return "the amulet of yendor";
  296.  
  297.             default: return null;
  298.         }
  299.     }
  300.  
  301.     static void update_status()
  302.     {
  303.         clear_status();
  304.  
  305.         CONSOLE.write_string(0, 24, "level: " + dungeon_level);
  306.     }
  307.  
  308.     #endregion // helper functions
  309.  
  310.     #region messages
  311.  
  312.     static string last_message = "";
  313.  
  314.     static void set_message(string text)
  315.     {
  316.         CONSOLE.write_string(0, 0, text);
  317.         last_message = text;
  318.     }
  319.  
  320.     static void clear_message()
  321.     {
  322.         CONSOLE.cursor_off();
  323.         CONSOLE.write_string(0, 0, "".PadRight(80));
  324.         CONSOLE.cursor_on();
  325.     }
  326.  
  327.     static void clear_status()
  328.     {
  329.         CONSOLE.write_string(0, 24, "".PadRight(79));
  330.     }
  331.  
  332.     #endregion // messages
  333.  
  334.     #region game over
  335.  
  336.     static void win_game()
  337.     {
  338.         CONSOLE.clear();
  339.  
  340.         CONSOLE.write_string("you win");
  341.  
  342.         CONSOLE.read_key();
  343.  
  344.         Environment.Exit(0);
  345.     }
  346.  
  347.     #endregion // game over
  348. }
  349.  
  350. class THING
  351. {
  352.     public int x, y;
  353.     public char under_ch;
  354.  
  355.     public void get_eraser(int x, int y)
  356.     {
  357.         this.under_ch = CONSOLE.get_char(x, y);
  358.     }
  359.  
  360.     public void erase()
  361.     {
  362.         CONSOLE.put_char(this.x, this.y, this.under_ch);
  363.     }
  364.  
  365.     public void draw()
  366.     {
  367.         CONSOLE.put_char(this.x, this.y, '@');
  368.     }
  369. }
  370.  
  371. class CONSOLE
  372. {
  373.     static char[,] buffer = new char[80, 25];
  374.  
  375.     static int cursor_x, cursor_y;
  376.    
  377.     public static char get_char(int x, int y)
  378.     {
  379.         return buffer[x, y];
  380.     }
  381.  
  382.     public static void put_char(char ch)
  383.     {
  384.         buffer[cursor_x, cursor_y] = ch;
  385.         cursor_x++;
  386.         if (cursor_x == 80)
  387.         {
  388.             cursor_x = 0;
  389.             cursor_y++;
  390.         }
  391.  
  392.         Console.Write(ch);
  393.     }
  394.  
  395.     public static void put_char(int x, int y, char ch)
  396.     {
  397.         move_cursor(x, y);
  398.         put_char(ch);
  399.     }
  400.  
  401.     public static void move_cursor(int x, int y)
  402.     {
  403.         cursor_x = x;
  404.         cursor_y = y;
  405.  
  406.         Console.SetCursorPosition(x, y);
  407.     }
  408.  
  409.     public static char read_key()
  410.     {
  411.         return Console.ReadKey(true).KeyChar;
  412.     }
  413.  
  414.     public static void write_string(string text)
  415.     {
  416.         for (int c = 0; c < text.Length; c++)
  417.             put_char(text[c]);
  418.     }
  419.  
  420.     public static void write_string(int x, int y, string text)
  421.     {
  422.         move_cursor(x, y);
  423.         write_string(text);
  424.     }
  425.  
  426.     public static void cursor_on()
  427.     {
  428.         Console.CursorVisible = true;
  429.     }
  430.  
  431.     public static void cursor_off()
  432.     {
  433.         Console.CursorVisible = false;
  434.     }
  435.  
  436.     public static void clear()
  437.     {
  438.         for (int y = 0; y < 25; y++)
  439.             for (int x = 0; x < 80; x++)
  440.                 buffer[x, y] = ' ';
  441.         cursor_x = cursor_y = 0;
  442.  
  443.         Console.Clear();
  444.     }
  445.  
  446.     static char[,] old_buffer = new char[80, 25];
  447.  
  448.     public static void back_up_buffer()
  449.     {
  450.         Array.Copy(CONSOLE.buffer, old_buffer, 80 * 25);
  451.     }
  452.  
  453.     public static void restore_buffer()
  454.     {
  455.         Array.Copy(old_buffer, CONSOLE.buffer, 80 * 25);
  456.  
  457.         for (int y = 0; y < 25; y++)
  458.             for (int x = 0; x < 80; x++)
  459.                 if (!(x == 79 && y == 24))
  460.                     CONSOLE.put_char(x, y, CONSOLE.buffer[x, y]);
  461.     }
  462. }
  463.  
Add Comment
Please, Sign In to add comment