注:要源码的私信我,官方也真是的,下个源码包都要VIP,服了!!!
必备知识点----控制台相关
char c = Console.ReadKey(true).KeyChar;
作用:可以不显示输入的这个字符。(后续会用到)
知识点二 重点记忆----实际开发中经常用到
控制台相关--练习
自己思路先写
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 必备知识点_控制台相关练习题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("控制台相关练习题");
//将控制台背景颜色改为红色
//Console.BackgroundColor = ConsoleColor.Red;
//Console.Clear();
//改变字体颜色
//Console.ForegroundColor = ConsoleColor.Yellow;
//Console.WriteLine("■");
//不停的输入 WASD 键 可以控制方块的移动
#region 用自己的思路先写
//将控制台背景颜色改为红色
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
//改变字体颜色
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("■");
//不停的输入 WASD 键 可以控制方块的移动
//隐藏光标
Console.CursorVisible = false;
int x = 0;
int y = 0;
bool b = true;
while (b)
{
//括号里加 true :控制台上可以不显示输入的字符
char c = Console.ReadKey(true).KeyChar;
//第一种清除之前信息的方法(闪烁比较严重)
//Console.Clear();
//第二种方法 把之前的方块擦除
Console.SetCursorPosition(x, y);
Console.WriteLine(" ");
switch (c)
{
case 'W':
case 'w':
if (y > 0)
{
y -= 1;
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
else
{
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
break;
case 'A':
case 'a':
if (x >= 2)
{
x -= 2;
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
else
{
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
break;
case 'S':
case 's':
//Console.BufferHeight -- 得到缓冲区的高度大小
if (y < Console.BufferHeight - 1)
{
y += 1;
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
else
{
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
break;
case 'D':
case 'd':
//Console.BufferWidth -- 得到缓冲区的宽度大小
if (x < Console.BufferWidth - 2)
{
x += 2;
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
else
{
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
}
break;
case 'T':
Environment.Exit(0);
break;
default:
Console.SetCursorPosition(x, y);
Console.WriteLine("■");
break;
}
}
#endregion
}
}
}
补充
代码依旧不够简练
必备知识点--随机数
Console.WriteLine("随机数");
#region 知识点一 产生随机数对象
//固定写法
//Random 随机数变量名 = new Readom();
Random r = new Random();
#endregion
#region 知识点二 生成随机数
int i = r.Next(); //生成一个非负数的随机数
Console.WriteLine(i);
//生成一个 0~99 的随机数 左边始终是0 左包含 右边是100 右不包含
i = r.Next(100);
Console.WriteLine(i);
//生成一个 5到99的随机数 左包含 右不包含
i = r.Next(5, 100);
Console.WriteLine(i);
#endregion
#region 自己思路先写
//自己思路先写
Random r = new Random();
int monsterDef = 10;
int monsterHp = 20;
int aotumAtk;
do
{
Console.WriteLine("=============奥特曼攻击小怪兽===========");
aotumAtk = r.Next(8, 13);
Console.WriteLine("奥特曼的攻击力为:" + aotumAtk);
if (aotumAtk > monsterDef)
{
monsterHp -= aotumAtk - monsterDef;
Console.WriteLine("小怪兽掉血{0},剩余血量{1}", aotumAtk - monsterDef, monsterHp);
}
else
{
Console.WriteLine("小怪兽防御力太厚!");
}
Console.WriteLine("再次攻击:");
Console.ReadKey();
Console.Clear();
} while (monsterHp > 0);
#endregion
#region 老师思路
Random r = new Random();
int monsterDef = 10;
int monsterHp = 20;
int aotumAtk;
while (true)
{
aotumAtk = r.Next(8, 13);
if (aotumAtk > monsterDef)
{
monsterHp -= aotumAtk - monsterDef;
if (monsterHp <= 0)
{
break;
}
Console.WriteLine("奥特曼本次攻击力为{0},造成{1}伤害,小怪兽剩{2}血量",
aotumAtk, aotumAtk - monsterDef, monsterHp);
}
else
{
Console.WriteLine("奥特曼本次攻击力为{0},不足以造成伤害", aotumAtk);
}
Console.WriteLine("请按任意键继续攻击");
Console.ReadKey(true);
Console.Clear();
}
Console.WriteLine("小怪物已死亡!");
#endregion
断点调试
实践项目:
控制方向的是:WSAD
确定键是:J
自己的思路先写:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 入门项目自个思路写
{
class Program
{
static void Main(string[] args)
{
#region 控制台基础设置
//隐藏光标
Console.CursorVisible = false;
//设置窗口和缓冲区大小
int x = 80;
int y = 40;
Console.SetWindowSize(x, y);
Console.SetBufferSize(x, y);
Random r = new Random();
int aoHP;
int geHP;
int aoAtk;
int geAtk;
#endregion
#region 界面切换
int nowSeneID = 1;
while (true)
{
switch (nowSeneID)
{
case 1:
Console.Clear();
Console.SetCursorPosition(30, 5);
Console.WriteLine("奥特曼营救公主");
char control = 'w';
while (true)
{
switch (control)
{
case 'W':
case 'w':
Console.SetCursorPosition(32, 8);
Console.WriteLine(" ");
Console.SetCursorPosition(32, 8);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("开始游戏");
Console.SetCursorPosition(32, 10);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("退出游戏");
nowSeneID = 2;
break;
case 'S':
case 's':
Console.SetCursorPosition(32, 8);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("开始游戏");
Console.SetCursorPosition(32, 10);
Console.WriteLine(" ");
Console.SetCursorPosition(32, 10);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("退出游戏");
nowSeneID = 4;
break;
}
control = Console.ReadKey(true).KeyChar;
//按J键就是进入另一个界面
if (control == 'J' || control == 'j')
{
break;
}
}
break;
case 2:
Console.Clear();
//边界制作
Console.ForegroundColor = ConsoleColor.Red;
for (int yy = 0; yy < 38; yy++)
{
for (int xx = 0; xx < 38; xx++)
{
if (yy == 0 || yy == 37 || yy == 30)
{
Console.Write("■");
}else if(xx == 0 || xx == 37)
{
Console.Write("■");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
//游戏制作
//怪兽的位置 (40,15)
Console.SetCursorPosition(40, 15);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("■");
//奥特曼初始位置(5,2)
int xxx = 6;
int yyy = 2;
char control2 = ' ';
while (true)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(xxx, yyy);
Console.WriteLine("●");
aoHP = 100;
geHP = 100;
if ((control2 == 'J' || control2 == 'j') && ((xxx == 40 && yyy == 16) || (xxx == 42 && yyy == 15)
|| (xxx == 40 && yyy == 14) || (xxx == 38 && yyy == 15)))
{
Console.SetCursorPosition(2, 31);
Console.WriteLine("开始战斗,按J键继续");
Console.SetCursorPosition(2, 32);
Console.WriteLine("奥特曼当前血量是:" + aoHP);
Console.SetCursorPosition(2, 33);
Console.WriteLine("小怪兽当前血量是:" + geHP);
while (true)
{
if (geHP <= 0)
{
Console.SetCursorPosition(40, 15);
Console.WriteLine(" ");
Console.SetCursorPosition(34, 10);
Console.WriteLine("★");
Console.SetCursorPosition(2, 31);
Console.WriteLine(" ");
Console.SetCursorPosition(2, 31);
Console.WriteLine("奥特曼战胜了小怪兽,快去营救小女孩。");
Console.SetCursorPosition(2, 32);
Console.WriteLine(" ");
Console.SetCursorPosition(2, 32);
Console.WriteLine("前往小女孩身边按J营救!");
Console.SetCursorPosition(2, 33);
Console.WriteLine(" ");
break;
}
else if (aoHP <= 0)
{
Console.SetCursorPosition(2, 31);
Console.WriteLine(" ");
Console.SetCursorPosition(2, 31);
Console.WriteLine("奥特曼没能打赢小怪兽,请重新挑战。");
Console.SetCursorPosition(2, 32);
Console.WriteLine(" ");
Console.SetCursorPosition(2, 33);
Console.WriteLine(" ");
nowSeneID = 1;
break;
}
char control3 = Console.ReadKey(true).KeyChar;
if(control3 == 'J' || control3 == 'j')
{
aoAtk = r.Next(10, 14);
geAtk = r.Next(10, 16);
aoHP -= geAtk;
geHP -= aoAtk;
Console.SetCursorPosition(2, 31);
Console.WriteLine(" ");
Console.SetCursorPosition(2, 31);
Console.WriteLine("开始战斗,按J键继续");
Console.SetCursorPosition(2, 32);
Console.WriteLine(" ");
Console.SetCursorPosition(2, 32);
Console.WriteLine("奥特曼对小怪兽造成:{0}点伤害,小怪兽剩余血量:{1}"
, aoAtk, geHP);
Console.SetCursorPosition(2, 33);
Console.WriteLine(" ");
Console.SetCursorPosition(2, 33);
Console.WriteLine("小怪兽对奥特曼造成:{0}点伤害,奥特曼剩余血量:{1}"
, geAtk, aoHP);
}
}
}
//当到达公主身边时
control2 = Console.ReadKey(true).KeyChar;
if ((control2 == 'J' || control2 == 'j') && ((xxx == 34 && yyy == 11) || (xxx == 36 && yyy == 10) ||
(xxx == 34 && yyy == 9) || (xxx == 32 && yyy == 10)))
{
nowSeneID = 3;
break;
}
Console.SetCursorPosition(xxx, yyy);
Console.WriteLine(" ");
switch (control2)
{
case 'W':
case 'w':
if((xxx == 40 && yyy == 16) || (xxx == 34 && yyy == 11))
{
break;
}
if(yyy > 1)
{
yyy -= 1;
}
break;
case 'A':
case 'a':
if ((xxx == 42 && yyy == 15) || (xxx == 36 && yyy == 10))
{
break;
}
if (xxx > 2)
{
xxx -= 2;
}
break;
case 'S':
case 's':
if ((xxx == 40 && yyy == 14) || (xxx == 34 && yyy == 9))
{
break;
}
if (yyy < 29)
{
yyy += 1;
}
break;
case 'D':
case 'd':
if ((xxx == 38 && yyy == 15) || (xxx == 32 && yyy == 10))
{
break;
}
if (xxx < 71)
{
xxx += 2;
}
break;
}
}
break;
case 3:
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(30,5);
Console.WriteLine("========游戏通关=========");
//W、S控制上下选择
char control4 = 'W';
while (true)
{
switch (control4)
{
case 'W':
case 'w':
Console.SetCursorPosition(36, 8);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("回到开始界面");
Console.SetCursorPosition(38, 10);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("退出游戏");
nowSeneID = 1;
break;
case 'S':
case 's':
Console.SetCursorPosition(36, 8);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("回到开始界面");
Console.SetCursorPosition(38, 10);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("退出游戏");
nowSeneID = 4;
break;
}
control4 = Console.ReadKey(true).KeyChar;
if(control4 == 'J' || control4 == 'j')
{
break;
}
}
break;
case 4:
Environment.Exit(0);
break;
}
}
#endregion
}
}
}
实现视频:
QQ录屏20231126183527
总结:因为是面向过程编程,逻辑不够清晰,但最后功能都能够实现。
跟着老师的思路一步一步实现如下:
1.需求分析
2.控制台基础设置:
3.多场景
4.开始场景的逻辑实现
//开始场景
case 1:
Console.Clear();
#region 3 开始场景逻辑
Console.SetCursorPosition(w / 2 - 7, 8);
Console.WriteLine("奥特曼营救公主");
//当前选择的编号
int nowSelIndex = 0;
//因为要输入 可以构造一个 开始界面的 死循环
//专门用来处理 开始场景相关的逻辑
while (true)
{
//用一个标识 来处理 想要在while循环内部的switch逻辑执行时 希望退出外层while循环时
//改变标识符即可
bool isQuitWhile = false;
//显示 内容
Console.SetCursorPosition(w / 2 - 4, 13);
//根据当前选择的编号 来决定 是否变色
//if(nowSelIndex == 0)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
//用三目运算符代替上面的if语句
Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine("开始游戏");
Console.SetCursorPosition(w / 2 - 4, 15);
//if (nowSelIndex == 1)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
//用三目运算符代替
Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine("退出游戏");
//检测 输入
//检测玩家 输入
char input = Console.ReadKey(true).KeyChar;
switch (input)
{
case 'W':
case 'w':
nowSelIndex = 0;
break;
case 'S':
case 's':
nowSelIndex = 1;
break;
case 'J':
case 'j':
if (nowSelIndex == 0)
{
//1.改变当前选择的场景ID
nowSceneID = 2;
//2.要退出 内层while循环
isQuitWhile = true;
}
else
{
//关闭控制台
Environment.Exit(0);
}
break;
}
//当 isQuitwhile 为 true 时退出外层while循环
if (isQuitWhile)
{
break;
}
}
#endregion
break;
5.游戏场景逻辑实现
不变红墙的实现
//设置颜色为红色
Console.ForegroundColor = ConsoleColor.Red;
//画墙
//上方墙
for (int i = 0; i < w - 2; i += 2)
{
Console.SetCursorPosition(i, 0);
Console.Write("■");
//整合
//下方墙
Console.SetCursorPosition(i, h - 1);
Console.Write("■");
//中间墙
Console.SetCursorPosition(i, h - 6);
Console.Write("■");
}
//下方墙
//for (int i = 0; i < w - 2; i += 2)
//{
// Console.SetCursorPosition(i, h - 1);
// Console.Write("■");
//}
//中间墙
//for (int i = 0; i < w - 2; i += 2)
//{
// Console.SetCursorPosition(i, h - 6);
// Console.Write("■");
//}
//左边墙
for (int i = 0; i < h; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
//整合
//右边墙
Console.SetCursorPosition(w - 4, i);
Console.Write("■");
}
//右边墙
//for (int i = 0; i < h; i++)
//{
// Console.SetCursorPosition(w - 4, i);
// Console.Write("■");
//}
#endregion
boss的实现代码
玩家移动相关
主角和boss战斗相关
公主相关
结束场景相关
所有代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 入门实践项目
{
class Program
{
static void Main(string[] args)
{
#region 控制台基础设置
//隐藏光标
Console.CursorVisible = false;
//设置舞台的大小
int w = 50;
int h = 30;
Console.SetWindowSize(w, h);
Console.SetBufferSize(w, h);
#endregion
#region 2 多个场景
//当前所在场景的编号
int nowSceneID = 1;
#region 9.结束场景相关
//结束场景显示的 文字提示内容
string gameOverInfo = "";
#endregion
while (true)
{
//不同场景ID 进行不同的逻辑处理
switch (nowSceneID)
{
//开始场景
case 1:
Console.Clear();
#region 3 开始场景逻辑
Console.SetCursorPosition(w / 2 - 7, 8);
Console.WriteLine("奥特曼营救公主");
//当前选择的编号
int nowSelIndex = 0;
//因为要输入 可以构造一个 开始界面的 死循环
//专门用来处理 开始场景相关的逻辑
while (true)
{
//用一个标识 来处理 想要在while循环内部的switch逻辑执行时 希望退出外层while循环时
//改变标识符即可
bool isQuitWhile = false;
//显示 内容
Console.SetCursorPosition(w / 2 - 4, 13);
//根据当前选择的编号 来决定 是否变色
//if(nowSelIndex == 0)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
//用三目运算符代替上面的if语句
Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine("开始游戏");
Console.SetCursorPosition(w / 2 - 4, 15);
//if (nowSelIndex == 1)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
//用三目运算符代替
Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine("退出游戏");
//检测 输入
//检测玩家 输入
char input = Console.ReadKey(true).KeyChar;
switch (input)
{
case 'W':
case 'w':
nowSelIndex = 0;
break;
case 'S':
case 's':
nowSelIndex = 1;
break;
case 'J':
case 'j':
if (nowSelIndex == 0)
{
//1.改变当前选择的场景ID
nowSceneID = 2;
//2.要退出 内层while循环
isQuitWhile = true;
}
else
{
//关闭控制台
Environment.Exit(0);
}
break;
}
//当 isQuitwhile 为 true 时退出外层while循环
if (isQuitWhile)
{
break;
}
}
#endregion
break;
//游戏场景
case 2:
Console.Clear();
#region 4.不变的红墙
//设置颜色为红色
Console.ForegroundColor = ConsoleColor.Red;
//画墙
//上方墙
for (int i = 0; i < w - 2; i += 2)
{
Console.SetCursorPosition(i, 0);
Console.Write("■");
//整合
//下方墙
Console.SetCursorPosition(i, h - 1);
Console.Write("■");
//中间墙
Console.SetCursorPosition(i, h - 6);
Console.Write("■");
}
//下方墙
//for (int i = 0; i < w - 2; i += 2)
//{
// Console.SetCursorPosition(i, h - 1);
// Console.Write("■");
//}
//中间墙
//for (int i = 0; i < w - 2; i += 2)
//{
// Console.SetCursorPosition(i, h - 6);
// Console.Write("■");
//}
//左边墙
for (int i = 0; i < h; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
//整合
//右边墙
Console.SetCursorPosition(w - 4, i);
Console.Write("■");
}
//右边墙
//for (int i = 0; i < h; i++)
//{
// Console.SetCursorPosition(w - 4, i);
// Console.Write("■");
//}
#endregion
#region 5.boss属性相关
int bossX = 24;
int bossY = 15;
int bossAtkMin = 7;
int bossAtkMax = 13;
int bossHp = 100;
string bossIcon = "■";
//申明一个 颜色变量
ConsoleColor bossColor = ConsoleColor.Green;
#endregion
#region 8.公主相关
int princessX = 24;
int princessY = 5;
string princessIcon = "★";
ConsoleColor princessColor = ConsoleColor.Blue;
#endregion
#region 6.玩家属性相关
int playerX = 4;
int playerY = 5;
int playerAtkMin = 8;
int playerAtkMax = 12;
int playerHp = 100;
string playerIcon = "●";
ConsoleColor playerColor = ConsoleColor.Yellow;
//玩家输入的内容 外面申明 可以节约性能
char playerInput;
#endregion
#region 7.玩家战斗相关
//战斗状态
bool isFight = false;
//作用是 从while循环内部的switch 改变标识符 用来跳出外层的while循环 -- 用于公主相关
bool isOver = false;
#endregion
//游戏场景的死循环 专门用来 检测 玩家输入相关循环
while (true)
{
#region 5.boss绘制
//boss活着时才绘制
if (bossHp > 0)
{
//绘制boss图标
Console.SetCursorPosition(bossX, bossY);
Console.ForegroundColor = bossColor;
Console.WriteLine(bossIcon);
}
#endregion
#region 8 公主相关
else
{
Console.SetCursorPosition(princessX, princessY);
Console.ForegroundColor = princessColor;
Console.Write(princessIcon);
}
#endregion
//画出玩家
Console.SetCursorPosition(playerX, playerY);
Console.ForegroundColor = playerColor;
Console.WriteLine(playerIcon);
//得到玩家输入
playerInput = Console.ReadKey(true).KeyChar;
//战斗状态处理什么逻辑
if (isFight)
{
//如果是战斗状态 你要做什么
//只会处理J键
if(playerInput == 'J' || playerInput == 'j')
{
//在这判断 玩家或者怪物 是否死亡 继续后面的流程
if(playerHp <= 0)
{
//游戏结束
//应该直接显示 游戏结束场景
nowSceneID = 3;
gameOverInfo = "游戏失败";
break;
}
else if(bossHp <= 0)
{
//去营救公主
//boss擦除
Console.SetCursorPosition(bossX, bossY);
Console.WriteLine(" ");
isFight = false;
}
else
{
//去处理按J键打架
//玩家打怪物
Random r = new Random();
//得到随机攻击力
int atk = r.Next(playerAtkMin, playerAtkMax);
//boss血量减对应攻击力
bossHp -= atk;
//打印信息
Console.ForegroundColor = ConsoleColor.Green;
//先擦除之前显示的内容
Console.SetCursorPosition(2, h - 4);
Console.WriteLine(" ");
//再来填写
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("你对boss造成了{0}点伤害,boss剩余血量为{1}", atk, bossHp);
//怪兽打玩家
if (bossHp > 0)
{
//得到随机攻击力
atk = r.Next(bossAtkMin, bossAtkMax);
//玩家血量减怪兽攻击力
playerHp -= atk;
//打印信息
Console.ForegroundColor = ConsoleColor.Yellow;
//先擦除之前显示的内容
Console.SetCursorPosition(2, h - 3);
Console.WriteLine(" ");
//再来填写
//Console.SetCursorPosition(2, h - 3);
//Console.WriteLine("boss对你造成了{0}点伤害,你剩余血量为{1}", atk, playerHp);
//boss如果把玩家打死了 做什么
if (playerHp <= 0)
{
Console.SetCursorPosition(2, h - 3);
Console.Write("很遗憾,你没能战胜boss。");
}
else
{
Console.SetCursorPosition(2, h - 3);
Console.Write("boss对你造成了{0}点伤害,你剩余血量为{1}", atk, playerHp);
}
}
else
{
//擦除之前的战斗信息
Console.SetCursorPosition(2, h - 5);
Console.WriteLine(" ");
Console.SetCursorPosition(2, h - 4);
Console.WriteLine(" ");
Console.SetCursorPosition(2, h - 3);
Console.WriteLine(" ");
//显示恭喜胜利的信息
Console.SetCursorPosition(2, h - 5);
Console.WriteLine("你战胜了boss,快去营救公主");
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("前往公主身边按J键继续");
}
}
}
}
//非战斗状态处理什么逻辑
else
{
#region 6.玩家移动相关
//擦除
Console.SetCursorPosition(playerX, playerY);
Console.WriteLine(" ");
//改位置
switch (playerInput)
{
case 'W':
case 'w':
--playerY;
if (playerY < 1)
{
playerY = 1;
}
//位置如果和boss重合了 并且boss没有死
else if (playerX == 24 && playerY == 15 && bossHp > 0)
{
//拉回去
++playerY;
}
else if(playerX == princessX && playerY == princessY && bossHp <= 0)
{
//拉回去
++playerY;
}
break;
case 'A':
case 'a':
playerX -= 2;
if (playerX < 2)
{
playerX = 2;
}
//位置如果和boss重合了 并且boss没有死
else if (playerX == 24 && playerY == 15 && bossHp > 0)
{
playerX += 2;
}
else if (playerX == princessX && playerY == princessY && bossHp <= 0)
{
//拉回去
playerX += 2;
}
break;
case 'S':
case 's':
++playerY;
if (playerY > h - 7)
{
--playerY;
}
//位置如果和boss重合了 并且boss没有死
else if (playerX == 24 && playerY == 15 && bossHp > 0)
{
//拉回去
--playerY;
}
else if (playerX == princessX && playerY == princessY && bossHp <= 0)
{
//拉回去
--playerY;
}
break;
case 'D':
case 'd':
playerX += 2;
if (playerX > w - 6)
{
playerX -= 2;
}
//位置如果和boss重合了 并且boss没有死
else if (playerX == 24 && playerY == 15 && bossHp > 0)
{
playerX -= 2;
}
else if (playerX == princessX && playerY == princessY && bossHp <= 0)
{
//拉回去
playerX -= 2;
}
break;
case 'J':
case 'j':
#region 7.玩家战斗相关
//开始战斗
//要让玩家不能移动
//下方能够显示信息
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, h - 5);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("开始和boss战斗了,按J键继续");
Console.SetCursorPosition(2, h - 4);
Console.WriteLine("玩家当前血量为:{0}", playerHp);
Console.SetCursorPosition(2, h - 3);
Console.WriteLine("boss当前血量为:{0}", bossHp);
}
#endregion
#region 8.公主相关
else if ((playerX == princessX && playerY == princessY - 1 ||
playerX == princessX && playerY == princessY + 1 ||
playerX == princessX - 2 && playerY == princessY ||
playerX == princessX + 2 && playerY == princessY) && bossHp <= 0)
{
//改变 场景ID
nowSceneID = 3;
gameOverInfo = "游戏通关";
//跳出 游戏界面的while循环 回到主循环
isOver = true;
break;
}
#endregion
//判断是否在公主身边按J键
break;
}
#endregion
}
//外层while循环逻辑
if (isOver)
{
//和while循环配对
break;
}
}
break;
//结束场景
case 3:
Console.Clear();
#region 9.结束场景逻辑
//标题的显示
Console.SetCursorPosition(w / 2 - 4, 5);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("GameOver");
//可变内容的显示 根据失败或者 成功显示的内容不一样
Console.SetCursorPosition(w / 2 - 4, 7);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(gameOverInfo);
int nowSelEndIndex = 0;
while (true)
{
bool isQuitEndWhile = false;
Console.SetCursorPosition(w / 2 - 6, 9);
Console.ForegroundColor = nowSelEndIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine("回到开始界面");
Console.SetCursorPosition(w / 2 - 4, 11);
Console.ForegroundColor = nowSelEndIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine("退出游戏");
char input = Console.ReadKey(true).KeyChar;
switch (input)
{
case 'W':
case 'w':
nowSelEndIndex = 0;
break;
case 'S':
case 's':
nowSelEndIndex = 1;
break;
case 'J':
case 'j':
if (nowSelEndIndex == 0)
{
nowSceneID = 1;
isQuitEndWhile = true;
}
else
{
Environment.Exit(0);
}
break;
}
//为了 从switch 中跳出上一层 while循环 加的标识
if (isQuitEndWhile)
{
break;
}
}
#endregion
break;
}
}
#endregion
}
}
}
实现视频:
C#入门项目老师实现
总结:
对比发现自己写的代码逻辑不够清晰,并且缺少了boss擦除(即自己只实现了boss的图标擦除,boss的位置并没有擦除)、公主位置出现、战胜boss后战斗状态未能终止。
经验:
老师多次用到了 跳出循环的标识符(学习的要点)