开发环境:
系统环境:Win10
开发工具:VS2019
语言:C#
项目:控制台应用(.NET Framework)
编码
英雄类:(更换namespace即可)
using System; namespace demos_oop_5 { /// <summary> /// 封装英雄信息的封装 /// </summary> public class Heros { private string id; private string name; private int HP; private int AC; private string Introduce; public Heros() { } /// <summary> /// 用于赋值的构造函数 /// </summary> /// <param name="id"></param> /// <param name="name"></param> /// <param name="hP"></param> /// <param name="aC"></param> /// <param name="introduce"></param> public Heros(string id, string name, int hP, int aC, string introduce) { this.id = id; this.name = name; HP = hP; AC = aC; Introduce = introduce; } public string Id { get => id; set => id = value; } public string Name { get => name; set => name = value; } public int HP1 { get => HP; set => HP = value; } public int AC1 { get => AC; set => AC = value; } public string Introduce1 { get => Introduce; set => Introduce = value; } } }
执行类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace demos_oop_5 { class Program { /// <summary> /// 声明英雄集合变量list /// </summary> static List<Heros> list = new List<Heros>(); static Heros one = new Heros(); static Heros Two = new Heros(); static void Main(string[] args) { //英雄对战小游戏 //*封装*、继承、*构造方法传参*、*while+switch*、*集合、泛型* //随机数,数学函数 Console.WriteLine("**************************"); Console.WriteLine("*****欢迎来到德莱联盟*****"); Console.WriteLine("*****英雄对战操作平台*****"); Console.WriteLine("**************************"); Console.WriteLine("请对以下功能进行选择性操作"); while (true) { Console.WriteLine("**************************"); Console.WriteLine("******1、查看英雄列表*****"); Console.WriteLine("******2、选择对战英雄*****"); Console.WriteLine("******3、开始对战游戏*****"); Console.WriteLine("******4、立即退出游戏*****"); Console.WriteLine("**************************"); string choose = Console.ReadLine(); switch (choose) { case "1": findAll(); break; case "2": peoVSme(); break; case "3": HeroVs(); break; case "4": Console.WriteLine("游戏结束"); return; default: Console.WriteLine("请按照菜单操作"); break; } } } /// <summary> /// 英雄对战方法 /// </summary> private static void HeroVs() { //开战之前一定要重置一下英雄数据,否则血量不足 findAll();//调用就相当于重置 Random ra = new Random(); //one vs two Console.WriteLine("对战英雄:"+one.Name+"\tVS\t"+Two.Name); Console.WriteLine("*************************"); while (true) { //人对机器造成伤害 Console.Write(one.Name+"\t对\t"+Two.Name+"发动攻击造成:"); //生成对战的信息,需要全部显示出来 int toTwo = ra.Next(one.AC1 / 2) + one.AC1 / 2; Two.HP1-= toTwo; Console.WriteLine(toTwo+"点伤害,\t"+Two.Name+"\t剩余血量:"+Two.HP1); if (Two.HP1 <= 0)//机器人是否死亡判断 { Console.WriteLine(one.Name + "胜利"); break; } //机器对人造成伤害 Console.Write(Two.Name + "\t对\t" + one.Name + "发动攻击造成:"); //生成对战的信息,需要全部显示出来 int toOne = ra.Next(Two.AC1 / 2) + Two.AC1 / 2; one.HP1 -= toOne; Console.WriteLine(toOne + "点伤害,\t" + one.Name + "\t剩余血量:" + one.HP1); //判断是否结束循环 if (one.HP1<=0) { Console.WriteLine(Two.Name+"胜利"); break; } } } /// <summary> /// 人机选择 /// </summary> private static void peoVSme() { ///人选 Console.WriteLine("请输入选择英雄角色编号"); string peo = Console.ReadLine(); foreach (var item in list) { if (item.Id.Equals(peo)) { one = item; break; } } //机选 Random ra = new Random(); Two = list[ra.Next(3)]; Console.WriteLine("选择成功"); Console.WriteLine("您选择的英雄是:" + one.Name); Console.WriteLine("电脑选择的英雄是:" + Two.Name); } private static void findAll() { //list集合的初始化操作 list = GetDefaultList(); foreach (var item in list) { Console.Write(item.Id + "\t"); Console.Write(item.Name + "\t"); Console.Write(item.HP1 + "\t"); Console.Write(item.AC1 + "\t"); Console.Write(item.Introduce1 + "\t"); Console.WriteLine(); } } /// <summary> /// 默认的英雄集合 /// </summary> /// <returns></returns> public static List<Heros> GetDefaultList() { List<Heros> heros = new List<Heros>(); heros.Add(new Heros(System.Guid.NewGuid().ToString("N"), "亚瑟", 860, 80, "亚瑟王~审判之剑")); heros.Add(new Heros(System.Guid.NewGuid().ToString("N"), "冰女", 520, 120, "飞箭")); heros.Add(new Heros(System.Guid.NewGuid().ToString("N"), "剑圣", 660, 110, "飞斩")); return heros; } } }
测试效果:
演示完毕。
源码地址:
【C#MUD_英雄大作战.rar-其他文档类资源-CSDN下载】