一、任务
(1)控制台中有一个 ■
(2)它会如贪食蛇一样自动移动
(3)请开启一个多线程来检测输入,控制它的转向
二、 枚举定义 - 移动方向
enum E_MoveDir
{
Up,
Down,
Right,
Left,
}
定义了四个方向的枚举,用于控制图标的移动方向。
三、Icon类 - 核心功能实现
成员变量
public E_MoveDir dir; // 当前移动方向
public int x; // X坐标位置
public int y; // Y坐标位置
构造函数
public Icon(int x, int y, E_MoveDir dir)
{
this.x = x;
this.y = y;
this.dir = dir;
}
核心方法
移动方法
public void Move()
{
switch (dir)
{
case E_MoveDir.Up: y -= 1; break; // 向上移动
case E_MoveDir.Down: y += 1; break; // 向下移动
case E_MoveDir.Right: x += 2; break; // 向右移动(注意:移动2格)
case E_MoveDir.Left: x -= 2; break; // 向左移动(注意:移动2格)
}
}
注意:左右移动时每次移动2格,这是因为控制台中字符的宽度是高度的两倍
绘制方法
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write("■"); // 使用方块字符表示图标
}
擦除方法
public void Clear()
{
Console.SetCursorPosition(x, y);
Console.Write(" "); // 用两个空格擦除图标
}
转向方法
public void ChangeDir(E_MoveDir dir)
{
this.dir = dir; // 改变移动方向
}
四、Program类 - 主程序逻辑
静态变量
static Icon icon; // 全局图标实例
Main方法 - 主线程
static void Main(string[] args)
{
Console.WriteLine("多线程练习题");
Console.CursorVisible = false; // 隐藏光标
// 初始化图标(位置10,5,初始向右移动)
icon = new Icon(10, 5, E_MoveDir.Right);
icon.Draw();
// 开启输入检测线程
Thread t = new Thread(NewThreadLogic);
t.IsBackground = true; // 设置为后台线程
t.Start();
// 主循环 - 处理图形渲染
while (true)
{
Thread.Sleep(500); // 每500ms更新一次
icon.Clear(); // 清除旧位置
icon.Move(); // 移动到新位置
icon.Draw(); // 在新位置绘制
}
}
输入处理线程方法
static void NewThreadLogic()
{
while (true)
{
// 检测按键输入并改变方向
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.W: icon.ChangeDir(E_MoveDir.Up); break;
case ConsoleKey.A: icon.ChangeDir(E_MoveDir.Left); break;
case ConsoleKey.S: icon.ChangeDir(E_MoveDir.Down); break;
case ConsoleKey.D: icon.ChangeDir(E_MoveDir.Right); break;
}
}
}
五、多线程设计分析
线程分工
-
主线程:负责图形渲染循环(清除→移动→绘制)
-
后台线程:专门处理用户输入检测
线程同步
代码使用了简单的共享变量方式(static Icon icon)进行线程间通信,没有使用复杂的同步机制,这是因为:
-
输入线程只写入方向数据
-
主线程只读取方向数据
-
方向改变是原子操作
六、完整代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace 进阶测试
{
enum E_MoveDir
{
Up,
Down,
Right,
Left,
}
class Icon
{
//当前移动的方向
public E_MoveDir dir;
//当前位置
public int x;
public int y;
public Icon(int x, int y, E_MoveDir dir)
{
this.x = x;
this.y = y;
this.dir = dir;
}
//移动
public void Move()
{
switch (dir)
{
case E_MoveDir.Up:
y -= 1;
break;
case E_MoveDir.Down:
y += 1;
break;
case E_MoveDir.Right:
x += 2;
break;
case E_MoveDir.Left:
x -= 2;
break;
}
}
//绘制
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write("■");
}
//擦除
public void Clear()
{
Console.SetCursorPosition(x, y);
Console.Write(" ");
}
//转向
public void ChangeDir(E_MoveDir dir)
{
this.dir = dir;
}
}
class Program
{
static Icon icon;
static void Main(string[] args)
{
Console.WriteLine("多线程练习题");
#region 练习题
//控制台中有一个■
//它会如贪食蛇一样自动移动
//请开启一个多线程来检测输入,控制它的转向
Console.CursorVisible = false;
icon = new Icon(10, 5, E_MoveDir.Right);
icon.Draw();
//开启多线程
Thread t = new Thread(NewThreadLogic);
t.IsBackground = true;
t.Start();
while (true)
{
Thread.Sleep(500);
icon.Clear();
icon.Move();
icon.Draw();
}
#endregion
}
static void NewThreadLogic()
{
while (true)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.W:
icon.ChangeDir(E_MoveDir.Up);
break;
case ConsoleKey.A:
icon.ChangeDir(E_MoveDir.Left);
break;
case ConsoleKey.S:
icon.ChangeDir(E_MoveDir.Down);
break;
case ConsoleKey.D:
icon.ChangeDir(E_MoveDir.Right);
break;
}
}
}
}
}

1123

被折叠的 条评论
为什么被折叠?



