C#四部曲入门——必备知识
·
一.控制台
1.输入输出
//输出
Console.WriteLine("123123");//光标空行
Console.Write("123123123");//不空行
//输入
string str = Console.ReadLine();
//ReadKey(true)不会把输入的内容显示在控制台上,但c已经存储了,可以输出
char c = Console.ReadKey(true).KeyChar;
2.控制台其他方法
(1)清空
Console.Clear();
(2)设置控制台的大小、
窗口大小,缓冲区大小
注意:
1.先设置窗口大小,再设置缓冲区大小;
2.缓冲区的大小不能小于窗口大小;
3.窗口大小不能大于控制台的最大尺寸。
//窗口大小
Console.SetWindowSize(100,50);
//缓冲区大小
Console.SetBufferSize(1000,1000);
(3)设置光标的位置
控制台的左上角为原点(0,0),右侧方向为x轴正方向,下侧方向为y轴正方向
注意:
1.边界问题(光标位置得在缓冲区有效范围内)
2.横纵距离单位不同(1y=2x)视觉上的
Console.SetCursorPosition(5,5);
Console.WriteLine("123123");
(4)设置颜色相关
文字颜色设置
Console.ForegroundColor = Console.Color.Red;
Console.WriteLine("123123");
Console.ForegroundColor = Console.Color.Green;
背景颜色设置
Console.BackgroundColor = Console.Color.White;
//重置背景颜色后需要Clear()一次,才能把整个背景颜色改变
Console.Clear();
(5)光标显隐
Console.CursorVisible = false;
(6)关闭控制台
Enrichment.Exit(0);
二.随机数
1.产生随机对象
固定写法:
Random 随机数变量名 = new Random();
2.生成随机数
Random r = new Random();
int i = r.Next();
Console.WriteLine(i);
//生成一个 0 ~ 99 的随机数,左边始终为0,左包含 右边是100,右不包含
i = r.Next(100);
Console.WriteLine(i);
//生成一个 5 ~ 99 的随机数,左边始终为0,左包含 右边是100,右不包含
i = r.Next(5,100);
Console.WriteLine(i);
三.项目调试
断点调试
调试会停在断点处,然后一步一步的执行。点击继续就会自动一步一步的运行。如果有多个断点,继续运行代码会暂停到下一个断点。
1.如何打断点
选择一行代码F9
2.如何一步一步的看代码逻辑
F10
3.继续执行(停止一步一步的看)
F5
4.可以通过监视窗口查看想要看的变量值
四.实践
1.需求分析
(1)开始界面
控制台输入输出、控制台颜色变化
(2)游戏界面
控制台输入输出、控制台颜色变化、while和switch、判断(条件运算符、if语句)、 回合制战斗(循环数、循环、if语句)
(3)结束界面
控制台输入输出、控制台颜色变化
(4) 界面之间的相互切换
2.控制台基础设置
#region 控制台基础设置
int width = 50;
int heigh = 30;
//隐藏光标
Console.CursorVisible = false;
//设置控制台大小
Console.SetWindowSize(width, heigh);
Console.SetBufferSize(width, heigh);
#endregion
3.多个场景
#region 多个场景
int sceneId = 1;
while (true)
{
//根据不同的场景Id,进行不同的逻辑
switch (sceneId)
{
//开始场景
case 1:
Console.Clear();
Console.WriteLine("开始场景");
break;
//游戏场景
case 2:
Console.Clear();
Console.WriteLine("游戏场景");
break;
//结束场景
case 3:
Console.Clear();
Console.WriteLine("结束场景");
break;
}
}
#endregion
4.开始场景逻辑实现
//开始场景
case 1:
Console.Clear();
//Console.WriteLine("开始场景");
#region 开始场景逻辑实现
Console.SetCursorPosition(width/2 - 4, 6);
Console.Write("营救公主");
//当前选项的编号,0为开始(红),1为退出(红)
int nowSetIndex = 0;
//输入(W\S控制按钮)
while (true)
{
bool isQuitWhile = false;
//显示内容
Console.SetCursorPosition(width / 2 - 4, 13);
//if(nowSetIndex == 0)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
Console.ForegroundColor = nowSetIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.Write("开始游戏");
Console.SetCursorPosition(width / 2 - 4, 15);
//if (nowSetIndex == 1)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
Console.ForegroundColor = nowSetIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.Write("退出游戏");
//检测输入
char input = Console.ReadKey(true).KeyChar;
switch(input)
{
//方法一,W只能向上选择S只能向下选择
case 'w':
case 'W':
--nowSetIndex;
if (nowSetIndex < 0)
{
nowSetIndex = 0;
}
break;
case 'S':
case 's':
++nowSetIndex;
if (nowSetIndex > 1)
{
nowSetIndex = 1;
}
break;
//方法二,W、S都可以选择,是循环滚轮
//case 'w':
//case 'W':
//case 'S':
//case 's':
// switch (nowSetIndex)
// {
// case 0:
// nowSetIndex = 1;
// break;
// case 1:
// nowSetIndex = 0;
// break;
// }
// break;
case 'j':
case 'J':
if(nowSetIndex == 0)
{
sceneId = 2;
isQuitWhile = true;
}
else
{
Environment.Exit(0);
}
break;
}
if(isQuitWhile)
{
break;
}
}
#endregion
break;
5.游戏场景逻辑实现
(1)不变的红墙
#region 不变的红墙
//设置颜色为红色
Console.ForegroundColor = ConsoleColor.Red;
//开始画墙
//横向的墙
for (int i = 0; i< width; i+=2)
{
Console.SetCursorPosition(i, 0);
Console.Write("■");
Console.SetCursorPosition(i, heigh - 7);
Console.Write("■");
Console.SetCursorPosition(i, heigh - 1);
Console.Write("■");
}
//纵向的墙
for (int i = 0; i < heigh; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(width - 2, i);
Console.Write("■");
}
#endregion
(2)Boss相关
while循环外
#region Boss相关
int bossX = 24, bossY = 15, bossAttackMin = 7, bossAttackMax = 13, bossHp = 100;
string bossIcon = "■";
//申明一个颜色变量
ConsoleColor bossColor = ConsoleColor.Green;
#endregion
while循环内
#region Boss相关
if (bossHp > 0)
{
Console.SetCursorPosition(bossX, bossY);
Console.ForegroundColor = bossColor;
Console.Write(bossIcon);
}
#endregion
(3)主角移动相关
while循环外
#region 玩家属性相关
int playerX = 4, playerY = 5, playerAttackMin = 8, playerAttackMax = 12, playerHp = 100;
string playerIcon = "●";
ConsoleColor playerColor = ConsoleColor.Yellow;
char playerInput;
which循环内
#region 玩家移动相关
Console.SetCursorPosition(playerX, playerY);
Console.ForegroundColor = playerColor;
Console.Write(playerIcon);
playerInput = Console.ReadKey(true).KeyChar;
#endregion
//非战斗状态
if (!isFight)
{
#region 玩家移动相关
Console.SetCursorPosition(playerX, playerY);
Console.Write(" ");
switch (playerInput)
{
case 'W':
case 'w':
--playerY;
if (playerY < 1)
{
playerY = 1;
}
else if (playerX == bossX && playerY == bossY && bossHp > 0)
{
++playerY;
}
break;
case 'A':
case 'a':
playerX -= 2;
if (playerX < 2)
{
playerX = 2;
}
else if (playerX == bossX && playerY == bossY && bossHp > 0)
{
playerX += 2;
}
break;
case 'S':
case 's':
++playerY;
if (playerY > heigh - 8)
{
playerY = heigh - 8;
}
else if (playerX == bossX && playerY == bossY && bossHp > 0)
{
--playerY;
}
break;
case 'D':
case 'd':
playerX += 2;
if (playerX > width - 4)
{
playerX = width - 4;
}
else if (playerX == bossX && playerY == bossY && bossHp > 0)
{
playerX -= 2;
}
break;
case 'j':
case 'J':
//开始战斗
if ((playerX == bossX && playerY == bossY - 1 ||
playerX == bossX && playerY == bossY + 1 ||
playerX == bossX - 2 && playerY == bossY ||
playerX == bossX + 2 && playerY == bossY) && bossHp > 0)
{
isFight = true;
Console.SetCursorPosition(2, heigh - 6);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("开始和boss战斗了,按J继续");
Console.SetCursorPosition(2, heigh - 5);
Console.Write("Boss当前血量为{0}", bossHp);
Console.SetCursorPosition(2, heigh - 4);
Console.Write("玩家当前血量为{0}", playerHp);
}
break;
}
#endregion
}
(4)主角和Boss战斗
while循环外
#region 玩家战斗相关
bool isFight = false;
#endregion
while循环内
//战斗状态
if (isFight)
{
#region 玩家战斗相关
if (playerInput == 'J' || playerInput == 'j')
{
//在这判断,玩家或者Boss是否死亡
if (playerHp <= 0)
{
sceneId = 3;
break;
}
if (bossHp <= 0)
{
Console.SetCursorPosition(bossX, bossY);
Console.Write(" ");
isFight = false;
}
else
{
//玩家打怪物
Random r = new Random();
int atk = r.Next(playerAttackMin, playerAttackMax);
bossHp -= atk;
//打印信息
Console.ForegroundColor = ConsoleColor.Green;
//先擦除这一行显示的内容
Console.SetCursorPosition(2, heigh - 5);
Console.Write(" ");
Console.SetCursorPosition(2, heigh - 5);
Console.Write("你对Boss造成了{0}点伤害,Boss剩余血量为{1}", atk, bossHp);
//怪物打玩家
if (bossHp > 0)
{
atk = r.Next(bossAttackMin, bossAttackMax);
playerHp -= atk;
//打印信息
Console.ForegroundColor = ConsoleColor.Yellow;
//先擦除这一行显示的内容
Console.SetCursorPosition(2, heigh - 4);
Console.Write(" ");
if (playerHp <= 0)
{
Console.SetCursorPosition(2, heigh - 4);
Console.Write("很遗憾,你没能通过Boss的试炼,战败了");
}
else
{
Console.SetCursorPosition(2, heigh - 4);
Console.Write("Boss对你造成了{0}点伤害,你剩余血量为{1}", atk, playerHp);
}
}
else
{
//擦除之前的战斗信息
Console.SetCursorPosition(2, heigh - 6);
Console.Write(" ");
Console.SetCursorPosition(2, heigh - 5);
Console.Write(" ");
Console.SetCursorPosition(2, heigh - 4);
Console.Write(" ");
//显示胜利信息
Console.SetCursorPosition(2, heigh - 6);
Console.Write("恭喜你战胜了Boss,快去营救公主");
Console.SetCursorPosition(2, heigh - 5);
Console.Write("前往公主身边,按J按键继续");
}
}
}
#endregion
}
(5)营救公主
while循环外
#region 公主属性相关
bool isGameOver = false;
int princessX = 24, princessY = 5;
string princessIcon = "★";
ConsoleColor princessColor = ConsoleColor.Blue;
#endregion
while循环内
#region 公主相关
if (bossHp <= 0)
{
Console.SetCursorPosition(princessX, princessY);
Console.ForegroundColor = princessColor;
Console.Write(princessIcon);
}
#endregion
if (isGameOver)
{
break;
}
非战斗状态中switch 的case'J'和case'j'中
else if((playerX == princessX && playerY == princessY - 1 ||
playerX == princessX && playerY == princessY + 1 ||
playerX == princessX - 2 && playerY == princessY ||
playerX == princessX + 2 && playerY == princessY) && bossHp <= 0)
{
sceneId = 3;
isGameOver = true;
}
6.结束场景逻辑实现
//结束场景
case 3:
Console.Clear();
//Console.WriteLine("结束场景");
#region 结束场景逻辑实现
//标题显示
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(width / 2 - 4, 6);
Console.Write("GameOver");
Console.ForegroundColor = ConsoleColor.Green;
Console.SetCursorPosition(width / 2 - 4, 7);
Console.Write(endStr);
int endIndex = 0;
while (true)
{
bool isQuitEnd = false;
Console.SetCursorPosition(width / 2 - 6, 10);
Console.ForegroundColor = endIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.Write("回到开始界面");
Console.SetCursorPosition(width / 2 - 4, 11);
Console.ForegroundColor = endIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.Write("退出游戏");
char input = Console.ReadKey(true).KeyChar;
switch (input)
{
case 'W':
case 'w':
--endIndex;
if (endIndex < 0)
{
endIndex = 0;
}
break;
case 'S':
case 's':
++endIndex;
if (endIndex > 1)
{
endIndex = 1;
}
break;
case 'J':
case 'j':
switch (endIndex)
{
case 0:
sceneId = 1;
isQuitEnd = true;
break;
case 1:
Environment.Exit(0);
break;
}
break;
}
if (isQuitEnd)
{
break;
}
}
#endregion
break;
更多推荐
所有评论(0)