Skip to main content

3月31日 控制台

// 1. 初始化
Random rd = new Random();
int bombPassword = rd.Next(1, 101);
int chances = 5;
bool isDefused = false;

// 初始界面:白底黑字,显得专业且正式
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear(); // 必须调用 Clear,背景色才会铺满全屏

Console.WriteLine("========= 紧急任务:炸弹拆解 =========");
Console.WriteLine($"警告:密码范围 1-100,剩余机会:{chances} 次");
Console.WriteLine("======================================");

// 2. 游戏主循环
for (int i = 1; i <= chances; i++)
{
// 恢复为默认的黑底白字进行输入,方便阅读
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;

Console.Write($"\n第 {i} 次尝试,输入密码:");

if (int.TryParse(Console.ReadLine(), out int guess))
{
if (guess == bombPassword)
{
// 成功:绿底白字
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear(); // 全屏变绿
Console.WriteLine("\n\n\n 【 恭喜 】密码正确!炸弹已拆除,你是英雄!");
isDefused = true;
break;
}
else
{
// 猜错提示:蓝底白字
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
string hint = guess > bombPassword ? " 提示:偏高了 ↑ " : " 提示:偏低了 ↓ ";
Console.WriteLine(hint);
}
}
}

// 3. 结局处理
if (!isDefused)
{
// 失败:红黑交替闪烁 3 次
for (int j = 0; j < 3; j++)
{
Console.BackgroundColor = ConsoleColor.Red; // 红底
Console.Clear();
System.Threading.Thread.Sleep(200); // 暂停 0.2 秒

Console.BackgroundColor = ConsoleColor.Black; // 黑底
Console.Clear();
System.Threading.Thread.Sleep(200);
}

Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("\n\n\n 【 轰!!! 】炸弹爆炸,任务失败!");
Console.WriteLine($" 正确密码是:{bombPassword}");
}

// 无论胜负,最后重置颜色
Console.ResetColor();
Console.WriteLine("\n\n按任意键退出程序...");
Console.ReadKey();