Advertisement
Nemo048

Untitled

Jun 15th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Threading;
  8.  
  9. namespace lesson7
  10. {
  11.     class Program
  12.     {
  13.         public static Random r;
  14.         static void Main(string[] args)
  15.         {
  16.             Console.CursorVisible = false;
  17.             r = new Random();
  18.  
  19.             NPC npc = new NPC('%');
  20.             npc.Move();
  21.  
  22.             NPC[] npcs = new NPC[9/*r.Next(0, 100)*/];
  23.  
  24.             for (int i = 0; i < npcs.Length; i++)
  25.             {
  26.                 npcs[i] = new NPC((char)r.Next(97, 119), r.Next(0, 20), r.Next(0, 20), (ConsoleColor)r.Next(0, 15));
  27.             }
  28.  
  29.             Cursor cursorYellow = new Cursor(npcs);
  30.             Cursor cursorRed = new Cursor(npcs);
  31.  
  32.             while (true)
  33.             {
  34.                 Console.Clear();
  35.                 foreach (NPC n in npcs)
  36.                 {
  37.                     if (n.isLive())
  38.                     {
  39.                         n.Move();
  40.                         Console.SetCursorPosition(n.X, n.Y);
  41.  
  42.                         Console.ForegroundColor = n.ForegroundColor;
  43.                         if (cursorYellow.Current() == n && cursorRed.Current() == n)
  44.                         {
  45.                             Console.BackgroundColor = ConsoleColor.Yellow;
  46.                         }
  47.                         else if(cursorRed.Current() == n && cursorYellow.Current() == n)
  48.                         {
  49.                             Console.BackgroundColor = ConsoleColor.Yellow;
  50.                         }
  51.                         else if(cursorYellow.Current() == n)
  52.                         {
  53.                             Console.BackgroundColor = ConsoleColor.Yellow;                     
  54.                         }
  55.                         else if(cursorRed.Current() == n)
  56.                         {
  57.                             Console.BackgroundColor = ConsoleColor.Red;
  58.                         }
  59.                        
  60.  
  61.                         Console.Write(n.Visual);
  62.  
  63.                         foreach (NPC n2 in npcs)
  64.                         {
  65.                             if (n2 != n && n2.isLive())
  66.                             {
  67.                                 if (n2.X == n.X && n.Y == n2.Y)
  68.                                 {
  69.                                     n2.Collision();
  70.                                 }
  71.                             }
  72.                         }
  73.  
  74.                     }
  75.                     Console.ForegroundColor = ConsoleColor.White;
  76.                     Console.BackgroundColor = ConsoleColor.Black;
  77.                 }
  78.                 ConsoleKeyInfo key = Console.ReadKey();
  79.                 if (key.Key == ConsoleKey.Tab)
  80.                 {
  81.                     cursorYellow.MoveNext();
  82.                 }
  83.                 else if(key.Key == ConsoleKey.Enter)
  84.                 {
  85.                     cursorRed.MoveNext();
  86.                 }
  87.             }
  88.         }
  89.  
  90.  
  91.         public static int Clamp(int value, int min, int max)
  92.         {
  93.             return value < min ? min : (value > max) ? max : value;
  94.         }
  95.     }
  96.  
  97.  
  98.     /*
  99.      * 2 метода: isLive, Collision
  100.      * 1 поле: колличество жизней
  101.      * Метод Collision отнимает N жизней, и ставит текущим символов, колличество жизни
  102.      */
  103.     class NPC
  104.     {
  105.         public char Visual;
  106.         public ConsoleColor ForegroundColor;
  107.         public int X, Y;
  108.         public int HP = 100, MaxHP = 100;
  109.  
  110.         public bool isLive()
  111.         {
  112.             return HP > 0;
  113.         }
  114.  
  115.         public void Collision()
  116.         {
  117.             HP -= 10;
  118.             HP = Program.Clamp(HP, 0, 100);
  119.             Visual = (HP / (MaxHP / 9)).ToString()[0];
  120.         }
  121.  
  122.         public NPC(char visual, int x = 5, int y = 5, ConsoleColor fc = ConsoleColor.White)
  123.         {
  124.             Visual = visual;
  125.             X = x;
  126.             Y = y;
  127.             ForegroundColor = fc;
  128.         }
  129.  
  130.         public void Move()
  131.         {
  132.             X = Program.Clamp(X + Program.r.Next(-1, 2), 0, Console.WindowWidth);
  133.             Y = Program.Clamp(Y + Program.r.Next(-1, 2), 0, Console.WindowHeight);
  134.         }
  135.  
  136.         /*public void Toggle()
  137.         {
  138.             ConsoleColor K = BackgroundColor;
  139.             BackgroundColor = ForegroundColor;
  140.             ForegroundColor = K;
  141.         }*/
  142.     }
  143. }
  144.  
  145. /*
  146.  * Инкапсуляция
  147.  * Полиморфизм
  148.  * Наследование
  149.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement