添加Lesson18
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,107 @@
|
||||
namespace Lesson18_多线程练习
|
||||
{
|
||||
enum E_Direction
|
||||
{
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
struct Position
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
}
|
||||
interface IDraw
|
||||
{
|
||||
public void Draw();
|
||||
}
|
||||
class Snake : IDraw
|
||||
{
|
||||
public E_Direction Direction;
|
||||
Position Position;
|
||||
public Snake()
|
||||
{
|
||||
Position.X = 15;
|
||||
Position.Y = 5;
|
||||
Direction = E_Direction.Right;
|
||||
}
|
||||
|
||||
public void Move()
|
||||
{
|
||||
switch (this.Direction)
|
||||
{
|
||||
case E_Direction.Up:
|
||||
Position.Y -= 1;
|
||||
break;
|
||||
case E_Direction.Down:
|
||||
Position.Y += 1;
|
||||
break;
|
||||
case E_Direction.Left:
|
||||
Position.X -= 2;
|
||||
break;
|
||||
case E_Direction.Right:
|
||||
Position.X += 2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void Draw()
|
||||
{
|
||||
Console.SetCursorPosition(Position.X, Position.Y);
|
||||
Console.Write("■");
|
||||
}
|
||||
public void Clean()
|
||||
{
|
||||
Console.SetCursorPosition(Position.X, Position.Y);
|
||||
Console.Write(" ");
|
||||
}
|
||||
public void ChangeDirection(E_Direction direction)
|
||||
{
|
||||
this.Direction = direction;
|
||||
}
|
||||
}
|
||||
class Program
|
||||
{
|
||||
static Snake snake;
|
||||
static void Main(string[] args)
|
||||
{
|
||||
snake = new Snake();
|
||||
Console.CursorVisible = false;
|
||||
Thread inputThread = new Thread(input);
|
||||
inputThread.Start();
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
snake.Clean();
|
||||
snake.Move();
|
||||
snake.Draw();
|
||||
}
|
||||
|
||||
}
|
||||
static void input()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
switch(Console.ReadKey(true).Key)
|
||||
{
|
||||
case ConsoleKey.UpArrow:
|
||||
snake.ChangeDirection(E_Direction.Up);
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
snake.ChangeDirection(E_Direction.Down);
|
||||
break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
snake.ChangeDirection(E_Direction.Left);
|
||||
break;
|
||||
case ConsoleKey.RightArrow:
|
||||
snake.ChangeDirection(E_Direction.Right);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user