首页 > 代码库 > 函数 及对战游戏
函数 及对战游戏
//对战游戏
struct duizhan
{
public string name;
public int xue;
public int gongji;
public int fangyu;
public int mingzhong;
public int shengfa;
}
static void Main(string[] args)
{
duizhan[] a = new duizhan[5];//定义结构体,并建立数组
int i = 0;
for (; i < 5;i++ )//输入
{
Console.Write("请输入第{0}个人的姓名:",i+1);
a[i].name = Console.ReadLine();
int m = (int)Convert.ToChar(a[i].name.Substring(0,1));
Random n = new Random(m);
a[i].xue = 1000+ n.Next(1000);
a[i].gongji = 50 + n.Next(100);
a[i].mingzhong = 50 + n.Next(100);
a[i].fangyu = 30 + n.Next(100);
a[i].shengfa = 30 + n.Next(100);
Console.Write("生命力:"+a[i].xue +"\t攻击力:"+a[i].gongji +"\t命中率:"+a[i].mingzhong +"\t防御力"+a[i].fangyu +"\t身法"+a[i].shengfa +"\n");
}
Console.WriteLine("按任意键开始!");
Console.ReadKey();//按任意键开始
while (true)
{
Random m = new Random();
int p = m.Next(5);
int q = m.Next(5);//随机生成两名选手
if (p != q && a[p].xue > 0 && a[q].xue > 0)
{
Console.WriteLine("\n");
Console.ForegroundColor = ConsoleColor.Yellow ;//修改字体颜色
Console.WriteLine(a[p].name + "准备攻击" + a[q].name);
Thread.Sleep(3000); //延迟3秒
a[p].gongji = m.Next(150) + 50;
a[p].mingzhong = m.Next(150) + 50;
int shashangli = a[p].gongji + a[p].mingzhong;
a[q].fangyu = m.Next(130) + 50;
a[q].shengfa = m.Next(130) + 50;
int dikangli = a[q].fangyu + a[q].shengfa;
Console.Write("\n");
if (shashangli > dikangli)//判断攻击条件
{
int shixue = a[p].gongji - a[q].fangyu;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(a[p].name + "攻击掉" + a[q].name + shixue + "滴血!");
Console.ForegroundColor = ConsoleColor.Green;
a[q].xue = a[q].xue - shixue;
Console.WriteLine(a[q].name + "剩余" + a[q].xue + "滴血!");
}
else
{
Console.ForegroundColor = ConsoleColor.Blue ;
Console.WriteLine(a[q].name + "躲过了" + a[p].name + "的攻击!");
}
}
else if(a[p].xue <=0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(a[p].name +"战败!");
}
else if (a[q].xue <= 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(a[q].name + "战败!");
}
else
{
continue ;
}
}
//函数 ( 名 输入 加工 输出 )高内聚,低耦合 独立 常用 分工 会做 调用
//在class里面加入static int Max(int a,int b)
//{
// if(a>b){return a}else{return b}
//}
//在main里就能利用了
//int x = 3, y = 4;
//Console.WriteLine("最大值是:"+max(x,y));
//int x = 3, y = 4, c = 5;
//int a = max(max(x,y),c);
//Console.WriteLine("最大值是:" + a);
函数 及对战游戏