基于C#模拟实现回合制游戏
前言
文章开始把我喜欢的这句话送个大家:这个世界上还有什么比自己写的代码运行在一亿人的电脑上更酷的事情吗,如果有那就是让这个数字再扩大十倍!!!
底部有下载链接:附带代码、答辩ppt、数据、音乐等素材。
规则简介
1>创建一个hero类特征name(英雄名字),hp(血量),mp(魔法值), physicsattack(物理攻击力),magicattack(魔法攻击力)
2>创建一个battlecontrol战斗管理类,该类有两个字段分别为list<hero> ours(存储己方阵营中五个英雄)和list<hero> enemy(存储对方五个英雄)类型,分别创建五个英雄和五个敌人分别添加到对应list泛型列表中。添加int类型字段round 记录战斗开始到结束总的回合数。注:英雄名字自定义,血量200 魔法值100 攻击力自定义。
3>模拟回合制游戏逻辑,初始化完毕后首先己方攻击,每次攻击随机从对方阵营筛选一个敌人进行攻击,
每次造成的伤害为物理攻击和魔法攻击的叠加值,如果没有魔法值那么伤害值只是物理攻击力即可。
每次魔法攻击之后需要减去相应的魔法值,比如魔法攻击力为20,那么魔法值也需要消耗20个点。
攻击的同时输出信息格式:小一攻击了小二,小二剩余血量为80。
己方所有英雄攻击完毕之后,敌人攻击,攻击流程同上。
4>当某一个敌人或者己方英雄死亡后将死亡角色从对应list泛型列表中移除,某一个阵营全部死亡,游戏结束,开始结算,显示获胜方。显示总的回合数。
可思考完善上述流程,比如添加加血buff,技能释放,闪避等等功能,丰富战斗内容
ps:如有报错,请根据本机音乐文件设置绝对路径!!!
功能介绍
初始化界面:复制测试用例即可
开始界面
查看敌我方英雄信息界面
更改敌我方英雄状况界面
强化敌我方英雄属性界面
激活敌我方英雄大招界面
打斗界面
实现代码
using system; using system.collections.generic; using system.threading; using system.linq; using system.text; using system.threading.tasks; using system.media;//在命名空间中添加 namespace bigwork { class program { static void main(string[] args) { system.media.soundplayer sp = new soundplayer(); sp.soundlocation = @"e:\qianyan\大作业\test\music\英雄联盟 - welcome to planet urf.wav"; sp.playlooping(); list<hero> ours = new list<hero>(); //存放我方英雄集合 list<hero> enemy = new list<hero>();//存放敌方英雄集合 input(ours, enemy); //输入我方、敌方英雄信息(引用传递) battlecontrol.instance.ours = ours; battlecontrol.instance.enemy = enemy; while (true) { function_print(); string a = console.readline(); if (string.isnullorempty(a)) { console.writeline("输出指令有误,按任意键重新输入"); console.readkey(); console.clear(); continue; } int n = int.parse(a); switch (n) { case 1: showallinfo(ours); console.writeline("点击任意按键继续…………"); console.readkey(); console.clear(); break; case 2: showallinfo(enemy); console.writeline("点击任意按键继续…………"); console.readkey(); console.clear(); break; case 3: if (ours[0].modify == true) modifyinfo(ours); else console.writeline("战斗已经进行,无法再进行修改"); console.readkey(); console.clear(); break; case 4: if (ours[0].modify == true) modifyinfo(enemy); else console.writeline("战斗已经进行,无法再进行修改"); console.readkey(); console.clear(); break; case 5: strengthenhero(ours); break; case 6: strengthenhero(enemy); break; case 7: activateheroultimateskills(ours); break; case 8: activateheroultimateskills(enemy); break; case 9: system.media.soundplayer sp1 = new soundplayer(); sp1.soundlocation = @"e:\qianyan\大作业\test\music\网络歌手 - 德玛德玛德玛西亚 00_00_00-00_00_04.wav"; sp1.play(); thread.sleep(5000); sp.soundlocation = @"e:\qianyan\大作业\test\music\英雄联盟 - welcome to planet urf.wav"; sp.playlooping(); battlecontrol.instance.fight(3);//3回合后激活一次 break; } } console.readkey(); } #region 输入我方、敌方英雄信息功能 public static void input(list<hero> ours, list<hero> enemy) { string a, d; int b, c; //输入我方英雄基本信息 for (int i = 1; i <= 5; i++) { console.writeline("请输入我方第{0}位英雄的名称:", i); a = console.readline(); console.writeline("请输入我方第{0}位英雄的物理攻击力:", i); b = int.parse(console.readline()); console.writeline("请输入我方第{0}位英雄的魔法攻击力:", i); c = int.parse(console.readline()); console.writeline("请输入我方第{0}位英雄的终极技能:", i); d = console.readline(); hero hero = new hero(a, b, c, d); ours.add(hero); console.clear(); //清屏函数 } //输入敌方英雄基本信息 for (int i = 1; i <= 5; i++) { console.writeline("请输入敌方第{0}位英雄的名称:", i); a = console.readline(); console.writeline("请输入敌方第{0}位英雄的物理攻击力:", i); b = int.parse(console.readline()); console.writeline("请输入敌方第{0}位英雄的魔法攻击力:", i); c = int.parse(console.readline()); console.writeline("请输入敌方第{0}位英雄的终极技能:", i); d = console.readline(); hero hero = new hero(a, b, c, d); enemy.add(hero); console.clear(); //清屏函数 } console.writeline("正在为您初始化英雄角色,请稍等…………"); thread.sleep(1000); console.clear(); } #endregion #region 显示功能选项函数 //static:不用创建对象就可以调用 public static void function_print() { console.writeline("****************************"); console.writeline("* *"); console.writeline("* 英雄大乱斗回合制游戏 *"); console.writeline("* *"); console.writeline("* 1、查看我方英雄情况 *"); console.writeline("* 2、查看敌方英雄情况 *"); console.writeline("* 3、更改我方英雄情况 *"); console.writeline("* 4、更改敌方英雄情况 *"); console.writeline("* 5、强化我方英雄 *"); console.writeline("* 6、强化敌方英雄 *"); console.writeline("* 7、激活我方英雄大招 *"); console.writeline("* 8、激活敌方英雄大招 *"); console.writeline("* 9、开始回合制打斗 *"); console.writeline("* *"); console.writeline("****************************"); } #endregion #region 查看英雄信息函数 public static void showallinfo(list<hero> hero) { console.writeline("5位英雄信息如下:"); foreach (var i in hero) { i.showinfo(); } } #endregion #region 修改信息 public static void modifyinfo(list<hero> heros) { string a; showallinfo(heros); console.write("\n输入修改的编号(从1开始):"); a = console.readline(); if (string.isnullorempty(a)) { console.writeline("输入有误,重新选择"); console.readline(); console.clear(); return; } int t = int.parse(a); if (t > heros.count) { console.writeline("输入数值超过英雄编号长度,重新选择需求"); } else { hero hero = heros[t - 1]; console.writeline("输入修改的值(按:姓名、血量、蓝量、物理攻击、魔法攻击写,不修改填-1或直接回车,填写好后按回车键修改各个值):"); hero.setcolor(consolecolor.red, consolecolor.black); console.writeline("注:英雄终极技能一旦创建无法更改"); hero.colorreset(); a = console.readline(); if (a != "-1" && !string.isnullorempty(a)) hero.name = a; a = console.readline(); if (a != "-1" && !string.isnullorempty(a)) hero.hp = int.parse(a); a = console.readline(); if (a != "-1" && !string.isnullorempty(a)) hero.mp = int.parse(a); a = console.readline(); if (a != "-1" && !string.isnullorempty(a)) hero.physicsattack = int.parse(a); a = console.readline(); if (a != "-1" && !string.isnullorempty(a)) hero.magicattack = int.parse(a); console.writeline("修改后的数据"); showallinfo(heros); } } #endregion #region 强化英雄 public static void strengthenhero(list<hero> heros) { string a; showallinfo(heros); console.write("\n输入强化的编号(从1开始):"); a = console.readline(); if (string.isnullorempty(a)) { console.writeline("输入有误,重新选择"); console.readline(); console.clear(); return; } int t = int.parse(a); if (t > heros.count) { console.writeline("输入数值超过英雄编号长度,重新选择需求"); console.readline(); console.clear(); } else { hero hero = heros[t - 1]; console.write("添加血量值:"); a = console.readline(); if (!string.isnullorempty(a) && int.parse(a) >= 0) hero.hp += int.parse(a); console.write("添加蓝量值:"); a = console.readline(); if (!string.isnullorempty(a) && int.parse(a) >= 0) hero.mp += int.parse(a); console.write("设置红buff(输入1为设置):"); a = console.readline(); if (!string.isnullorempty(a) && int.parse(a) == 1) hero.redbuff = true; console.write("设置蓝buff(输入1为设置):"); a = console.readline(); if (!string.isnullorempty(a) && int.parse(a) == 1) hero.bluebuff = true; console.write("设置眩晕buff(输入1为设置):"); a = console.readline(); if (!string.isnullorempty(a) && int.parse(a) == 1) hero.dizzybuff = true; } } #endregion #region 激活终极技能 public static void activateheroultimateskills(list<hero> heros) { console.writeline("终极技能激活情况"); foreach (var hero in heros) { console.write("{0}的终极技能:", hero.name); if (hero.activateultimateskills == false) { hero.setcolor(consolecolor.red, consolecolor.black); console.writeline("未激活"); hero.colorreset(); } else { hero.setcolor(consolecolor.green, consolecolor.black); console.writeline("已激活"); hero.colorreset(); } } console.write("\n输入激活大招的编号(从1开始):"); string a = console.readline(); if (string.isnullorempty(a)) { console.writeline("输入有误,重新选择"); console.readline(); console.clear(); return; } int t = int.parse(a); if (t > heros.count) { console.writeline("输入数值超过英雄编号长度,重新选择需求"); console.readkey(); } else heros[t - 1].activateultimateskills = true; console.clear(); } #endregion } class hero { private string name; private int hp = 200; // 血量 private int mp = 100; // 魔法值 private int physicsattack; // 物理攻击力 private int magicattack; // 魔法攻击力 private string ultimateskills; // 终极技能 private bool sign = false; //本回合攻击过就标记 private bool modify = true; // 设置是否能再次修改信息 private bool activateultimateskills = false; // 终极技能的激活情况 private bool bluebuff = false; // 设置蓝buff效果 private bool redbuff = false; // 设置红buff效果 private bool dizzybuff = false; // 设置晕眩buff效果 private bool dizzy = false; // 设置晕眩效果 public string name { get => name; set => name = value; } public int hp { get => hp; set => hp = value; } public int mp { get => mp; set => mp = value; } public int physicsattack { get => physicsattack; set => physicsattack = value; } public int magicattack { get => magicattack; set => magicattack = value; } public string ultimateskills { get => ultimateskills; set => ultimateskills = value; } public bool modify { get => modify; set => modify = value; } public bool activateultimateskills { get => activateultimateskills; set => activateultimateskills = value; } public bool bluebuff { get => bluebuff; set => bluebuff = value; } public bool redbuff { get => redbuff; set => redbuff = value; } public bool dizzybuff { get => dizzybuff; set => dizzybuff = value; } public bool dizzy { get => dizzy; set => dizzy = value; } public bool sign { get => sign; set => sign = value; } public hero() { } public hero(string name, int physicsattack, int magicattack, string ultimateskills) { this.name = name; this.physicsattack = physicsattack; this.magicattack = magicattack; this.ultimateskills = ultimateskills; } // 展示信息 public void showinfo() { console.write("英雄名称:"); setcolor(consolecolor.green, consolecolor.black); console.write(" " + name + " "); colorreset(); console.write(" 血量:"); setcolor(consolecolor.red, consolecolor.black); console.write(" " + hp + " "); colorreset(); console.write(" 魔法值:"); setcolor(consolecolor.blue, consolecolor.black); console.write(" " + mp + " "); colorreset(); console.write(" 物理攻击力:"); setcolor(consolecolor.green, consolecolor.black); console.write(" " + physicsattack + " "); colorreset(); console.write(" 魔法攻击力:"); setcolor(consolecolor.green, consolecolor.black); console.write(" " + magicattack + " "); colorreset(); console.write(" 终极技能:"); setcolor(consolecolor.cyan, consolecolor.black); console.write(" " + ultimateskills + " "); colorreset(); console.writeline(); } public static void setcolor(consolecolor a, consolecolor b) { console.backgroundcolor = a; console.foregroundcolor = b; } public static void colorreset() { console.backgroundcolor = consolecolor.black; console.foregroundcolor = consolecolor.white; } } class battlecontrol { //回合 private int round = 1; //己方人物 private list<hero> ours; //敌方人物 private list<hero> enemy; private static battlecontrol instance = null; private battlecontrol() { } public static battlecontrol instance { get { if (instance == null) { instance = new battlecontrol(); } return instance; } } public int round { get => round; set => round = value; } internal list<hero> ours { get => ours; set => ours = value; } internal list<hero> enemy { get => enemy; set => enemy = value; } //实现回合制打斗方法 public void fight(int activation) { int i, j; random random = new random(); int sum = 0; while (true) { console.writeline("第{0}回合开始!!!!!!!!", round); sum = 0; while (sum != (ours.count + enemy.count)) { sum = 0; #region 选出所要战斗的英雄 i = (int)random.next(0, ours.count);//我方英雄 thread.sleep(1000); j = (int)random.next(0, enemy.count);//敌方英雄 #endregion #region 大招 //回合数达到三 就可以释放或者不释放 if (round == activation) { console.writeline("是否放大招1是放 2不放:"); string a = console.readline(); int n = int.parse(a); for (int t = 0; t < ours.count; t++) { if (ours[t].hp != 0) { ours[t].activateultimateskills = true; } switch (n) { case 1: for (int k = 0; k < ours.count; k++) { if (enemy[j].hp > 0) { enemy[j].hp = enemy[j].hp > 10 ? enemy[j].hp - 10 : 0; } } break; case 2: break; } } n = int.parse(a); for (int t = 0; t < enemy.count; t++) { if (enemy[t].hp != 0) { ours[t].activateultimateskills = true; } switch (n) { case 1: for (int k = 0; k < ours.count; k++) { if (ours[i].hp > 0) { ours[i].hp = ours[i].hp > 10 ? ours[i].hp - 10 : 0; } } break; case 2: break; } } } #endregion if (ours.count == 0) { break; } if (enemy.count == 0) { break; } if (!ours[i].sign && !enemy[j].sign)//本回合都未进攻过 { //进攻操作 //首先把选出的英雄进行标记 ours[i].sign = true; enemy[j].sign = true; //去判断双方所带的增益有哪些 //如果进攻方有一方有眩晕buff就让对方眩晕 if (ours[i].dizzybuff) { enemy[j].dizzy = true; } if (enemy[j].dizzybuff) { ours[i].dizzy = true; } //进攻的时候判断 如果已经被眩晕就不进攻 if (ours[i].dizzy && !enemy[j].dizzy) { enemy_attack(i, j); } else if (!ours[i].dizzy && enemy[j].dizzy) { ours_attack(i, j); } else if (!ours[i].dizzy && !enemy[j].dizzy) { ours_attack(i, j); enemy_attack(i, j); } else if (ours[i].dizzy && enemy[j].dizzy) { console.writeline("双方都被眩晕所以这回合两个英雄都未掉血!!!"); } } else if (ours[i].sign && !enemy[j].sign) { enemy[j].sign = true;//把没有进行进攻的标记 enemy_attack(i, j); } else if (!ours[i].sign && enemy[j].sign) { ours[i].sign = true;//把没有进行进攻的标记 ours_attack(i, j); } else if (ours[i].sign && enemy[j].sign) { console.writeline("选出的两个英雄本回合都出战过~~~系统从新选择"); } for (int k = 0; k < ours.count; k++) { if (ours[k].sign == true) { sum++; } } for (int k = 0; k < enemy.count; k++) { if (enemy[k].sign == true) { sum++; } } console.writeline("{0}与{1}对战,{2}剩余血量{3},{4}剩余血量{5}", ours[i].name, enemy[j].name, ours[i].name, ours[i].hp, enemy[j].name, enemy[j].hp); #region 如果已经战死就移除 if (ours[i].hp == 0) { console.writeline("{0}死亡将会从新选择英雄", ours[i].name); ours.remove(ours[i]); } if (enemy[j].hp == 0) { console.writeline("{0}死亡将会从新选择英雄", enemy[j].name); enemy.remove(enemy[j]); } #endregion } console.writeline("第{0}回合结束!!!!!!!!", round); //一回合结束后更新所有英雄的状态 update_status(); if (ours.count == 0) { break; } if (enemy.count == 0) { break; } //更新回合数 round++; } if (ours.count == 0) { console.writeline("我方输"); } else if (enemy.count == 0) { console.writeline("敌方输"); } else if (enemy.count == 0 && ours.count == 0) { console.writeline("平局"); } } //更新状态 public void update_status() { int i; for (i = 0; i < ours.count; i++) { ours[i].dizzy = false; ours[i].sign = false; //开局给的buff最多支撑三回合 if (round == 3) { ours[i].bluebuff = false; ours[i].redbuff = false; } } for (i = 0; i < enemy.count; i++) { enemy[i].dizzy = false; enemy[i].sign = false; //开局给的buff最多支撑三回合 if (round == 3) { enemy[i].bluebuff = false; enemy[i].redbuff = false; } } } //ours进攻 public void ours_attack(int i, int j) { //红buff if (ours[i].redbuff) { console.writeline("红buff对{0}造成10点伤害", enemy[j].name); //红buff造成的伤害,和回血 //防止血量出现负数 enemy[j].hp = enemy[j].hp > 10 ? enemy[j].hp - 10 : 0; if (ours[i].hp == 200) { console.writeline("{0}血量已满无法回血", ours[i].name); } else { //防止血量超过200 ours[i].hp = 200 > (ours[i].hp + 4) ? ours[i].hp + 4 : 200; console.writeline("{0}回了4点血", ours[i].name); } } //蓝buff if (ours[i].bluebuff) { console.writeline("{0}回复了10点魔法值", ours[i].name); //最高值限定 ours[i].mp = ours[i].mp + 10 > 100 ? 100 : ours[i].mp + 10; } //魔法攻击力加物理攻击力造成伤害-最高值限定 if (ours[i].mp >= ours[i].magicattack) { enemy[j].hp = (ours[i].physicsattack + ours[i].magicattack) > enemy[j].hp ? 0 : enemy[j].hp - (ours[i].physicsattack + ours[i].magicattack); ours[i].mp -= ours[i].magicattack; //更新魔法值 } else//魔法值不足以发动魔法攻击 { enemy[j].hp = ours[i].physicsattack > enemy[j].hp ? 0 : enemy[j].hp - ours[i].physicsattack; } } //enemy进攻 public void enemy_attack(int i, int j) { //红buff if (enemy[j].redbuff) { console.writeline("红buff对{0}造成10点伤害", ours[i].name); //红buff造成的伤害 //防止血量出现负数 ours[i].hp = ours[i].hp > 10 ? ours[i].hp - 10 : 0; if (enemy[j].hp == 200) { console.writeline("{0}血量已满无法回血", enemy[j].name); } else { //防止血量超过200 enemy[j].hp = 200 < (enemy[j].hp + 4) ? enemy[j].hp + 4 : 200; console.writeline("{0}回了4点血", enemy[j].name); } } //蓝buff if (enemy[j].bluebuff) { console.writeline("{0}回复了10点魔法值", enemy[j].name); //最高值限定 enemy[j].mp = enemy[j].mp + 10 > 100 ? 100 : enemy[j].mp + 10; } //魔法攻击力加物理攻击力造成伤害-最高值限定 if (enemy[j].mp >= enemy[j].magicattack) { ours[i].hp = (enemy[j].physicsattack + enemy[j].magicattack) > ours[i].hp ? 0 : ours[i].hp - (enemy[j].physicsattack + enemy[j].magicattack); enemy[j].mp -= enemy[j].magicattack; //更新魔法值 } else//魔法值不足以发动魔法攻击 { ours[i].hp = enemy[j].physicsattack > ours[i].hp ? 0 : ours[i].hp - enemy[j].physicsattack; } } } }
百度网盘链接
下载地址 提取码:ycq9
以上就是基于c#模拟实现回合制游戏的详细内容,更多关于c#模拟回合制游戏的资料请关注其它相关文章!