码迷,mamicode.com
首页 > 其他好文 > 详细

对战游戏自己简单版+大神版

时间:2015-04-19 17:29:43      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

自己简单版:

namespace ConsoleApplication25
{
    
    class Program
    {
        struct player
        {
            public string name;
            public int hp,atk,def;
            public wugong wg;
        }
        struct wugong
        {
            public string name;
            public int gongji;
        }
        static void Main(string[] args)
        {
            Console.Write("请输入玩家P1名字:");
            string x=Console.ReadLine();
            Console.Write("请输入玩家P2名字:");
            string y = Console.ReadLine();
            player p1 = new player();
            Random r = new Random();
            p1.name = x;
            p1.hp = r.Next(1,100)+1000;
            p1.atk = r.Next(1,50)+100;
            p1.def = r.Next(1, 20)+50;
            player p2 = new player();
            p2.name = y;
            p2.hp = r.Next(1, 100) + 1000;
            p2.atk = r.Next(1, 50)+100;
            p2.def = r.Next(1, 20)+50;
            Console.WriteLine(p1.name + "的血量为:" + p1.hp + "\t攻击力为:" + p1.atk + "\t防御为:" + p1.def + "\n" + p2.name + "的血量为:" + p2.hp + "\t攻击力为:" + p2.atk + "\t防御为:" + p2.def);
            Console.WriteLine("请按Enter键开始攻击!");
            Console.ReadLine();

            int hp1=p1.hp, hp2=p2.hp,m,n;
            while (true)
            {
                m = p2.atk + r.Next(20, 50) - (p1.def / 2);
                hp1 = hp1 - m;
                n = p1.atk + r.Next(20, 50) - (p1.def / 2);
                hp2 = hp2 - n;
                if (hp1<0)
                {
                    hp1 = 0;
                }
                if (hp2 < 0)
                {
                    hp2 = 0;
                }
                Console.WriteLine(p1.name + "受到了" + p2.name + "" + m + "点攻击," + "剩余血量为" + hp1 + "\t" + p2.name + "受到了" + p1.name + "" + n + "点攻击," + "剩余血量为" + hp2);
                
                if (hp1<=0&&hp2<=0)
                {
                    Console.WriteLine(p1.name+""+p2.name+"同归于尽!");
                    break;
                }
                if (hp1<=0)
                {
                    Console.WriteLine(p2.name + "获胜!");
                    break;
                }
                if (hp2 <= 0)
                {
                    Console.WriteLine(p1.name + "获胜!");
                    break;
                }

            }
            Console.ReadLine();
        }
    }
}


大神版:
namespace ConsoleApplication5
{





    class Program
    {
        struct player
        {
            public string name;
            public int hp;
            public int attack;
            public int defend;
            public int quick;
            public WuGong WG;
        }
        struct WuGong
        {
            public string name;
            public int atk;
        }

        static void Main(string[] args)
        {

            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();
            player p1 = new player();
            player p2 = new player();
            Random r = new Random();

            //生成两个玩家
            Console.Write("请输入第一个战士的名字:");
            p1.name = Console.ReadLine();
            Console.Write("请输入第二个战士的名字:");
            p2.name = Console.ReadLine();
            //生成血量
            p1.hp = r.Next(1, 1000) + 1000;//初始血量
            p2.hp = r.Next(1, 1000) + 1000;
            //生成攻防
            p1.attack = r.Next(1, 50) + 80;
            p2.attack = r.Next(1, 50) + 80;
            p1.defend = r.Next(1, 50) + 50;
            p2.defend = r.Next(1, 50) + 50;
            //生成身法-敏捷度
            p1.quick = r.Next(100);
            p2.quick = r.Next(100);

            Console.WriteLine("玩家:" + p1.name + " 血量:" + p1.hp + "\t攻击力:" + p1.attack
             + "\t物防:" + p1.defend + " 敏捷度:" + p1.quick);
            Console.WriteLine("VS");
            Console.WriteLine("玩家:" + p2.name + " 血量:" + p2.hp + "\t攻击力:" + p2.attack
            + "\t物防:" + p2.defend + " 敏捷度:" + p2.quick);
            Console.WriteLine("点击任意键开始游戏.....");
            Console.ReadKey();

            while (true)
            {
                // 得出最后结果,跳出循环
                if (p1.hp <= 0 && p2.hp <= 0)
                {
                    Console.WriteLine(p1.name + "" + p2.name + "同归于尽!");
                    Console.ReadLine();
                    break;
                }
                if (p1.hp <= 0)
                {
                    Console.WriteLine(p2.name + "" + p1.name + "KO..");
                    Console.ReadLine();
                    break;
                }
                if (p2.hp <= 0)
                {
                    Console.WriteLine(p1.name + "" + p2.name + "KO..");
                    Console.ReadLine();
                    break;
                }

                //对战

                WuGong[] wg = new WuGong[3];
                //gongfu(wg);
                wg[0].name = "降龙十八掌";
                wg[0].atk = 500;

                wg[1].name = "打狗棒法";
                wg[1].atk = 600;

                wg[2].name = "独孤九剑";
                wg[2].atk = 600;
                //大招
                int dz1 = r.Next(10);//大招随机
                if (dz1 >= 5) //从1到9的随机数里 取8和9 有20%的几率用大招。
                {
                    Random x = new Random();
                    int a = x.Next(0, 3);
                    int dx1 = r.Next(100);//声明 掉血 变量
                    int gj1 = r.Next(100) - 50;//为了浮动 P2的 攻击力;
                    int fy1 = r.Next(100) - 50;//为了浮动 P1的防御力;
                    dx1 = ((dx1 + wg[a].atk) - (p1.defend + fy1)) < 0 ? 0 : ((dx1 + wg[a].atk) - (p1.defend + fy1));
                    //为了防止掉血 出现负值
                    p1.hp -= dx1;
                    if (p1.hp < 0)
                    {
                        p1.hp = 0;
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(p2.name + "使用大招 " + wg[a].name + " 攻击,对" + p1.name + "造成" + dx1 + "的伤害。");

                }
                else //平常招式
                {
                    //躲闪
                    int sf1 = r.Next(80);//身法的随机数

                    if (p1.quick - sf1 >= 0)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.WriteLine(p1.name + "躲闪" + p2.name + "的攻击");
                    }
                    else
                    {
                        int dx1 = r.Next(100);//声明 掉血 变量
                        int gj1 = r.Next(100) - 50;//为了浮动 P2的 攻击力;
                        int fy1 = r.Next(100) - 50;//为了浮动 P1的防御力;
                        dx1 = (dx1 + (p2.attack + gj1) - (p1.defend + fy1)) < 0 ? 0 : (dx1 + (p2.attack + gj1) - (p1.defend + fy1));
                        //为了防止掉血 出现负值
                        p1.hp -= dx1;
                        if (p1.hp < 0)
                        {
                            p1.hp = 0;
                        }
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(p1.name + "使用普通攻击,对" + p2.name + "造成" + dx1 + "的伤害。");
                    }
                }
                //稍等一下
                System.Threading.Thread.Sleep(1000);

                int dz2 = r.Next(10);
                if (dz2 >= 5)
                {
                    Random y = new Random();
                    int b = y.Next(0, 3);
                    int dx2 = r.Next(100);
                    int gj2 = r.Next(100) - 50;//为了浮动 P1的 攻击力;
                    int fy2 = r.Next(100) - 50;//为了浮动 P2的防御力;
                    dx2 = ((dx2 + wg[b].atk) - (p2.defend + fy2)) < 0 ? 0 : ((dx2 + wg[b].atk) - (p2.defend + fy2));
                    //为了防止掉血 出现负值
                    p2.hp -= dx2;
                    if (p2.hp < 0)
                    {
                        p2.hp = 0;
                    }
                    Console.ForegroundColor = ConsoleColor.DarkMagenta;
                    Console.WriteLine(p1.name + "使用大招 " + wg[b].name + " 攻击,对" + p2.name + "造成" + dx2 + "的伤害。");
                }
                else
                {
                    //躲闪
                    int sf2 = r.Next(80);
                    if (p2.quick - sf2 > sf2)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.WriteLine(p2.name + "躲闪" + p1.name + "的攻击");
                    }
                    else
                    {
                        int dx2 = r.Next(100);
                        int gj2 = r.Next(100) - 50;//为了浮动 P1的 攻击力;
                        int fy2 = r.Next(100) - 50;//为了浮动 P2的防御力;
                        dx2 = (dx2 + (p1.attack + gj2) - (p2.defend + fy2)) < 0 ? 0 : (dx2 + (p1.attack + gj2) - (p2.defend + fy2));
                        //为了防止掉血 出现负值
                        p2.hp -= dx2;
                        if (p2.hp < 0)
                        {
                            p2.hp = 0;
                        }
                        Console.ForegroundColor = ConsoleColor.DarkMagenta;
                        Console.WriteLine(p2.name + "使用普通攻击,对" + p1.name + "造成" + dx2 + "的伤害。");
                    }
                }
                //稍等一下
                System.Threading.Thread.Sleep(1000);
                Console.ForegroundColor = ConsoleColor.DarkBlue;
                Console.WriteLine("玩家:" + p1.name + "\t血量:" + p1.hp + "\t");
                Console.WriteLine("玩家:" + p2.name + "\t血量:" + p2.hp);


            }
        }

    }

}

 

 

 

对战游戏自己简单版+大神版

标签:

原文地址:http://www.cnblogs.com/dlexia/p/4439421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!