Synchronization of old projects

Part1
This commit is contained in:
2025-09-30 16:46:01 +08:00
parent 116b65164b
commit 4e1548abe2
167 changed files with 8668 additions and 43 deletions
@@ -0,0 +1,49 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36408.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "必备知识点_控制台相关", "必备知识点_控制台相关\必备知识点_控制台相关.csproj", "{06573DDE-AC71-42D8-98CA-98DD42A3C236}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "控制台相关练习题", "练习题\控制台相关练习题.csproj", "{88D217C8-8B85-4719-98E2-3A8FAF8489A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "随机数", "随机数\随机数.csproj", "{A2D7438B-0455-43FA-A8E0-ED830E1DD30B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "随机数相关练习题", "随机数相关练习题\随机数相关练习题.csproj", "{1D204805-9EFC-4157-AA4F-A16BBD9CD95A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "小游戏", "小游戏\小游戏.csproj", "{7DD928E2-3B86-4EDB-92BB-E53BF3D0D069}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{06573DDE-AC71-42D8-98CA-98DD42A3C236}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06573DDE-AC71-42D8-98CA-98DD42A3C236}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06573DDE-AC71-42D8-98CA-98DD42A3C236}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06573DDE-AC71-42D8-98CA-98DD42A3C236}.Release|Any CPU.Build.0 = Release|Any CPU
{88D217C8-8B85-4719-98E2-3A8FAF8489A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{88D217C8-8B85-4719-98E2-3A8FAF8489A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{88D217C8-8B85-4719-98E2-3A8FAF8489A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{88D217C8-8B85-4719-98E2-3A8FAF8489A3}.Release|Any CPU.Build.0 = Release|Any CPU
{A2D7438B-0455-43FA-A8E0-ED830E1DD30B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2D7438B-0455-43FA-A8E0-ED830E1DD30B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2D7438B-0455-43FA-A8E0-ED830E1DD30B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2D7438B-0455-43FA-A8E0-ED830E1DD30B}.Release|Any CPU.Build.0 = Release|Any CPU
{1D204805-9EFC-4157-AA4F-A16BBD9CD95A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D204805-9EFC-4157-AA4F-A16BBD9CD95A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D204805-9EFC-4157-AA4F-A16BBD9CD95A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D204805-9EFC-4157-AA4F-A16BBD9CD95A}.Release|Any CPU.Build.0 = Release|Any CPU
{7DD928E2-3B86-4EDB-92BB-E53BF3D0D069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7DD928E2-3B86-4EDB-92BB-E53BF3D0D069}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DD928E2-3B86-4EDB-92BB-E53BF3D0D069}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DD928E2-3B86-4EDB-92BB-E53BF3D0D069}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93EA734C-376F-48C2-9A20-13FCCD3D81DB}
EndGlobalSection
EndGlobal
+370
View File
@@ -0,0 +1,370 @@
#region
using System.ComponentModel.Design;
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
int w = 50;
int h = 30;
Console.SetWindowSize(w, h);
Console.SetBufferSize(w, h);
Console.CursorVisible = false;
#endregion
#region
int nowScene = 1;
try
{
while (true)
{
switch (nowScene)
{
//主界面
case 1:
Console.Clear();
Console.WriteLine("开始场景");
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(w / 2 - 3, h / 4);
Console.Write("寄掰游戏");
bool notExit = true;
int nowSceneIndex = 1;
char choice = 'w';
nowScene = 2;
while (notExit)
{
Console.SetCursorPosition(w / 2 - 3, h / 3 + 1);
Console.ForegroundColor = nowSceneIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.Write("开始游戏");
Console.ForegroundColor = nowSceneIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.SetCursorPosition(w / 2 - 3, h / 3 + 3);
Console.Write("退出游戏");
choice = Console.ReadKey(true).KeyChar;
switch (choice)
{
case '↑':
case 'w':
case 'W':
nowScene = 2;
nowSceneIndex = 1;
break;
case '↓':
case 's':
case 'S':
nowSceneIndex = 0;
nowScene = 3;
break;
case '\r':
notExit = false;
break;
}
}
break;
//游戏场景
case 2:
#region
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("游戏场景");
Console.SetCursorPosition(2, h - 5);
Console.Write("1.WASD移动 2.靠近BOSS按J攻击");
#region
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < w; i += 2)
{
for (int j = 0; j < h; j++)
{
//Console.SetCursorPosition(25, 12);
//Console.WriteLine("当前i{0},j{1}", i, j);//测试用
if (i == 0 || j == 0)
{
Console.SetCursorPosition(i, j);
Console.Write("■");
}
else if (j == Console.BufferHeight - 6)
{
Console.SetCursorPosition(i, j);
Console.Write("■");
}
else if (i == Console.BufferWidth - 2 || j == Console.BufferHeight - 1)
{
Console.SetCursorPosition(i, j);
Console.Write("■");
}
}
}
#endregion
#endregion
#region
Random r = new Random();
notExit = true;
bool isBossDefeated = false;
bool isPlayerAlive = true;
bool isPlayerApproaching = false;
char movement;
//BOSS参数
string bossIcon = "■";
int bossHP = 100;
int bossAttackMax = 120;
int bossAttackMin = 100;
int bossDefense = 5;
int bossLocationX = r.Next(2, w - 2);
int bossLocationY = r.Next(1, h - 7);
while (bossLocationX < 2 || bossLocationY < 2 || bossLocationX > w - 4 || bossLocationY > h - 8)
{
bossLocationX = r.Next(2, w - 2);
bossLocationY = r.Next(1, h - 7);
}
//玩家参数
string playerIcon = "●";
int playerHP = 100;
int playerAttackMax = 12;
int playerAttackMin = 7;
int playerLocationX = r.Next(2, w - 2);
int playerLocationY = r.Next(1, h - 7);
while (playerLocationX == bossLocationX && playerLocationY == bossLocationY)
{
playerLocationX = r.Next(2, w - 2);
playerLocationY = r.Next(1, h - 7);
}
#endregion
while (notExit)
{
#region
if (playerHP > 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.SetCursorPosition(playerLocationX, playerLocationY);
Console.WriteLine(playerIcon);
}
#endregion
#region BOSS
if (bossHP > 0)
{
Console.SetCursorPosition(bossLocationX, bossLocationY);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(bossIcon);
}
#endregion
#region BOSS位置
if (playerLocationY == bossLocationY && playerLocationX == bossLocationX ||
playerLocationY == bossLocationY && playerLocationX == bossLocationX - 1 ||
playerLocationY == bossLocationY && playerLocationX == bossLocationX + 1)
{
isPlayerApproaching = true;
while (isPlayerAlive)
{
Console.SetCursorPosition(2, h - 4);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Press J to join fight!");
movement = Console.ReadKey(true).KeyChar;
if (movement == 'J' || movement == 'j')
{
int round = 1;
bool isDefend = false;
int damage;
#region
Console.SetCursorPosition(2, h - 4);
Console.WriteLine(" ");
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("Combat start!");
Thread.Sleep(2000);
Console.SetCursorPosition(2, h - 4);
Console.WriteLine(" ");
#endregion
while (isPlayerAlive)
{
damage = 0;
Console.SetCursorPosition(2, h - 5);
Console.WriteLine(" ");
if (bossHP <= 0)
{
isBossDefeated = true;
Console.SetCursorPosition(2, h - 5);
Console.WriteLine("你击败了BOSS,按下任何按键继续");
Thread.Sleep(1000);
isBossDefeated = true;
notExit = true;
nowScene = 3;
break;
}
if (playerHP <= 0)
{
isPlayerAlive = false;
Console.SetCursorPosition(2, h - 5);
Console.WriteLine("你被BOSS打败了,按下任何按键继续");
Thread.Sleep(1000);
notExit = true;
nowScene = 3;
break;
}
if (round % 2 != 0)
{
Console.SetCursorPosition(2, h - 5);
Console.WriteLine("现在是你的回合,按下1防御,按下2攻击");
movement = Console.ReadKey(true).KeyChar;
if (movement == '1')
{
isDefend = true;
Console.SetCursorPosition(2, h - 5);
Console.WriteLine("你选择了防御");
Thread.Sleep(2000);
}
else if (movement == '2')
{
isDefend = false;
damage = r.Next(playerAttackMin, playerAttackMax);
bossHP -= damage;
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("你攻击了BOSS,造成了{0}点伤害,BOSS剩余血量{1}", damage, bossHP);
Thread.Sleep(2000);
}
}
else
{
Console.SetCursorPosition(2, h - 5);
Console.WriteLine("现在是BOSS的回合");
Thread.Sleep(2000);
if (isDefend = false)
{
damage = r.Next(bossAttackMin, bossAttackMax);
playerHP -= damage;
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("boss攻击了你{0}点血,剩余{1}点血",damage,playerHP);
Thread.Sleep(2400);
}
else
{
damage = r.Next(bossAttackMin, bossAttackMax) / 2; //防御时伤害减半
playerHP -= damage;
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("boss攻击了你{0}点血,剩余{1}点血", damage, playerHP);
Thread.Sleep(2500);
}
}
round++;
}
if (isBossDefeated)
{
notExit = false; //退出游戏场景
nowScene = 3; //进入结算场景
break;
}
}
else
{
Console.SetCursorPosition(2, h - 4);
Console.WriteLine(" ");
break;
}
notExit = false; //退出游戏场景
nowScene = 3; //进入结算场景
break;
}
}
#endregion
#region
movement = Console.ReadKey(true).KeyChar;
//test
Console.SetCursorPosition(2, h - 2);
Console.WriteLine(movement);
#region
Console.SetCursorPosition(playerLocationX, playerLocationY);
Console.WriteLine(" ");
#endregion
switch (movement)
{
case 'w':
case 'W':
if (playerLocationY > 1)
{
playerLocationY--;
}
break;
case 's':
case 'S':
if (playerLocationY < h - 7)
{
playerLocationY++;
}
break;
case 'a':
case 'A':
if (playerLocationX > 2)
{
playerLocationX -= 1;
}
break;
case 'd':
case 'D':
if (playerLocationX < w - 4)
{
playerLocationX += 1;
}
break;
case 'j':
case 'J':
if (isPlayerApproaching == false)
{
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("距离不足");
}
break;
case 'q':
case 'Q':
nowScene = 3; //退出游戏
break;
}
#endregion
#region
if (nowScene == 3)
{
notExit = false;
break;
}
#endregion
}
break;
//结算场景
case 3:
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("结算场景");
//Console.WriteLine("恭喜你,成功营救公主!");
Console.WriteLine("Press any key to quit");
int block = Console.ReadKey(true).KeyChar;
Environment.Exit(0);
break;
}
}
}
catch (Exception e)
{
Console.WriteLine("Error code:{0}",e);
}
finally
{
Console.ResetColor(); //重置颜色为默认颜色
Console.Clear(); //清空控制台内容
Environment.Exit(0);
}
#endregion
@@ -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,70 @@
using System;
namespace _控制台相关
{
class Program
{
static void Main(string[] args)
{
#region
// 输出
Console.WriteLine("123");//输出后换行
Console.Write("456");//输出后不换行
// 输入
string str = Console.ReadLine(); //读取一行输入
char c = Console.ReadKey(/*这里加true不会将输入的内容显示在控制台上*/).KeyChar;//读取一个按键输入
#endregion
#region
//1.清空
Console.Clear(); //清空控制台内容
//2.设置控制台大小
//2.1 设置控制台窗口大小
Console.SetWindowSize(1000, 500); //设置控制台窗口大小为100列,50行
//2.2 设置控制台缓冲区大小
Console.SetBufferSize(1000, 500); //设置控制台缓冲区大小为200列,100行
//注意:
//1.先设置窗口大小,再设置缓冲区大小,否则可能会报错。
//2.缓冲区大小必须大于等于窗口大小,否则会报错。
//3.窗口的大小不能超过屏幕的大小,否则会报错。
//3.设置光标位置
//控制台左上角为(0,0),右下角为(控制台宽度-1, 控制台高度-1)
Console.SetCursorPosition(10, 10); //设置光标位置为(10,10)
Console.WriteLine("123");
//注意:
//1.光标位置不能超过控制台的大小,否则会报错。
//2.横纵距离单位不同,视觉上大约1y=2x
//4.设置颜色
//4.1 设置文字颜色(前景色)
Console.ForegroundColor = ConsoleColor.Red; //设置文字颜色为红色
//4.2 设置背景颜色
Console.BackgroundColor = ConsoleColor.Blue; //设置背景颜色为蓝色
Console.WriteLine("现在是红色文字,蓝色背景");
//4.3 重置颜色
Console.ResetColor(); //重置颜色为默认颜色
Console.Clear(); //重置后要清空控制台内容,否则颜色不会改变
Console.WriteLine("现在是默认颜色文字,默认背景");
//5.光标显影
Console.CursorVisible = false; //设置光标不可见
//注意:设置光标不可见后,光标位置仍然可以通过SetCursorPosition方法设置,但不会显示出来。
//6.关闭控制台
Environment.Exit(0); //退出程序,参数为退出码,0表示正常退出,非0表示异常退出
//注意:在调试模式时,不会自动关闭,在exe中才能体现。
#endregion
}
}
}
@@ -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>
+68
View File
@@ -0,0 +1,68 @@
Console.ForegroundColor = ConsoleColor.Yellow; //设置文字颜色为绿色
Console.BackgroundColor = ConsoleColor.Red; //设置背景颜色为黑色
Console.SetWindowSize(50, 25);
Console.SetBufferSize(50, 25);
Console.CursorVisible = false;
try
{
char dis;
int x=0, y=0;
while (true)
{
Console.Clear();
Console.SetCursorPosition(x, y);
Console.Write("■");
dis = Console.ReadKey(true).KeyChar;
switch (dis)
{
case 'w':
case 'W':
y--;
if (y < 0)
{
y = 0; //防止光标位置小于0
}
break;
case 'a':
case 'A':
x -= 2;
if (x < 0)
{
x = 0; //防止光标位置小于0
}
break;
case 's':
case 'S':
y++;
if (y > Console.BufferHeight - 1)
{
y = Console.BufferHeight - 1; //防止光标位置超过缓冲区高度
}
break;
case 'd':
case 'D':
x += 2;
if ( x > Console.BufferWidth - 2)
{
x = Console.BufferWidth - 2;
}
break;
}
}
}catch (Exception ex)
{
Console.WriteLine($"发生异常: {ex.Message}");
}
finally
{
Console.ResetColor(); //重置颜色为默认颜色
Console.Clear(); //清空控制台内容
Console.WriteLine("程序结束");
Environment.Exit(0);
}
@@ -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>
+24
View File
@@ -0,0 +1,24 @@
using System;
namespace
{
class Program
{
static void Main(string[] args)
{
#region
//固定写法
// Random 关键字
Random r = new Random();
#endregion
#region
int i = r.Next(); //生成一个非负随机整数
Console.WriteLine(i);
i = r.Next(100); //生成一个非负随机整数,范围为[0, 100)
Console.WriteLine(i);
i = r.Next(50, 100); //生成一个非负随机整数,范围为[50, 100)
Console.WriteLine(i);
#endregion
}
}
}
@@ -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,34 @@
Random r = new Random();
int bossHealth = 20;
int bossDefense = 10;
int round = 1;
try
{
Console.WriteLine("游戏开始");
while (true)
{
int attack = r.Next(8, 13);
Console.WriteLine("当前第{0}回合",round);
if (attack >= bossDefense)
{
bossHealth -= attack - bossDefense;
Console.WriteLine("你攻击了BOSS,造成了" + attack + "点伤害\n" + "BOSS剩余血量:" + bossHealth);
}else
{
Console.WriteLine("你的攻击被BOSS防御住了,造成了0点伤害");
}
round++;
if (bossHealth <= 0)
{
Console.WriteLine("你打赢了BOSS,共使用了{0}回合",round-1);
break;
}
}
}catch (Exception ex)
{
Console.WriteLine("错误代码: " + ex.Message);
}
finally
{
Console.WriteLine("游戏结束");
}
@@ -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>