c#对战游戏
namespace 对战游戏
{
class Program
{
public struct ZhanShi
{
public string MingZi;
public int GongJi;
public int XueLiang;
}
static void Main(string[] args)
{
创建战士给战士赋予属性
ZhanShi zs1 = new ZhanShi();
zs1.MingZi = "张无忌";
zs1.GongJi = 100;
zs1.XueLiang = 1000;
ZhanShi zs2 = new ZhanShi();
zs2.MingZi = "洪七公";
zs2.GongJi = 150;
zs2.XueLiang = 700;
//对打
while (true)
{
int ShangHai = Program.PuTongGongJi(zs1.GongJi);
zs2.XueLiang = zs2.XueLiang - ShangHai;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(zs1.MingZi + " 对 " + zs2.MingZi + " 发动了普通攻击,造成了" + ShangHai + "点伤害, " + zs2.MingZi + " 剩余" + zs2.XueLiang + "点血量!");
Console.WriteLine();
System.Threading.Thread.Sleep(1000);
ShangHai = Program.PuTongGongJi(zs2.GongJi);
zs1.XueLiang = zs1.XueLiang - ShangHai;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(zs2.MingZi + " 对 " + zs1.MingZi + " 发动了普通攻击,造成了" + ShangHai + "点伤害, " + zs1.MingZi + " 剩余" + zs1.XueLiang + "点血量!");
Console.WriteLine();
System.Threading.Thread.Sleep(1000);
if (zs1.XueLiang <= 0 || zs2.XueLiang <= 0)
{
if (zs1.XueLiang <= 0 && zs2.XueLiang <= 0)
{
Console.WriteLine("二位同归于尽了!!!");
}
else
{
if (zs1.XueLiang <= 0)
{
Console.WriteLine(zs2.MingZi + "获得了最终胜利!!!");
}
else
{
Console.WriteLine(zs1.MingZi + "获得了最终胜利!!!");
}
}
break;
}
}
//判断输赢
Console.ReadKey();
}
生成浮动攻击力
/// <summary>
/// 生成普通攻击的浮动攻击值
/// </summary>
/// <param name="a">普通攻击的基础攻击力</param>
/// <returns></returns>
public static int PuTongGongJi(int a)
{
int cc = 0;
Random r = new Random();
int aa = r.Next(0, 141);
int bb = r.Next(0, 2);
if (bb == 0)
{
cc = a + aa;
}
else
{
cc = a - aa;
}
return cc;
}
}
}
效果图: