Advertisement
AbitDeveloper

C# Multi-Thread Auta

May 6th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace Auta
  6. {
  7.     public class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             Init();
  12.  
  13.             while (true)
  14.             {
  15.                 Render();
  16.                 Thread.Sleep(1000 / 30);
  17.             }
  18.         }
  19.  
  20.         public static List<Car> cars = new List<Car>();
  21.  
  22.         public static ConsoleColor[] colors = new ConsoleColor[] { ConsoleColor.Red, ConsoleColor.Blue, ConsoleColor.Green };
  23.  
  24.         public static void Init(int count = 20)
  25.         {
  26.             Random rnd = new Random();
  27.             for (; count > 0; count--)
  28.                 cars.Add(new Car(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 1024) / Math.PI * 2, colors[rnd.Next(colors.Length)]));
  29.         }
  30.  
  31.         public static void Render()
  32.         {
  33.             foreach (var car in cars)
  34.             {
  35.                 double x, y, angle;
  36.                 ConsoleColor color;
  37.                 lock (car)
  38.                 {
  39.                     x = car.X;
  40.                     y = car.Y;
  41.                     angle = car.AngleRad;
  42.                     color = car.Color;
  43.                 }
  44.                 RenderCar(x, y, angle, color);
  45.             }
  46.         }
  47.  
  48.         private static void RenderCar(double x, double y, double angle, ConsoleColor color)
  49.         {
  50.             throw new NotImplementedException();
  51.         }
  52.     }
  53.     public class Car
  54.     {
  55.         public Car(double x, double y, double angleRad, ConsoleColor color)
  56.         {
  57.             this.X = x;
  58.             this.Y = y;
  59.             this.AngleRad = angleRad;
  60.  
  61.             this.Thread = new Thread(() =>
  62.             {
  63.                 while (true)
  64.                 {
  65.                     Thread.Sleep(100);
  66.                     lock (this)
  67.                     {
  68.                         Tick(0.1f);
  69.                     }
  70.                 }
  71.             });
  72.             this.Thread.Start();
  73.         }
  74.  
  75.         public double X
  76.         {
  77.             get;
  78.             set;
  79.         }
  80.  
  81.         public double Y
  82.         {
  83.             get;
  84.             set;
  85.         }
  86.  
  87.         public ConsoleColor Color
  88.         {
  89.             get;
  90.             set;
  91.         }
  92.  
  93.         public double AngleRad
  94.         {
  95.             get;
  96.             set;
  97.         }
  98.  
  99.         public double Speed
  100.         {
  101.             get;
  102.             set;
  103.         }
  104.  
  105.         public void Destroy()
  106.         {
  107.             Thread.Abort();
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Perform a tick
  112.         /// </summary>
  113.         /// <param name="delta">Time in seconds of duration of tick</param>
  114.         public void Tick(float delta)
  115.         {
  116.             double y = Math.Sin(AngleRad) * Speed * delta;
  117.             double x = Math.Cos(AngleRad) * Speed * delta;
  118.  
  119.             //[0,0] = top left tick
  120.             X += x;
  121.             Y -= y;
  122.         }
  123.  
  124.         private Thread Thread;
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement