Advertisement
dmitryzenevich

Untitled

Dec 29th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Delegates
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Attack[] attacks = new Attack[3]
  11.             {
  12.                     new Attack(100, ConsoleColor.DarkRed, "Босс атаковал с немыслимой яростью своими руками"),
  13.                     new Attack(140, ConsoleColor.DarkMagenta, "Босс исполнил новый альбом Ольги бузовой"),
  14.                     new Attack(80, ConsoleColor.DarkGray, "Босс приуныл и рассказал вам о своём долгом пути и дал пару советов, после выпил ритуальный стопарь боярки"),
  15.             };
  16.  
  17.             Enemy Boss = new Enemy(attacks);
  18.             Player Player = new Player(1000, 20);
  19.  
  20.             Service.ShowMessage(ConsoleColor.Green, "Нажмите enter для начала боя");
  21.             Console.ReadLine();
  22.  
  23.             Boss.Attack(Player);
  24.         }
  25.     }
  26.     class Enemy
  27.     {
  28.         private Attack[] Attacks { get; set; }
  29.         public bool IsRandomAttack { get; private set; }
  30.         public Enemy(params Attack[] attacks)
  31.         {
  32.             Attacks = attacks;
  33.             IsRandomAttack = (DateTime.Now.Millisecond % 2) == 0;
  34.             Service.ShowMessage(ConsoleColor.Yellow, "Босс может атаковать в двух режимах: все атаки по очереди и случайной атакой");
  35.             Service.ShowMessage(ConsoleColor.Yellow, "Босс будет атаковать: " + (IsRandomAttack ? "случайно" : "все атаки по очереди"));
  36.         }
  37.  
  38.         public void Attack(Player player)
  39.         {
  40.             int attackNumber = 0;
  41.             while (!player.IsDead)
  42.             {
  43.                 int attackIndex = IsRandomAttack ? DateTime.Now.Millisecond % 3 : attackNumber;
  44.                 Attacks[attackIndex].Invoke(player);
  45.                 if (!IsRandomAttack)
  46.                 {
  47.                     attackNumber++;
  48.                     if (attackNumber > Attacks.Length - 1)
  49.                         attackNumber = 0;
  50.                 }
  51.                 Thread.Sleep(1000);
  52.             }
  53.             Console.ReadKey();
  54.         }
  55.     }
  56.     class Attack
  57.     {
  58.         private string Message { get; set; }
  59.         private int Damage { get; set; }
  60.         private ConsoleColor ConsoleColor { get; set; }
  61.         public Attack(int damage, ConsoleColor consoleColor, string message)
  62.         {
  63.             Damage = damage;
  64.             ConsoleColor = consoleColor;
  65.             Message = message;
  66.         }
  67.         public void Invoke(Player player)
  68.         {
  69.             Console.Clear();
  70.             Service.ShowMessage(ConsoleColor, Message);
  71.             player.TakeDamage(Damage);
  72.         }
  73.     }
  74.     class Player
  75.     {
  76.         public int Health { get; private set; }
  77.         public int Armor { get; private set; }
  78.         public bool IsDead { get; private set; }
  79.         public Player(int health, int armor)
  80.         {
  81.             Health = health;
  82.             Armor = armor;
  83.             IsDead = false;
  84.         }
  85.         public void TakeDamage(int damage)
  86.         {
  87.             Service.ShowMessage(ConsoleColor.Red, "У вас здоровья: " + Health);
  88.  
  89.             Health = Health - (damage - Armor);
  90.             if (Health <= 0 && !IsDead)
  91.                 Death();
  92.         }
  93.         private void Death()
  94.         {
  95.             IsDead = true;
  96.  
  97.             Console.Clear();
  98.             Service.ShowMessage(ConsoleColor.DarkGray, "Бой закончен, вы погибли");
  99.         }
  100.     }
  101.     public static class Service
  102.     {
  103.         private static ConsoleColor oldColor = Console.ForegroundColor;
  104.         public static void ShowMessage(ConsoleColor color, string message)
  105.         {
  106.             oldColor = Console.ForegroundColor;
  107.             Console.ForegroundColor = color;
  108.             Console.WriteLine(message);
  109.             Console.ForegroundColor = oldColor;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement