Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace Auta
- {
- public class MainClass
- {
- public static void Main(string[] args)
- {
- Init();
- while (true)
- {
- Render();
- Thread.Sleep(1000 / 30);
- }
- }
- public static List<Car> cars = new List<Car>();
- public static ConsoleColor[] colors = new ConsoleColor[] { ConsoleColor.Red, ConsoleColor.Blue, ConsoleColor.Green };
- public static void Init(int count = 20)
- {
- Random rnd = new Random();
- for (; count > 0; count--)
- cars.Add(new Car(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 1024) / Math.PI * 2, colors[rnd.Next(colors.Length)]));
- }
- public static void Render()
- {
- foreach (var car in cars)
- {
- double x, y, angle;
- ConsoleColor color;
- lock (car)
- {
- x = car.X;
- y = car.Y;
- angle = car.AngleRad;
- color = car.Color;
- }
- RenderCar(x, y, angle, color);
- }
- }
- private static void RenderCar(double x, double y, double angle, ConsoleColor color)
- {
- throw new NotImplementedException();
- }
- }
- public class Car
- {
- public Car(double x, double y, double angleRad, ConsoleColor color)
- {
- this.X = x;
- this.Y = y;
- this.AngleRad = angleRad;
- this.Thread = new Thread(() =>
- {
- while (true)
- {
- Thread.Sleep(100);
- lock (this)
- {
- Tick(0.1f);
- }
- }
- });
- this.Thread.Start();
- }
- public double X
- {
- get;
- set;
- }
- public double Y
- {
- get;
- set;
- }
- public ConsoleColor Color
- {
- get;
- set;
- }
- public double AngleRad
- {
- get;
- set;
- }
- public double Speed
- {
- get;
- set;
- }
- public void Destroy()
- {
- Thread.Abort();
- }
- /// <summary>
- /// Perform a tick
- /// </summary>
- /// <param name="delta">Time in seconds of duration of tick</param>
- public void Tick(float delta)
- {
- double y = Math.Sin(AngleRad) * Speed * delta;
- double x = Math.Cos(AngleRad) * Speed * delta;
- //[0,0] = top left tick
- X += x;
- Y -= y;
- }
- private Thread Thread;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement