Advertisement
MrVeiran

С# Func LifeBar

Mar 13th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 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 ConsoleApp42
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int end = 0;
  14.             Console.CursorVisible = false;
  15.             int width = Console.WindowWidth;
  16.             Console.SetCursorPosition(width / 2 - 24, 7);
  17.  
  18.             Console.Write("Тестирование шкалы здоровья");
  19.             Console.SetCursorPosition(width / 2 - 24, 9);
  20.  
  21.             Console.Write("Доступны два удара. Первый удар на 50, второй на 100");
  22.             Console.SetCursorPosition(width / 2 - 24, 11);
  23.  
  24.  
  25.             Console.Write("Нажмите любую клавишу");
  26.             Console.ReadLine();
  27.             Console.Clear();
  28.             int MaxLife = 1000;
  29.             int Life = 1000;
  30.             int hit = 0;
  31.  
  32.             int zero = 0;
  33.             while (true)
  34.             {
  35.  
  36.                 Console.SetCursorPosition(width / 2 - 24, 7);
  37.                 Console.Write("Выберите удар клавишей 1 или 2");
  38.                 if (zero == 0)
  39.                 {
  40.                     Console.SetCursorPosition(26, 9);
  41.                     Console.Write("Здоровье: ");
  42.                     Console.Write(Life);
  43.                     ConsoleColor DefaultColor = Console.BackgroundColor;
  44.                     Console.SetCursorPosition(24, 10);
  45.                     Console.Write("[");
  46.                     Console.BackgroundColor = ConsoleColor.Red;
  47.                     Console.SetCursorPosition(25, 10);
  48.                     Console.Write("##########");
  49.                     Console.BackgroundColor = DefaultColor;
  50.                     Console.Write("]");
  51.  
  52.                 }
  53.  
  54.                 zero = 1;
  55.                 ConsoleKeyInfo charKey = Console.ReadKey();
  56.                 switch (charKey.Key)
  57.                 {
  58.                     case ConsoleKey.D1:
  59.                         Console.Write("\b \b");
  60.                         Console.SetCursorPosition(width / 2 - 24, 8);
  61.                         hit = 50;
  62.                         Life = draw(Life,MaxLife, hit, "Здоровье", ConsoleColor.Red);
  63.                         if (Life <= 0)
  64.                             end = 5;
  65.                         break;
  66.                     case ConsoleKey.D2:
  67.                         Console.Write("\b \b");
  68.                         Console.SetCursorPosition(width / 2 - 24, 8);
  69.                         hit = 100;
  70.                         Life = draw(Life,MaxLife, hit, "Здоровье", ConsoleColor.Red);
  71.                         if (Life <= 0)
  72.                         {
  73.                             end = 5;
  74.                         }
  75.                         break;
  76.                 }
  77.                 hit = 0;
  78.                 if (end > 2)
  79.                     break;
  80.             }
  81.             Console.WriteLine();
  82.         }
  83.  
  84.         static int draw(int Value, int MaxLife,int hit, string name, ConsoleColor color, char barchar='#')
  85.         {
  86.             ConsoleColor DefaultColor = Console.BackgroundColor;
  87.  
  88.             Console.Clear();
  89.             Value -= hit;
  90.             if (Value < 0)
  91.                 Value = 0;
  92.             Console.SetCursorPosition(26, 9);
  93.             Console.Write(name + ": " + Value);
  94.  
  95.             Console.SetCursorPosition(24, 10);
  96.             Console.SetCursorPosition(24, 10);
  97.             Console.Write("[");
  98.             Console.BackgroundColor = color;
  99.             string bar = "";
  100.             for (int i=0;i<Value/100;i++)
  101.             {
  102.                 bar += barchar;
  103.             }
  104.             Console.Write(bar);
  105.             Console.BackgroundColor = DefaultColor;
  106.             bar = "";
  107.             barchar = ' ';
  108.             for (int i = Value/100; i < MaxLife/100; i++)
  109.                 bar += barchar;
  110.  
  111.             Console.Write(bar);
  112.             Console.BackgroundColor = DefaultColor;
  113.             Console.Write("]");
  114.             return Value;
  115.  
  116.  
  117.  
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement