基于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
栏 目:.NET代码
下一篇:C#中的WebRequest与WebResponse抽象类、DNS静态类、Ping类介绍
本文标题:基于C#模拟实现回合制游戏
本文地址:http://www.codeinn.net/misctech/215941.html