From 907cede2f6ca2fd4ac034b2e0ca2926df99cc84d Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Wed, 11 Feb 2026 02:09:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AA=E5=90=83=E8=9B=87+=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=8F=90=E7=A4=BA=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GitCommit.txt | 2 +- Kernel.cs | 2 +- docs/cmleonos/docs/commands.md | 17 +++ shell/CommandList.cs | 3 + shell/Commands/Help/Help.cs | 6 + shell/Commands/Utility/SnakeCommand.cs | 191 +++++++++++++++++++++++++ shell/Shell.cs | 11 ++ 7 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 shell/Commands/Utility/SnakeCommand.cs diff --git a/GitCommit.txt b/GitCommit.txt index 6885200..933450e 100644 --- a/GitCommit.txt +++ b/GitCommit.txt @@ -1 +1 @@ -22c896e \ No newline at end of file +d6f11f2 \ No newline at end of file diff --git a/Kernel.cs b/Kernel.cs index f4145b6..4ea6087 100644 --- a/Kernel.cs +++ b/Kernel.cs @@ -366,7 +366,7 @@ namespace CMLeonOS Console.WriteLine("2. Record the system version and steps to reproduce the error."); Console.WriteLine("3. Send an post to the CMLeonOS official forum."); Console.WriteLine("Contact us via https://lbbs.ecuil.com/#/thread/category/10, including the error information for support."); - Console.WriteLine("Please include the system build version, runtime environment, and operation steps before the crash."); + Console.WriteLine("Please include the error information, system build version, runtime environment, and operation steps before the crash."); Console.WriteLine("Warning: Unsaved data in memory will be lost due to the emergency system shutdown."); Console.WriteLine("Press any key to restart."); Console.ReadKey(); diff --git a/docs/cmleonos/docs/commands.md b/docs/cmleonos/docs/commands.md index da3ae94..8fabdfc 100644 --- a/docs/cmleonos/docs/commands.md +++ b/docs/cmleonos/docs/commands.md @@ -817,6 +817,23 @@ labyrinth - 出口位置用红色 E 表示 - 迷宫使用递归回溯算法随机生成 +### snake +玩贪吃蛇游戏。 + +**用法:** +```bash +snake +``` + +**说明:** +- 使用方向键 (↑ ↓ ← →) 控制蛇的移动 +- 按 ESC 或 Q 键退出游戏 +- 蛇身用绿色 ■ 表示 +- 食物用红色 ● 表示 +- 吃到食物得分 +10 分 +- 撞到墙壁或自己身体游戏结束 +- 游戏区域:40x20 字符 + ### diff 比较两个文件的差异。 diff --git a/shell/CommandList.cs b/shell/CommandList.cs index d1a0283..eff36d9 100644 --- a/shell/CommandList.cs +++ b/shell/CommandList.cs @@ -205,6 +205,9 @@ namespace CMLeonOS.shell case "labyrinth": shell.ProcessLabyrinth(); break; + case "snake": + shell.PlaySnake(); + break; case "alias": shell.ProcessAlias(args); break; diff --git a/shell/Commands/Help/Help.cs b/shell/Commands/Help/Help.cs index 37bc7a9..938ca71 100644 --- a/shell/Commands/Help/Help.cs +++ b/shell/Commands/Help/Help.cs @@ -100,6 +100,12 @@ namespace CMLeonOS.Commands Description = "Play maze escape game" }, new CommandInfo + { + Command = "snake", + Parameters = "", + Description = "Play snake game" + }, + new CommandInfo { Command = "edit", Parameters = "", diff --git a/shell/Commands/Utility/SnakeCommand.cs b/shell/Commands/Utility/SnakeCommand.cs new file mode 100644 index 0000000..4663946 --- /dev/null +++ b/shell/Commands/Utility/SnakeCommand.cs @@ -0,0 +1,191 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace CMLeonOS.Commands.Utility +{ + public static class SnakeCommand + { + private static readonly int width = 40; + private static readonly int height = 20; + private static readonly int snakeStartLength = 3; + private static List<(int x, int y)> snake; + private static (int x, int y) food; + private static (int dx, int dy) direction; + private static bool running; + private static Random random; + private static int score; + private static bool gameOver; + + public static void PlaySnake() + { + Console.Clear(); + + InitializeGame(); + + while (running) + { + if (Console.KeyAvailable) + { + var key = Console.ReadKey(true); + HandleInput(key); + } + + Update(); + Render(); + + if (gameOver) + { + ShowGameOver(); + break; + } + + Thread.Sleep(100); + } + + Console.Clear(); + Console.ResetColor(); + } + + private static void InitializeGame() + { + snake = new List<(int, int y)>(); + direction = (1, 0); + random = new Random(); + score = 0; + gameOver = false; + running = true; + + int startX = width / 2; + int startY = height / 2; + + for (int i = 0; i < snakeStartLength; i++) + { + snake.Add((startX - i, startY)); + } + + SpawnFood(); + } + + private static void HandleInput(ConsoleKeyInfo key) + { + switch (key.Key) + { + case ConsoleKey.UpArrow: + if (direction.dy == 0) + direction = (0, -1); + break; + case ConsoleKey.DownArrow: + if (direction.dy == 0) + direction = (0, 1); + break; + case ConsoleKey.LeftArrow: + if (direction.dx == 0) + direction = (-1, 0); + break; + case ConsoleKey.RightArrow: + if (direction.dx == 0) + direction = (1, 0); + break; + case ConsoleKey.Escape: + case ConsoleKey.Q: + running = false; + break; + } + } + + private static void Update() + { + if (!running || gameOver) + return; + + int headX = snake[0].x + direction.dx; + int headY = snake[0].y + direction.dy; + + if (headX < 0 || headX >= width || headY < 0 || headY >= height) + { + gameOver = true; + return; + } + + for (int i = 1; i < snake.Count; i++) + { + if (headX == snake[i].x && headY == snake[i].y) + { + gameOver = true; + return; + } + } + + snake.Insert(0, (headX, headY)); + + if (headX == food.x && headY == food.y) + { + score += 10; + SpawnFood(); + } + else + { + snake.RemoveAt(snake.Count - 1); + } + } + + private static void Render() + { + Console.SetCursorPosition(0, 0); + Console.ForegroundColor = ConsoleColor.DarkBlue; + Console.BackgroundColor = ConsoleColor.Black; + Console.Clear(); + + Console.ForegroundColor = ConsoleColor.Green; + foreach (var segment in snake) + { + Console.SetCursorPosition(segment.x, segment.y); + Console.Write("#"); + } + + Console.ForegroundColor = ConsoleColor.Red; + Console.SetCursorPosition(food.x, food.y); + Console.Write("O"); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.SetCursorPosition(0, height); + Console.Write($"Score: {score} | Arrow keys to move | ESC or Q to quit"); + + Console.ResetColor(); + } + + private static void SpawnFood() + { + bool validPosition = false; + while (!validPosition) + { + food.x = random.Next(0, width); + food.y = random.Next(0, height); + + validPosition = true; + foreach (var segment in snake) + { + if (food.x == segment.x && food.y == segment.y) + { + validPosition = false; + break; + } + } + } + } + + private static void ShowGameOver() + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.SetCursorPosition(width / 2 - 5, height / 2); + Console.WriteLine("GAME OVER!"); + Console.SetCursorPosition(width / 2 - 8, height / 2 + 1); + Console.WriteLine($"Final Score: {score}"); + Console.SetCursorPosition(width / 2 - 10, height / 2 + 3); + Console.WriteLine("Press any key to exit..."); + Console.ReadKey(true); + } + } +} \ No newline at end of file diff --git a/shell/Shell.cs b/shell/Shell.cs index 1214e7d..b49e78f 100644 --- a/shell/Shell.cs +++ b/shell/Shell.cs @@ -86,7 +86,9 @@ namespace CMLeonOS public void Run() { + User currentUser = userSystem.CurrentLoggedInUser; bool shouldExit = false; + while (true) { // 显示当前文件夹路径作为提示符(彩色) @@ -94,6 +96,10 @@ namespace CMLeonOS ConsoleColor originalColor = Console.ForegroundColor; // Console.ForegroundColor = ConsoleColor.Cyan; // Console.Write($"{currentPath} | {prompt}"); + Console.ForegroundColor = ConsoleColor.DarkGreen; + Console.Write($"{currentUser.Username}"); + Console.ForegroundColor = ConsoleColor.White; + Console.Write($" $ "); Console.ForegroundColor = ConsoleColor.Magenta; Console.Write($"{currentPath}"); Console.ForegroundColor = ConsoleColor.White; @@ -1628,6 +1634,11 @@ namespace CMLeonOS Commands.LabyrinthCommand.ProcessLabyrinth(); } + public void PlaySnake() + { + Commands.Utility.SnakeCommand.PlaySnake(); + } + public void ProcessAlias(string args) { if (string.IsNullOrWhiteSpace(args))