Advertisement
tehKaiN

Napierdalanka 30.05.2015

May 29th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace graNaKonsole
  8. {
  9.  
  10.     class hud
  11.     {
  12.         static Byte bHpBarLength = 20;
  13.         static public void drawHP()
  14.         {
  15.             Byte bHp = map.oPlayer.bHP;
  16.             Byte bMaxHp = map.oPlayer.bMaxHP;
  17.             Double dHpPercent = Convert.ToDouble(bHp) / Convert.ToDouble(bMaxHp);
  18.             ConsoleColor ccLight, ccDark;
  19.  
  20.             if(dHpPercent <= 0.25)
  21.             {
  22.                 ccLight = ConsoleColor.Red;
  23.                 ccDark = ConsoleColor.DarkRed;
  24.             }
  25.             else if(dHpPercent <= 0.5)
  26.             {
  27.                 ccLight = ConsoleColor.Yellow;
  28.                 ccDark = ConsoleColor.DarkYellow;
  29.             }
  30.             else
  31.             {
  32.                 ccLight = ConsoleColor.Green;
  33.                 ccDark = ConsoleColor.DarkGreen;
  34.             }
  35.  
  36.       char[] szBarOutTxt = new char[bHpBarLength];
  37.             string szBarText = String.Format("{0}/{1}", bHp, bMaxHp);
  38.             szBarText.CopyTo(0, szBarOutTxt, (bHpBarLength + 1 - szBarText.Length) / 2, szBarText.Length);
  39.  
  40.             Console.ForegroundColor = ConsoleColor.Red;
  41.             Console.SetCursorPosition(0, 0);
  42.             Console.Write(" \x03 ");
  43.  
  44.             Console.ForegroundColor = ccDark;
  45.             Console.BackgroundColor = ccLight;
  46.             Console.Write(szBarOutTxt.Take(bHpBarLength * bHp / bMaxHp).ToArray());
  47.  
  48.             Console.ForegroundColor = ccLight;
  49.             Console.BackgroundColor = ccDark;
  50.             Console.Write(szBarOutTxt.Skip(bHpBarLength * bHp / bMaxHp).ToArray());
  51.  
  52.             Console.BackgroundColor = ConsoleColor.Black;
  53.         }
  54.         static public void drawAll()
  55.         {
  56.             drawHP();
  57.         }
  58.     }
  59.  
  60.     class map
  61.     {
  62.         static public List<entity> lstEntities;
  63.         static public living oPlayer;
  64.         static public Byte bX, bY;
  65.         static public Byte bMarginY;
  66.         static public bool[,] aWalls;
  67.  
  68.         static public void generate(Byte x, Byte y)
  69.         {
  70.             lstEntities = new List<entity>();
  71.             bX = x;
  72.             bY = y;
  73.             bMarginY = 1;
  74.             aWalls = new bool[bX, bY];
  75.  
  76.             Console.SetBufferSize(x, y+10);
  77.             Console.SetWindowSize(x, y+10);
  78.  
  79.             Random rr = new Random();
  80.             for (x = 0; x != bX; ++x)
  81.                 for (y = 0; y != bY; ++y)
  82.                 {
  83.                     if (x == 0 || x == bX - 1 || y == 0 || y == bY - 1)
  84.                         aWalls[x, y] = true;
  85.                     else
  86.                     {
  87.                         if (rr.Next(10) < 1)
  88.                             aWalls[x, y] = true;
  89.                         else
  90.                             aWalls[x, y] = false;
  91.                     }
  92.                 }
  93.             display();
  94.         }
  95.  
  96.         static public void display()
  97.         {
  98.             char[] aBfr = new char[bX*bY];
  99.             Byte x, y;
  100.             // fill bfr with walls
  101.             Console.ForegroundColor = ConsoleColor.DarkGray;
  102.             for (y = 0; y != bY; ++y)
  103.             {
  104.                 for (x = 0; x != bX; ++x)
  105.                     if (map.aWalls[x, y])
  106.                         aBfr[x + bX*y] = '#';
  107.                     else
  108.                         aBfr[x + bX*y] = ' ';
  109.             }
  110.  
  111.             Console.SetCursorPosition(0, bMarginY);
  112.             Console.Write(aBfr);
  113.             //Console.SetWindowPosition(0, 0);
  114.             Console.SetCursorPosition(0, 0);
  115.         }
  116.  
  117.     }
  118.  
  119.     class entity:IEquatable<entity>
  120.     {
  121.         public Byte bPosX, bPosY;
  122.         Int32 lId;
  123.         Char cAppearance;
  124.         ConsoleColor oColor;
  125.  
  126.         public bool Equals(entity ent)
  127.         {          
  128.             if (this.lId == ent.lId)
  129.                 return true;
  130.             return false;
  131.         }
  132.  
  133.         public void draw()
  134.         {
  135.             Console.SetCursorPosition(bPosX, bPosY + map.bMarginY);
  136.             Console.ForegroundColor = oColor;
  137.             Console.Write(cAppearance);
  138.         }
  139.  
  140.         public entity(Byte x, Byte y, Char c, ConsoleColor cc)
  141.         {
  142.             bPosX = x;
  143.             bPosY = y;
  144.             cAppearance = c;
  145.             oColor = cc;
  146.             draw();
  147.             map.lstEntities.Add(this);
  148.             lId = map.lstEntities.Count;
  149.         }
  150.         public entity(Char c, ConsoleColor cc)
  151.         {
  152.             Random rr = new Random();
  153.             bool done = false;
  154.             while(!done)
  155.             {
  156.                 bPosX = (Byte)rr.Next(1, map.bX-1);
  157.                 bPosY = (Byte)rr.Next(1, map.bY-1);
  158.                 done = true;
  159.                 foreach(entity ent in map.lstEntities)
  160.                 {
  161.                     if (ent.bPosX == bPosX && ent.bPosY == bPosY)
  162.                     {
  163.                         done = false;
  164.                         break;
  165.                     }
  166.                 }
  167.             }
  168.  
  169.             cAppearance = c;
  170.             oColor = cc;
  171.             draw();
  172.             map.lstEntities.Add(this);
  173.             lId = map.lstEntities.Count;
  174.         }
  175.  
  176.         public void erase()
  177.         {
  178.             Console.SetCursorPosition(bPosX, bPosY + map.bMarginY);
  179.             Console.Write(' ');
  180.         }
  181.  
  182.         public void move(SByte dx, SByte dy)
  183.         {
  184.             if (map.aWalls[bPosX + dx, bPosY + dy])
  185.                 return;
  186.             foreach(entity ent in map.lstEntities)
  187.                 if((ent.bPosX == bPosX + dx) && (ent.bPosY == bPosY + dy))
  188.                 {
  189.                     ent.interact();
  190.                     return;
  191.                 }
  192.  
  193.             erase();
  194.             bPosX = (Byte)(bPosX + dx);
  195.             bPosY = (Byte)(bPosY + dy);
  196.             draw();
  197.         }
  198.  
  199.         public virtual void interact()
  200.         {
  201.  
  202.         }
  203.  
  204.     }
  205.  
  206.     class living:entity
  207.     {
  208.         public Byte bHP;
  209.         public Byte bMaxHP;
  210.  
  211.         public living(Byte x, Byte y, Char c, ConsoleColor cc, Byte hp, Byte maxHP): base(x,y,c,cc)
  212.         {
  213.             bHP = hp;
  214.             bMaxHP = maxHP;
  215.         }
  216.     }
  217.  
  218.     class potion: entity
  219.     {
  220.         Byte bHP;
  221.  
  222.         public override void interact()
  223.         {
  224.             map.oPlayer.bHP = (Byte)Math.Min(map.oPlayer.bHP+bHP, map.oPlayer.bMaxHP);
  225.             this.erase();
  226.             map.lstEntities.Remove(this);
  227.             hud.drawHP();
  228.         }
  229.  
  230.         public potion(Byte x, Byte y, Byte hp): base(x, y, '\x03', ConsoleColor.Red)
  231.         {
  232.             bHP = hp;
  233.         }
  234.         public potion(Byte hp): base('\x03', ConsoleColor.Red)
  235.         {
  236.             bHP = hp;
  237.     }
  238.     }
  239.  
  240.     class Program
  241.     {
  242.         static void Main(string[] args)
  243.         {
  244.             Console.CursorVisible = false;
  245.             map.generate(80, 25);
  246.  
  247.             map.oPlayer = new living(5, 5, '\x02', ConsoleColor.Green, 5, 50);
  248.  
  249.             for(Byte i = 0; i < 20; ++i)
  250.             {
  251.                 new potion(10);
  252.             }
  253.  
  254.             hud.drawAll();
  255.  
  256.             while (true)
  257.             {
  258.                 if (Console.KeyAvailable)
  259.                 {
  260.                     ConsoleKey kk = Console.ReadKey(true).Key;
  261.  
  262.                     switch (kk)
  263.                     {
  264.                         case ConsoleKey.Escape:
  265.                             return;
  266.                         case ConsoleKey.W:
  267.                             map.oPlayer.move(0, -1);
  268.                             break;
  269.                         case ConsoleKey.S:
  270.                             map.oPlayer.move(0, 1);
  271.                             break;
  272.                         case ConsoleKey.A:
  273.                             map.oPlayer.move(-1, 0);
  274.                             break;
  275.                         case ConsoleKey.D:
  276.                             map.oPlayer.move(1, 0);
  277.                             break;
  278.                     }
  279.                 }
  280.             }
  281.         }
  282.     }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement