C# 控制台移动元素小 Demo
参考
- https://www.bilibili.com/video/BV1Ew411o73C
- https://www.cnblogs.com/luna-hehe/p/13999750.html
- https://blog.csdn.net/nocomment_84/article/details/53992730
- https://learn.microsoft.com/zh-cn/dotnet/api/system.console.readkey?view=net-7.0
- https://blog.csdn.net/qq_40091720/article/details/87935319
- https://learn.microsoft.com/zh-cn/dotnet/api/system.console.setwindowposition?view=net-8.0
环境
软件/系统 | 版本 | 说明 |
---|---|---|
Windows | windows 10 专业版 22H2 | 64 位操作系统, 基于 x64 的处理器 |
Microsoft Visual Studio | Community 2022 (64 位) - Current 版本 17.13.6 | |
.NET SDK | 9.0.203 |
正文
演示
代码
实现贪吃蛇的话,可以将玩家数据存储为一个链表,可以通过擦除最后一个,新增第一个的形式进行移动。或者整个链表的所有节点位置都进行调整。
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);
}
}
}
}
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18860530
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18860530
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。