首页 > 代码库 > 1月5日 结构体(对战小游戏)

1月5日 结构体(对战小游戏)

namespace duzhanxiaoyouxi
{
struct RenWu                              //定义 struct  类型
{
public string Name;
public int Blood;                   //这个就是结构体:结构体相当于我们自己定义的一种复杂的类型,用来解决生活中的复杂问题。
public int Attack;
public int Defence;
public int Speed;
public WuGong WG;            //结构体内也可以引用结构体
}
struct WuGong                           //结构体可以定义多个
{
public string Name;
public int Attack;                 //public 公共的      public   变量类型  变量名;
public int Defence;
public int Speed;
}
class Program
{
static void Main(string[] args)
{
//创建
RenWu r1 = new RenWu();                   //构造的类型可以和int、string等一样使用
RenWu r2 = new RenWu();

Console.Write("请输入第一个战士的姓名:");
r1.Name = Console.ReadLine();

Console.Write("请输入第二个战士的姓名:");
r2.Name = Console.ReadLine();

//生成血量
Random rand = new Random();
r1.Blood = rand.Next(1000) + 1000;
r2.Blood = rand.Next(1000) + 1000;

//生成攻防
r1.Attack = rand.Next(100) + 50;
r1.Defence = rand.Next(100) + 50;

r2.Attack = rand.Next(100) + 50;
r2.Defence = rand.Next(100) + 50;

//生成身法
r1.Speed = rand.Next(100);
r2.Speed = rand.Next(100);

Console.WriteLine(r1.Name + "的血量为:" + r1.Blood + "滴\t攻击力为:" + r1.Attack + "\t防御力为:" + r1.Defence);
Console.WriteLine(r2.Name + "的血量为" + r2.Blood + "滴\t攻击力为:" + r2.Attack + "\t防御力为:" + r2.Defence);
Console.WriteLine();
//对战
while (true)
{
//跳出循环的条件
if (r1.Blood <= 0 && r2.Blood <= 0)
{
Console.WriteLine(r1.Name + "和" + r2.Name + "同归于尽了");
break;
}
if (r1.Blood <= 0)
{
Console.WriteLine(r2.Name + "把" + r1.Name + "KO了");
break;
}
if (r2.Blood <= 0)
{
Console.WriteLine(r1.Name + "把" + r2.Name + "KO了");
break;
}

//开始对战
int shenfa = rand.Next(50)+50;

//速度稍慢一些 暂停一秒
System.Threading.Thread.Sleep(1000);


if (r2.Speed > shenfa)
{
Console.WriteLine(r2.Name + "躲过了" + r1.Name + "的攻击");
}
else
{
int shanghai1 = rand.Next(100) + 50;//第一个对第二个的伤害
shanghai1 += r1.Attack - r2.Defence; //第二个人实际掉的血

r2.Blood = (r2.Blood - shanghai1) < 0 ? 0 : (r2.Blood - shanghai1);

//改变文字颜色
Console.ForegroundColor = ConsoleColor.Blue;

Console.Write(r1.Name + "发起攻击," + r2.Name + "掉了" + (shanghai1 < 0 ? 0 : shanghai1) + "滴血\t");
Console.WriteLine();
}

if (r1.Speed > shenfa)
{
Console.WriteLine(r1.Name + "躲过了" + r2.Name + "的攻击");
}
else
{
int shanghai2 = rand.Next(100) + 50;
shanghai2 += r2.Attack - r1.Defence;

r1.Blood = (r1.Blood - shanghai2) < 0 ? 0 : (r1.Blood - shanghai2);

//改变文字颜色
Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine(r2.Name + "发起攻击," + r1.Name + "掉了" + (shanghai2 < 0 ? 0 : shanghai2) + "滴血");
}
//改变文字颜色
Console.ForegroundColor = ConsoleColor.Green;

Console.WriteLine(r1.Name + "的剩余血量为" + r1.Blood + "\t" + r2.Name + "的剩余血量为" + r2.Blood );

Console.WriteLine();
Console.WriteLine();

}
}
}
}

1月5日 结构体(对战小游戏)