kisame1313

2.4.1.3

Jul 25th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 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.     //В следующем коде, сделайте добавление NPC перед началом цикла отрисовки.
  10.     //NPC должны добавляться в массив в случайном количестве со случайными данными.
  11.  
  12. namespace IMJunior
  13. {
  14.     class Program
  15.     {
  16.         static Random R;
  17.  
  18.         static void Main ( string [] args )
  19.         {
  20.             R = new Random ();
  21.  
  22.             Console.CursorVisible = false;
  23.             NPC [] NPCs;
  24.  
  25.             while ( true )
  26.             {
  27.                 Console.Clear ();
  28.                 NPCs = new NPC [R.Next (1,6)];
  29.                 for ( int i =0 ; i < NPCs.Length; i++ )
  30.                 {
  31.                     NPCs [i] = new NPC ();
  32.                     NPCs[i].Visual = '#';
  33.                     NPCs [i].Foreground = RandomColor ();
  34.                     NPCs [i].Background = RandomColor ();
  35.                     NPCs [i].X = NPCs [i].Y = R.Next(1,11);
  36.                     MoveNPC ( NPCs [i] );
  37.                     RenderNPC ( NPCs [i] );
  38.                 }
  39.                 Thread.Sleep ( 500 );
  40.             }
  41.         }
  42.  
  43.         static ConsoleColor RandomColor ()
  44.         {
  45.             Array consoleColor = Enum.GetValues ( typeof ( ConsoleColor ) );
  46.             return ( ConsoleColor ) R.Next ( consoleColor.Length);
  47.         }
  48.  
  49.         static void RenderNPC ( NPC npc )
  50.         {
  51.             Console.SetCursorPosition ( npc.X, npc.Y );
  52.             Console.ForegroundColor = npc.Foreground;
  53.             Console.BackgroundColor = npc.Background;
  54.             Console.WriteLine ( npc.Visual );
  55.         }
  56.  
  57.         static void MoveNPC ( NPC npc )
  58.         {
  59.             npc.X = Clamp ( npc.X + R.Next ( -1, 2 ), 1, Console.WindowWidth );
  60.             npc.Y = Clamp ( npc.Y + R.Next ( -1, 2 ), 1, Console.WindowHeight );
  61.  
  62.         }
  63.  
  64.         static int Clamp ( int value, int min, int max )
  65.         {
  66.             return ( value < min ) ? min : ( value > max ) ? max : value;
  67.         }
  68.  
  69.     }
  70.  
  71.     class NPC
  72.     {
  73.         public char Visual;
  74.         public int X, Y;
  75.         public ConsoleColor Foreground, Background;
  76.     }
  77. }
Add Comment
Please, Sign In to add comment