redSkywalker

Задача 2.4.1.3 (npc)

Feb 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _2._4._1._3npc
  9. {
  10.     class Program
  11.     {
  12.         static Random R;
  13.        
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             R = new Random();
  18.             NPCGenerator generator = new NPCGenerator();
  19.            
  20.             List<NPC> AllNPC = new List<NPC>();
  21.  
  22.             int NPCCount = R.Next(3, 10);
  23.             for(int i=1; i<=NPCCount;i++)
  24.             {
  25.                 NPC newNPC = generator.CreateNPC();
  26.                 AllNPC.Add(newNPC);
  27.             }
  28.  
  29.             Console.CursorVisible = false;
  30.  
  31.             while (true)
  32.             {
  33.                 Console.Clear();
  34.                 foreach(NPC n in AllNPC)
  35.                 {
  36.                     MoveNPC(n);
  37.                     RenderNPC(n);                    
  38.                 }
  39.                 Thread.Sleep(500);
  40.  
  41.             }
  42.         }
  43.  
  44.         static void RenderNPC(NPC npc)
  45.         {
  46.             Console.SetCursorPosition(npc.X, npc.Y);
  47.             Console.ForegroundColor = npc.Foreground;
  48.             Console.BackgroundColor = npc.Background;
  49.             Console.WriteLine(npc.Visual);
  50.         }
  51.  
  52.         static void MoveNPC(NPC npc)
  53.         {
  54.             npc.X = Clamp(npc.X + R.Next(-1, 2), 0, Console.WindowWidth);
  55.             npc.Y = Clamp(npc.Y + R.Next(-1, 2), 0, Console.WindowHeight);
  56.            
  57.  
  58.         }
  59.  
  60.         static int Clamp(int value, int min, int max)
  61.         {
  62.             return (value < min) ? min : (value > max) ? max : value;
  63.         }
  64.  
  65.     }
  66.  
  67.     class NPC
  68.     {
  69.         public char Visual;
  70.         public int X, Y;
  71.         public ConsoleColor Foreground, Background;
  72.  
  73.         public NPC(char ch, int coord, ConsoleColor frgr)
  74.         {
  75.             Visual = ch;
  76.             X = Y = coord;
  77.             Foreground = frgr;
  78.             Background = ConsoleColor.White;
  79.            
  80.         }
  81.     }
  82.  
  83.     class NPCGenerator
  84.     {
  85.         Random r = new Random();
  86.         public char[] CharSource = new char[] {'$', '@', '%', '^','&','*','~','{','#','!' };
  87.         public ConsoleColor[] ColorSource = new ConsoleColor[] {ConsoleColor.DarkRed, ConsoleColor.DarkMagenta, ConsoleColor.Green, ConsoleColor.Red, ConsoleColor.Yellow, ConsoleColor.Gray, ConsoleColor.DarkYellow, ConsoleColor.DarkGreen, ConsoleColor.Blue, ConsoleColor.Magenta };
  88.         public int[] CoordinatesSource = new int[] { 6, 8, 10, 12, 4, 15, 5, 17, 19, 21 };
  89.  
  90.         public NPC CreateNPC()
  91.         {
  92.             char NPCchar = CharSource[r.Next(0, 9)];
  93.             ConsoleColor NPCColor = ColorSource[r.Next(0, 9)];
  94.             int NPCCoordinates = CoordinatesSource[r.Next(0, 9)];
  95.             NPC randomNPC = new NPC(NPCchar, NPCCoordinates, NPCColor);
  96.  
  97.             return randomNPC;
  98.         }
  99.  
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment