From 116b65164ba23665e2e7b3f6691d0eb2481a2c16 Mon Sep 17 00:00:00 2001
From: Kister Hakusan <2753888203@qq.com>
Date: Sun, 21 Sep 2025 20:11:21 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B4=AA=E5=90=83=E8=9B=87=E5=B0=8F=E9=A1=B9?=
=?UTF-8?q?=E7=9B=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
C#核心/test/Program.cs | 78 ++++++++----
C#核心实践/C#核心实践.sln | 8 +-
C#核心实践/贪吃蛇/Lesson1/Game.cs | 78 ++++++++++++
C#核心实践/贪吃蛇/Lesson1/ISceneUpdate.cs | 18 +++
C#核心实践/贪吃蛇/Lesson2/BaseScene.cs | 48 ++++++++
C#核心实践/贪吃蛇/Lesson2/BeginScene.cs | 31 +++++
C#核心实践/贪吃蛇/Lesson2/EndScene.cs | 32 +++++
C#核心实践/贪吃蛇/Lesson2/GameScene.cs | 65 ++++++++++
C#核心实践/贪吃蛇/Lesson3/GameObject.cs | 19 +++
C#核心实践/贪吃蛇/Lesson3/IDraw.cs | 13 ++
C#核心实践/贪吃蛇/Lesson3/Position.cs | 28 +++++
C#核心实践/贪吃蛇/Lesson4/Food.cs | 36 ++++++
C#核心实践/贪吃蛇/Lesson4/SnakeBody.cs | 29 +++++
C#核心实践/贪吃蛇/Lesson4/Wall.cs | 22 ++++
C#核心实践/贪吃蛇/Lesson5/Map.cs | 49 ++++++++
C#核心实践/贪吃蛇/Lesson6/Snake.cs | 141 ++++++++++++++++++++++
C#核心实践/贪吃蛇/Program.cs | 11 ++
C#核心实践/贪吃蛇/贪吃蛇.csproj | 14 +++
18 files changed, 695 insertions(+), 25 deletions(-)
create mode 100644 C#核心实践/贪吃蛇/Lesson1/Game.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson1/ISceneUpdate.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson2/BaseScene.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson2/BeginScene.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson2/EndScene.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson2/GameScene.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson3/GameObject.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson3/IDraw.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson3/Position.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson4/Food.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson4/SnakeBody.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson4/Wall.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson5/Map.cs
create mode 100644 C#核心实践/贪吃蛇/Lesson6/Snake.cs
create mode 100644 C#核心实践/贪吃蛇/Program.cs
create mode 100644 C#核心实践/贪吃蛇/贪吃蛇.csproj
diff --git a/C#核心/test/Program.cs b/C#核心/test/Program.cs
index 23c0696..4a41437 100644
--- a/C#核心/test/Program.cs
+++ b/C#核心/test/Program.cs
@@ -1,25 +1,55 @@
-// See https://aka.ms/new-console-template for more information
-Console.WriteLine("Hello, World!");
-#region 知识点一
-#endregion
+using System;
+namespace Test
+{
+ enum E_RoomType
+ {
+ Bedroom,
+ LivingRoom
+ }
+ class Bedroom : IChangeRoom
+ {
+ public void ChangeRoom()
+ {
+ Console.WriteLine("卧室");
+ }
+ }
+ class LivingRoom : IChangeRoom
+ {
+ public void ChangeRoom()
+ {
+ Console.WriteLine("客厅");
+ }
+ }
+ interface IChangeRoom
+ {
+ void ChangeRoom() { }
+ }
+ class Show
+ {
+ IChangeRoom room;
+ public Show()
+ {
+ room = new Bedroom();
+ }
+ public Show(IChangeRoom room)
+ {
+ this.room = room;
+ }
+ public void ShowRoom()
+ {
+ room.ChangeRoom();
+ }
-#region 知识点二
-#endregion
-
-#region 知识点三
-#endregion
-
-#region 知识点四
-#endregion
-
-#region 知识点五
-#endregion
-
-#region 知识点六
-#endregion
-
-#region 知识点七
-#endregion
-
-#region 知识点八
-#endregion
\ No newline at end of file
+ }
+
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ Show s1 = new Show();
+ Show s2 = new Show(new LivingRoom());
+ s1.ShowRoom();
+ s2.ShowRoom();
+ }
+ }
+}
\ No newline at end of file
diff --git a/C#核心实践/C#核心实践.sln b/C#核心实践/C#核心实践.sln
index 60cfa81..778bcf4 100644
--- a/C#核心实践/C#核心实践.sln
+++ b/C#核心实践/C#核心实践.sln
@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
-VisualStudioVersion = 17.14.36202.13 d17.14
+VisualStudioVersion = 17.14.36202.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "必备知识点_多脚本文件", "必备知识点_多脚本文件\必备知识点_多脚本文件.csproj", "{20F6B501-8D7E-468E-9D92-B0ACC81D217B}"
EndProject
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "必备知识点_UML类图",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "必备知识点_面向对象七大原则", "必备知识点_面向对象七大原则\必备知识点_面向对象七大原则.csproj", "{6379831D-2766-4714-A82E-1072023A574F}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "贪吃蛇", "贪吃蛇\贪吃蛇.csproj", "{B3D215DA-A41B-4CBE-B572-1FD94BF0239F}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
{6379831D-2766-4714-A82E-1072023A574F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6379831D-2766-4714-A82E-1072023A574F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6379831D-2766-4714-A82E-1072023A574F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {B3D215DA-A41B-4CBE-B572-1FD94BF0239F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {B3D215DA-A41B-4CBE-B572-1FD94BF0239F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B3D215DA-A41B-4CBE-B572-1FD94BF0239F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {B3D215DA-A41B-4CBE-B572-1FD94BF0239F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/C#核心实践/贪吃蛇/Lesson1/Game.cs b/C#核心实践/贪吃蛇/Lesson1/Game.cs
new file mode 100644
index 0000000..847dae2
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson1/Game.cs
@@ -0,0 +1,78 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ class Game
+ {
+ //窗口宽高
+ public const int h = 20;
+ public const int w = 80;
+ //当前游戏场景
+ public static ISceneUpdate nowScene;
+
+ public Game()
+ {
+ Console.CursorVisible = false;
+ Console.SetWindowSize(w, h);
+ Console.SetBufferSize(w, h);
+
+ ChangeScene(E_Scene.Begin);//初始场景为开始场景
+ }
+ public void Start()
+ {
+ //游戏主循环
+ while (true)
+ {
+ try
+ {
+ if (nowScene != null)
+ {
+ nowScene.Update();//场景不为空就更新场景
+ }
+ }
+ catch
+ {
+
+ }
+ }
+ }
+ public static void ChangeScene(E_Scene type)
+ {
+ //切场景时先擦除上一次绘制的内容
+ Console.Clear();
+ switch (type)
+ {
+ case E_Scene.Begin:
+ nowScene = new BeginScene();
+ break;
+ case E_Scene.Game:
+ nowScene = new GameScene();
+ break;
+ case E_Scene.End:
+ nowScene = new EndScene();
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ enum E_Scene
+ {
+ ///
+ /// 开始场景
+ ///
+ Begin,
+ ///
+ /// 游戏场景
+ ///
+ Game,
+ ///
+ /// 结束场景
+ ///
+ End,
+ }//游戏场景枚举
+}
diff --git a/C#核心实践/贪吃蛇/Lesson1/ISceneUpdate.cs b/C#核心实践/贪吃蛇/Lesson1/ISceneUpdate.cs
new file mode 100644
index 0000000..51812e1
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson1/ISceneUpdate.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ ///
+ /// 场景更新接口
+ ///
+ interface ISceneUpdate
+ {
+ void Update()
+ {
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson2/BaseScene.cs b/C#核心实践/贪吃蛇/Lesson2/BaseScene.cs
new file mode 100644
index 0000000..efcf458
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson2/BaseScene.cs
@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ abstract class BaseScene : ISceneUpdate
+ {
+ protected int nowSceneIndex = 0;
+ protected string title;
+ protected string str1;
+
+ public abstract void PressEnter();
+ public void Update()
+ {
+ //要完成的内容:绘制开始和结束场景,并完成切换选择功能
+ Console.ForegroundColor = ConsoleColor.White;
+
+ //绘制标题
+ Console.SetCursorPosition(Game.w / 2 - title.Length, 5);
+ Console.Write(title);
+
+ //绘制选项
+ Console.SetCursorPosition(Game.w / 2 - str1.Length, 8);
+ Console.ForegroundColor = nowSceneIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
+ Console.Write(str1);
+
+ Console.SetCursorPosition(Game.w / 2 - 4, 10);
+ Console.ForegroundColor = nowSceneIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
+ Console.Write("结束游戏");
+ //根据nowSceneIndex绘制wsj选中的索引
+ switch (Console.ReadKey(true).Key)
+ {
+ case ConsoleKey.W:
+ nowSceneIndex = 0;
+ break;
+ case ConsoleKey.S:
+ nowSceneIndex = 1;
+ break;
+ case ConsoleKey.Enter:
+ PressEnter();
+ break;
+ }
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson2/BeginScene.cs b/C#核心实践/贪吃蛇/Lesson2/BeginScene.cs
new file mode 100644
index 0000000..fcf937c
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson2/BeginScene.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ class BeginScene : BaseScene
+ {
+ public BeginScene()
+ {
+ title = "贪吃蛇小游戏";
+ str1 = "开始游戏";
+ }
+ public override void PressEnter()
+ {
+ //根据nowSceneIndex的值决定按下回车后做什么
+ if( nowSceneIndex == 0 )
+ {
+ //切换到开始游戏
+ Game.ChangeScene(E_Scene.Game);
+ }
+ else
+ {
+ //结束游戏
+ Environment.Exit(0);
+ }
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson2/EndScene.cs b/C#核心实践/贪吃蛇/Lesson2/EndScene.cs
new file mode 100644
index 0000000..8dd6d9d
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson2/EndScene.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Cryptography.X509Certificates;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ class EndScene : BaseScene
+ {
+ public EndScene()
+ {
+ title = "游戏结束";
+ str1 = "重新开始";
+ }
+ public override void PressEnter()
+ {
+ //根据nowSceneIndex的值决定按下回车后做什么
+ if (nowSceneIndex == 0)
+ {
+ //切换到开始游戏
+ Game.ChangeScene(E_Scene.Begin);
+ }
+ else
+ {
+ //结束游戏
+ Environment.Exit(0);
+ }
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson2/GameScene.cs b/C#核心实践/贪吃蛇/Lesson2/GameScene.cs
new file mode 100644
index 0000000..3e2a145
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson2/GameScene.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ class GameScene : ISceneUpdate
+ {
+ Map map;
+ Snake snake;
+ Food food;
+ public GameScene()
+ {
+ map = new Map();
+ snake = new Snake(40, 10);
+ food = new Food(snake);
+ }
+ public void Update()
+ {
+
+ map.Draw();//画地图
+ food.Draw();//画食物
+ snake.Move();//让蛇动起来
+ snake.Draw();//画蛇
+ if (snake.CheckEnd(map))//检测游戏是否结束
+ {
+ Game.ChangeScene(E_Scene.End);
+ }
+ snake.CheckEat(food);//检测蛇是否吃到食物
+ Thread.Sleep(200);//控制蛇的移动速度
+ //检测用户输入
+ if (Console.KeyAvailable)//Console.KeyAvailable的作用是:如果有按键输入才返回Ture,防止卡住
+ {
+ switch (Console.ReadKey(true).Key)
+ {
+ case ConsoleKey.W:
+ case ConsoleKey.UpArrow:
+ snake.ChangeDirection(E_MoveDirection.Up);
+ //向上
+ break;
+ case ConsoleKey.S:
+ case ConsoleKey.DownArrow:
+ snake.ChangeDirection(E_MoveDirection.Down);
+ //向下
+ break;
+ case ConsoleKey.A:
+ case ConsoleKey.LeftArrow:
+ snake.ChangeDirection(E_MoveDirection.Left);
+ //向左
+ break;
+ case ConsoleKey.D:
+ case ConsoleKey.RightArrow:
+ snake.ChangeDirection(E_MoveDirection.Right);
+ //向右
+ break;
+ default:
+ break;
+ }
+ }
+
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson3/GameObject.cs b/C#核心实践/贪吃蛇/Lesson3/GameObject.cs
new file mode 100644
index 0000000..4ed6652
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson3/GameObject.cs
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ abstract class GameObject : IDraw
+ {
+ //当前位置
+ public Position pos;
+
+ //绘制对象
+ //可以继承接口后,把接口中的方法变成抽象方法,让子类去实现
+ //因为时抽象行为,所以子类必须实现
+ public abstract void Draw();
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson3/IDraw.cs b/C#核心实践/贪吃蛇/Lesson3/IDraw.cs
new file mode 100644
index 0000000..9632301
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson3/IDraw.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ interface IDraw
+ {
+ void Draw();
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson3/Position.cs b/C#核心实践/贪吃蛇/Lesson3/Position.cs
new file mode 100644
index 0000000..2f0a761
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson3/Position.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ struct Position
+ {
+ public int x;
+ public int y;
+ public Position(int x, int y)
+ {
+ this.x = x;
+ this.y = y;
+ }
+ //重载==和!=运算符来判断两个Position是否相等
+ public static bool operator == (Position p1, Position p2)
+ {
+ return p1.x == p2.x && p1.y == p2.y;
+ }
+ public static bool operator !=(Position p1, Position p2)
+ {
+ return !(p1 == p2);
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson4/Food.cs b/C#核心实践/贪吃蛇/Lesson4/Food.cs
new file mode 100644
index 0000000..5e285de
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson4/Food.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ class Food : GameObject
+ {
+ public Food(Snake snake)
+ {
+ RandomPos(snake);
+ }
+ public override void Draw()
+ {
+ Console.ForegroundColor = ConsoleColor.Cyan;
+ Console.SetCursorPosition(pos.x, pos.y);
+ Console.Write("◎");
+ }
+ public void RandomPos(Snake snake)
+ {
+ Random r = new Random();
+ int x = r.Next(1, Game.w / 2 - 1) * 2; //1-38的随机数
+ int y = r.Next(2, Game.h - 3); //1-18的随机数
+ pos = new Position(x, y);
+ //判断生成的位置是否和蛇身重合
+ if (snake.CheckSamePos(pos))
+ {
+ RandomPos(snake);
+ }
+
+ }
+
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson4/SnakeBody.cs b/C#核心实践/贪吃蛇/Lesson4/SnakeBody.cs
new file mode 100644
index 0000000..fcc8909
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson4/SnakeBody.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ enum E_BodyType
+ {
+ Head, //蛇头
+ Body, //蛇身
+ }
+ class SnakeBody : GameObject
+ {
+ private E_BodyType bodyType; //身体类型
+ public SnakeBody(int x, int y, E_BodyType bodyType)
+ {
+ pos = new Position(x, y);
+ this.bodyType = bodyType;
+ }
+ public override void Draw()
+ {
+ Console.SetCursorPosition(pos.x, pos.y);
+ Console.ForegroundColor = bodyType == E_BodyType.Head ? ConsoleColor.Yellow : ConsoleColor.Green;
+ Console.Write(bodyType == E_BodyType.Head ? "●" : "○");
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson4/Wall.cs b/C#核心实践/贪吃蛇/Lesson4/Wall.cs
new file mode 100644
index 0000000..b4dae80
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson4/Wall.cs
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ class Wall : GameObject
+ {
+ public Wall(int x, int y)
+ {
+ pos = new Position(x, y);
+ }
+ public override void Draw()
+ {
+ Console.ForegroundColor = ConsoleColor.Red;
+ Console.SetCursorPosition(pos.x, pos.y);
+ Console.Write("■");
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson5/Map.cs b/C#核心实践/贪吃蛇/Lesson5/Map.cs
new file mode 100644
index 0000000..24f2858
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson5/Map.cs
@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ class Map : IDraw
+ {
+ public Wall[] walls; //存放墙位置的数组
+
+ public Map()
+ {
+ //初始化墙
+ walls = new Wall[Game.w + (Game.h -3) * 2];
+ int index = 0;
+ //上边墙
+ for (int x = 0; x < Game.w; x+=2)
+ {
+ walls[index++] = new Wall(x, 0);
+ }
+ //下边墙
+ for (int x = 0; x < Game.w; x+=2)
+ {
+ walls[index++] = new Wall(x, Game.h - 2);
+ }
+ //左边墙
+ for (int y = 1; y < Game.h - 2; y++)
+ {
+ walls[index++] = new Wall(0, y);
+ }
+ //右边墙
+ for (int y = 1; y < Game.h - 2; y++)
+ {
+ walls[index++] = new Wall(Game.w - 2, y);
+ }
+ }
+
+ public void Draw()
+ {
+ for (int x = 0; x < walls.Length; x++)
+ {
+ walls[x].Draw();
+
+ }
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Lesson6/Snake.cs b/C#核心实践/贪吃蛇/Lesson6/Snake.cs
new file mode 100644
index 0000000..2066657
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Lesson6/Snake.cs
@@ -0,0 +1,141 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace 贪吃蛇
+{
+ enum E_MoveDirection
+ {
+ Up,
+ Down,
+ Left,
+ Right
+ }
+ class Snake : IDraw
+ {
+ E_MoveDirection moveDirection;//记录蛇当前的移动方向
+ SnakeBody[] bodies; //存放蛇身体的数组
+ int nowNum;//用一个变量记录蛇的长度
+ public Snake(int x, int y)
+ {
+ moveDirection = E_MoveDirection.Right;
+ bodies = new SnakeBody[200];
+ bodies[0] = new SnakeBody(x, y, E_BodyType.Head);
+ nowNum = 1;
+ }
+ public void Draw()
+ {
+ //只画出当前的蛇身
+ for (int i = 0; i < nowNum; i++)
+ {
+ bodies[i].Draw();
+ }
+
+ }
+
+ #region Lesson7
+ //蛇的移动
+ public void Move()
+ {
+ //移动前擦除前一个位置
+ SnakeBody lastBody = bodies[nowNum - 1];
+ Console.SetCursorPosition(lastBody.pos.x, lastBody.pos.y);
+ Console.Write(" ");
+
+ for(int i = nowNum - 1; i > 0; i--)
+ {
+ bodies[i].pos = bodies[i - 1].pos;
+ }
+ switch (moveDirection)
+ {
+ case E_MoveDirection.Up:
+ bodies[0].pos.y--;
+ break;
+ case E_MoveDirection.Down:
+ bodies[0].pos.y++;
+ break;
+ case E_MoveDirection.Left:
+ bodies[0].pos.x -= 2;
+ break;
+ case E_MoveDirection.Right:
+ bodies[0].pos.x += 2;
+ break;
+ }
+ }
+ #endregion
+
+ #region Lesson8
+ public void ChangeDirection(E_MoveDirection newDirection)
+ {
+ //不能直接掉头
+ if ( newDirection == moveDirection ||
+ nowNum > 1 && (moveDirection == E_MoveDirection.Right && newDirection == E_MoveDirection.Left ) ||
+ nowNum > 1 && (moveDirection == E_MoveDirection.Left && newDirection == E_MoveDirection.Right) ||
+ nowNum > 1 && (moveDirection == E_MoveDirection.Up && newDirection == E_MoveDirection.Down) ||
+ nowNum > 1 && (moveDirection == E_MoveDirection.Down && newDirection == E_MoveDirection.Up) )
+ {
+ return;
+ }
+ moveDirection = newDirection;
+ }
+ #endregion
+
+ #region Lesson9
+ //碰撞检测
+ public bool CheckEnd(Map map)
+ {
+ //是否和墙体位置重合
+ for(int i = 0; i < map.walls.Length; i++)
+ {
+ if (bodies[0].pos == map.walls[i].pos)
+ {
+ return true;
+ }
+ }
+ //是否和自己身体位置重合
+ for(int i = 1; i < nowNum; i++)
+ {
+ if (bodies[0].pos == bodies[i].pos)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+ #endregion
+
+ #region Lesson10
+ public bool CheckSamePos(Position p)
+ {
+ for(int i = 0; i < nowNum; i++)
+ {
+ if (bodies[i].pos == p)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+ public void CheckEat(Food food)
+ {
+ if (bodies[0].pos == food.pos)
+ {
+ //吃到了就再次随机生成食物,并且让蛇变长
+ food.RandomPos(this);
+ GrowUp();
+ }
+ }
+ #endregion
+
+ #region Lesson11
+ private void GrowUp()
+ {
+ SnakeBody lastBody = bodies[nowNum - 1];
+ bodies[nowNum] = new SnakeBody(lastBody.pos.x, lastBody.pos.y, E_BodyType.Body);
+ nowNum++;
+ }
+ #endregion
+ }
+}
diff --git a/C#核心实践/贪吃蛇/Program.cs b/C#核心实践/贪吃蛇/Program.cs
new file mode 100644
index 0000000..22c38ee
--- /dev/null
+++ b/C#核心实践/贪吃蛇/Program.cs
@@ -0,0 +1,11 @@
+namespace 贪吃蛇
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ Game game = new Game();
+ game.Start();
+ }
+ }
+}
diff --git a/C#核心实践/贪吃蛇/贪吃蛇.csproj b/C#核心实践/贪吃蛇/贪吃蛇.csproj
new file mode 100644
index 0000000..f45e109
--- /dev/null
+++ b/C#核心实践/贪吃蛇/贪吃蛇.csproj
@@ -0,0 +1,14 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+