mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
贪吃蛇+修改命令提示符
This commit is contained in:
@@ -1 +1 @@
|
|||||||
22c896e
|
d6f11f2
|
||||||
@@ -366,7 +366,7 @@ namespace CMLeonOS
|
|||||||
Console.WriteLine("2. Record the system version and steps to reproduce the error.");
|
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("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("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("Warning: Unsaved data in memory will be lost due to the emergency system shutdown.");
|
||||||
Console.WriteLine("Press any key to restart.");
|
Console.WriteLine("Press any key to restart.");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|||||||
@@ -817,6 +817,23 @@ labyrinth
|
|||||||
- 出口位置用红色 E 表示
|
- 出口位置用红色 E 表示
|
||||||
- 迷宫使用递归回溯算法随机生成
|
- 迷宫使用递归回溯算法随机生成
|
||||||
|
|
||||||
|
### snake
|
||||||
|
玩贪吃蛇游戏。
|
||||||
|
|
||||||
|
**用法:**
|
||||||
|
```bash
|
||||||
|
snake
|
||||||
|
```
|
||||||
|
|
||||||
|
**说明:**
|
||||||
|
- 使用方向键 (↑ ↓ ← →) 控制蛇的移动
|
||||||
|
- 按 ESC 或 Q 键退出游戏
|
||||||
|
- 蛇身用绿色 ■ 表示
|
||||||
|
- 食物用红色 ● 表示
|
||||||
|
- 吃到食物得分 +10 分
|
||||||
|
- 撞到墙壁或自己身体游戏结束
|
||||||
|
- 游戏区域:40x20 字符
|
||||||
|
|
||||||
### diff
|
### diff
|
||||||
比较两个文件的差异。
|
比较两个文件的差异。
|
||||||
|
|
||||||
|
|||||||
@@ -205,6 +205,9 @@ namespace CMLeonOS.shell
|
|||||||
case "labyrinth":
|
case "labyrinth":
|
||||||
shell.ProcessLabyrinth();
|
shell.ProcessLabyrinth();
|
||||||
break;
|
break;
|
||||||
|
case "snake":
|
||||||
|
shell.PlaySnake();
|
||||||
|
break;
|
||||||
case "alias":
|
case "alias":
|
||||||
shell.ProcessAlias(args);
|
shell.ProcessAlias(args);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -100,6 +100,12 @@ namespace CMLeonOS.Commands
|
|||||||
Description = "Play maze escape game"
|
Description = "Play maze escape game"
|
||||||
},
|
},
|
||||||
new CommandInfo
|
new CommandInfo
|
||||||
|
{
|
||||||
|
Command = "snake",
|
||||||
|
Parameters = "",
|
||||||
|
Description = "Play snake game"
|
||||||
|
},
|
||||||
|
new CommandInfo
|
||||||
{
|
{
|
||||||
Command = "edit",
|
Command = "edit",
|
||||||
Parameters = "<file>",
|
Parameters = "<file>",
|
||||||
|
|||||||
191
shell/Commands/Utility/SnakeCommand.cs
Normal file
191
shell/Commands/Utility/SnakeCommand.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -86,7 +86,9 @@ namespace CMLeonOS
|
|||||||
|
|
||||||
public void Run()
|
public void Run()
|
||||||
{
|
{
|
||||||
|
User currentUser = userSystem.CurrentLoggedInUser;
|
||||||
bool shouldExit = false;
|
bool shouldExit = false;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// 显示当前文件夹路径作为提示符(彩色)
|
// 显示当前文件夹路径作为提示符(彩色)
|
||||||
@@ -94,6 +96,10 @@ namespace CMLeonOS
|
|||||||
ConsoleColor originalColor = Console.ForegroundColor;
|
ConsoleColor originalColor = Console.ForegroundColor;
|
||||||
// Console.ForegroundColor = ConsoleColor.Cyan;
|
// Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
// Console.Write($"{currentPath} | {prompt}");
|
// Console.Write($"{currentPath} | {prompt}");
|
||||||
|
Console.ForegroundColor = ConsoleColor.DarkGreen;
|
||||||
|
Console.Write($"{currentUser.Username}");
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
Console.Write($" $ ");
|
||||||
Console.ForegroundColor = ConsoleColor.Magenta;
|
Console.ForegroundColor = ConsoleColor.Magenta;
|
||||||
Console.Write($"{currentPath}");
|
Console.Write($"{currentPath}");
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
@@ -1628,6 +1634,11 @@ namespace CMLeonOS
|
|||||||
Commands.LabyrinthCommand.ProcessLabyrinth();
|
Commands.LabyrinthCommand.ProcessLabyrinth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void PlaySnake()
|
||||||
|
{
|
||||||
|
Commands.Utility.SnakeCommand.PlaySnake();
|
||||||
|
}
|
||||||
|
|
||||||
public void ProcessAlias(string args)
|
public void ProcessAlias(string args)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(args))
|
if (string.IsNullOrWhiteSpace(args))
|
||||||
|
|||||||
Reference in New Issue
Block a user