C# 控制台移动元素小 Demo

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.13.6
.NET SDK 9.0.203

正文

演示

image

代码

实现贪吃蛇的话,可以将玩家数据存储为一个链表,可以通过擦除最后一个,新增第一个的形式进行移动。或者整个链表的所有节点位置都进行调整。

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 监听用户输入
            UserInput.Listening();
            // 页面刷新
            ConsoleView.Interface.Render();
            // 
        }
        public class Position{
            public int X;
            public int Y;
            public Position(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        public class Player
        {
            //
            public Position Position = new Position(0, 0);

            public Player() { }

            // 更新
            public void Render()
            {
                string userInput = UserInput.Content;
                switch (userInput)
                {
                    case "W":
                        Position.Y -= 1;
                        break;
                    case "A":
                        Position.X -= 1;
                        break;
                    case "S":
                        Position.Y += 1;
                        break;
                    case "D":
                        Position.X += 1;
                        break;
                    default:
                        break;
                }
                //
                ConsoleView.SafePosition(Position);
                Console.SetCursorPosition(Position.X, Position.Y);
                Console.Write($"*");
            }

        }
        public class ConsoleView
        {
            private readonly static int Width = 30;
            private readonly static int Height = 15;
            public static ConsoleView Interface { get; private set; } = new ConsoleView();
            private Player Player;

            // 这里是引用类型吧
            public static void SafePosition(Position position)
            {
                if(position.X < 0)
                {
                    position.X = 0;
                }
                if (position.X >= Width)
                {
                    position.X = Width - 1;
                }
                if (position.Y < 0)
                {
                    position.Y = 0;
                }
                if (position.Y >= Height)
                {
                    position.Y = Height - 1;
                }
            }

            // 初始化
            private ConsoleView() {
                // 设置窗口大小
                Console.SetWindowSize(Width, Height);
                // 设置缓冲区大小
                Console.SetBufferSize(Width, Height);
                // 隐藏光标
                Console.CursorVisible = false;
                //
                this.Player = new Player();
            }
            // 刷新
            public void Render()
            {
                while (true)
                {
                    // 清空界面
                    Thread.Sleep(100);
                    for (int row = 0; row < Height; row++)
                    {
                        for (int col = 0; col < Width; col++)
                        {
                            Console.SetCursorPosition(col, row);
                            Console.Write(" ");
                        }
                    }
                    // 绘画玩家
                    this.Player.Render();
                }
            }
        }
        class UserInput
        {
            public static UserInput Interface { get; private set; } = new UserInput();
            private UserInput() { }
            // 用户输入内容
            private static string _content { get; set; } = "";
            // 用户输入内容,读取一次后清空
            public static string Content { 
                get {
                    string temp = _content;
                    _content = "";
                    return temp;
                } }

            public static void Listening()
            {
                var thread = new Thread(() => {
                    while (true)
                    {
                        _content = Console.ReadKey(true).KeyChar.ToString().ToUpper();
                        DebugUtil.Log($"Content:{_content}");
                    }
                });
                thread.Start();
            }
        }

        public class DebugUtil
        {
            public static void Log(string msg) {
                System.Diagnostics.Trace.WriteLine(msg);
            }
        }
    }
}

posted @ 2025-05-05 22:24  夏秋初  阅读(20)  评论(0)    收藏  举报