VR引擎程序设计基础1028pm

55 阅读3分钟

集合类 链表类

LinkedList<string> list = new LinkedList<string>();

添加

list.AddFirst("头");//插入头结点的前面。传入插入的结点值

拿到头结点

 Console.WriteLine(list.First.Value);//list.First表示找到头结点
            list.AddFirst("尾结点");
            Console.WriteLine(list.Last.Value);//list.Last找到尾结点

            list.AddBefore(list.First,"新头");//表示在某个结点的前面插入一个新的结点值为新头
            Console.WriteLine(list.Last.Value);

找到指定的结点,没有找到返回空

LinkedListNode<string> node = list.Find("尾结点");
            Console.WriteLine(node.Value);

remove移除

list.Remove("新尾");
            Console.WriteLine(list.Last.Value);
            list.RemoveLast();//removeFirst
            Console.WriteLine(list.Last.Value);

查询该节点是否在链表中

Console.WriteLine(list.Contains("新头"));

遍历链表

foreach(string i in list)
            {
                Console.WriteLine(i);
            }
//Console.WriteLine(list.Last.Previous.Value);//找出最后一个节点的上一个节点
        }

image.png

一、进程和线程

1.什么是进程?

进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,简单理解就是我们每打开一个应用程序就是在操作系统上开启了一个进程。

2.什么是线程

操作系统能够进行运算调度的最小单位,包含在进程中,是进程中的实际运作单位,一个线程指的是进程中的单一顺序的控制流,一个进程可以并发多个线程,我们目前写的程序都在主线程中,简单理解就是代码从到下运行的一条“管道” image.png

3.什么是多线程

我们可以通过代码开启新的线程,可以同时运行多条“管道”,运行多个线程 image.png

image.png

//创建一个方块类
        public class Block
        {
            public Block(int x, int y)
            {
                this.x = x * 2;
                this.y = y;
            }
            public int x;
            public int y;
            public ConsoleColor color = ConsoleColor.White;

            //方块都有一个画的方法  
            public void draw()
            {
                Console.SetCursorPosition(this.x, this.y);
                Console.ForegroundColor = color;
                Console.Write("■");
            }
            //清除的方法  
            public void clear()
            {
                Console.SetCursorPosition(this.x, this.y);
                Console.Write("  ");
            }

        }
        //创建一个子类  头
        public class Head : Block
        {
            public Head(int x, int y, Dir dir = Dir.right) : base(x, y)
            {
                this.dir = dir;
                base.color = ConsoleColor.Red;
            }
            public Dir dir;
            //移动的方法
            public void move()
            {
                switch (dir)
                {
                    case Dir.up:
                        y--;
                        break;
                    case Dir.down:
                        y++;
                        break;
                    case Dir.left:
                        x -= 2;
                        break;
                    case Dir.right:
                        x += 2;
                        break;
                }
            }

            //碰撞检测的方法  
            public bool crash(Block block)
            {
                if (block.x == this.x && block.y == this.y)
                {
                    return true;
                }
                return false;
            }

        }

        //创建一个枚举来存方向 
        public enum Dir
        {
            up, down, left, right
        }

        internal class Program
        {
            public static object obj = new object();
            public static int width = Console.BufferWidth;//屏幕宽度
            public static int height = Console.BufferHeight;//屏幕高度 
                                                            //创建一个不动方块对象 
            public static Block block = new Block(5, 5);
            //创建一个头  
            public static Head head = new Head(0, 0);
            //创建一个列表存每一个方块  
            public static List<Head> list = new List<Head>();

            //更新不动的方块  
            public static void update()
            {
                Random random = new Random();
                int x = random.Next(0, 30) * 2;
                int y = random.Next(0, 30);
                block.x = x;
                block.y = y;
                block.draw();
            }
            static void Main(string[] args)
            {
                Console.CursorVisible = false;

                list.Add(head);
                list[0].draw();
                //创建一个线程
                Thread T = new Thread(ThreadLogic);
                Thread T2 = new Thread(ThreadLogic2);
                T2.Start();
                T.Start();
                T.IsBackground = true;
                T2.IsBackground = true;
                while (true)
                {
                    lock (obj)
                    {
                        Thread.Sleep(250);
                        list[0].clear();
                        list[0].move();
                        list[0].draw();
                    }
                }

            }
            static void ThreadLogic2()
            {
                block.draw();
                while (true)
                {
                    lock (obj)
                    {
                        if (list[0].crash(block))
                        {
                            update();

                        };//碰完要更新block的位置     
                    }
                }
            }
            static void ThreadLogic()
            {

                while (true)
                {

                    switch (Console.ReadKey(true).Key)//检测用户输入的按键  
                    {
                        case ConsoleKey.UpArrow:
                            //只有一个头随便跑  如果不是一个方块那么不能往反方向走 
                            if (head.dir != Dir.down || list.Count == 1) head.dir = Dir.up;
                            break;
                        case ConsoleKey.DownArrow:
                            //只有一个头随便跑  如果不是一个方块那么不能往反方向走 
                            if (head.dir != Dir.up || list.Count == 1) head.dir = Dir.down;
                            break;
                        case ConsoleKey.LeftArrow:
                            //只有一个头随便跑  如果不是一个方块那么不能往反方向走 
                            if (head.dir != Dir.right || list.Count == 1) head.dir = Dir.left;
                            break;
                        case ConsoleKey.RightArrow:
                            //只有一个头随便跑  如果不是一个方块那么不能往反方向走 
                            if (head.dir != Dir.left || list.Count == 1) head.dir = Dir.right;
                            break;

                    }
                }
            }
        }
    }

image.png