封装练习 对战游戏

Posted ༺҉戰҉༻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装练习 对战游戏相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 对战游戏
{
    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();

            //战士,名字,生命值,攻击力,招式

            Soldier s1 = new Soldier();
            Console.Write("请输入第一个战士的姓名:");
            s1.Name = Console.ReadLine();
            s1.Gongji = r.Next(50, 101);
            s1.Blood = r.Next(500, 1001);
            zhaoshi z1 = new zhaoshi();
            Console.Write("请输入小招名称:");
            z1.Name = Console.ReadLine();
            z1.Gongji = r.Next(100, 201);
            s1.Xiaozhao = z1;
            zhaoshi z2 = new zhaoshi();
            Console.Write("请输入大招名称:");
            z2.Name = Console.ReadLine();
            z2.Gongji = r.Next(300, 501);
            s1.Dazhao = z2;
            s1.Miss = r.Next(0, 40);
            //装备
            equipment e1 = new equipment();
            Console.Write("请输入武器的名称:");
            e1.Name = Console.ReadLine();
            e1.Gongji = r.Next(50, 100);
            s1.Arms = e1;
            equipment e2 = new equipment();
            Console.Write("请输入防装的名称:");
            e2.Name = Console.ReadLine();
            e2.Gongji = r.Next(-50, 0);
            s1.Fangyu= e2;


            Soldier s2 = new Soldier();
            Console.Write("请输入第二个战士的姓名:");
            s2.Name = Console.ReadLine();
            s2.Gongji = r.Next(50, 101);
            s2.Blood = r.Next(500, 1001);
            zhaoshi z3 = new zhaoshi();
            Console.Write("请输入小招名称:");
            z3.Name = Console.ReadLine();
            z3.Gongji = r.Next(100, 201);
            s2.Xiaozhao = z3;
            zhaoshi z4 = new zhaoshi();
            Console.Write("请输入大招名称:");
            z4.Name = Console.ReadLine();
            z4.Gongji = r.Next(300, 501);
            s2.Dazhao = z4;
            s2.Miss = r.Next(0, 40);

            equipment e3 = new equipment();
            Console.Write("请输入武器的名称:");
            e3.Name = Console.ReadLine();
            e3.Gongji = r.Next(50, 100);
            s2.Arms = e3;
            equipment e4 = new equipment();
            Console.Write("请输入防装的名称:");
            e4.Name = Console.ReadLine();
            e4.Gongji = r.Next(-50, 0);
            s2.Fangyu = e4;
            Console.WriteLine("======================战士信息展示=======================");
            Console.WriteLine("战士1:" + s1.Name + ",攻击:" + s1.Gongji + ",闪避:" + s1.Miss + ",血量:" + s1.Blood + ",小招:" + s1.Xiaozhao.Name + "" + s1.Xiaozhao.Gongji + ",大招:" + s1.Dazhao.Name + "" + s1.Dazhao.Gongji + ",武器:" + s1.Arms.Name + "" + s1.Arms.Gongji + ",防装:" + s1.Fangyu.Name + "" + Math.Abs(s1.Fangyu.Gongji));
            Console.WriteLine("战士2:" + s2.Name + ",攻击:" + s2.Gongji + ",闪避:" + s2.Miss + ",血量:" + s2.Blood + ",小招:" + s2.Xiaozhao.Name + "" + s2.Xiaozhao.Gongji + ",大招:" + s2.Dazhao.Name + "" + s2.Dazhao.Gongji + ",武器:" + s2.Arms.Name + "" + s2.Arms.Gongji + ",防装:" + s2.Fangyu.Name + "" + Math.Abs(s2.Fangyu.Gongji));
            Console.WriteLine("按下任意键开始战斗!!!");
            Console.ReadKey();

            //循环战斗,战个痛快(不死不休)

            int count = 1;
            while (true)
            {
                Console.WriteLine("==========================第" + count + "回合==========================");
                System.Threading.Thread.Sleep(1500);

                if (r.Next(0, 101) < s2.Miss)
                {
                    Console.WriteLine(s2.Name + "躲避了此次攻击!!!");
                }
                else
                {
                    int ss1 = r.Next(0, 100);
                    int ss2 = r.Next(0, 100);

                    if (ss1 > 95)//开大招
                    {
                        Console.ForegroundColor = ConsoleColor.Red;

                        Console.WriteLine("" + s1.Name + "】对【" + s2.Name + "】释放了☆☆☆☆☆" + s1.Dazhao.Name + "☆☆☆☆☆,造成" + s1.Dazhao.Gongji + "点伤害,【" + s2.Name + "】剩余" + (s2.Blood - s1.Dazhao.Gongji) + "点血量");
                        Console.WriteLine();
                        //S2扣血
                        s2.Blood -= s1.Dazhao.Gongji;
                    }
                    else if (ss1 >= 80 && ss1 <= 95)//开小招
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;

                        Console.WriteLine("" + s1.Name + "】对【" + s2.Name + "】释放了☆☆" + s1.Xiaozhao.Name + "☆☆,造成" + s1.Xiaozhao.Gongji + "点伤害,【" + s2.Name + "】剩余" + (s2.Blood - s1.Xiaozhao.Gongji) + "点血量");
                        Console.WriteLine();
                        //S2扣血
                        s2.Blood -= s1.Xiaozhao.Gongji;
                    }
                    else//普通攻击
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        if (ss1 >= 60 && ss1 <= 75)
                        {
                            if (ss2 > 40)
                            {
                                Console.WriteLine("" + s1.Name + "】触发☆" + s1.Arms.Name + "☆,不幸的是【" + s2.Name + "】也触发了☆" + s2.Fangyu.Name + "☆只对【" + s2.Name + "】,造成" + (s1.Gongji + s1.Arms.Gongji + s2.Fangyu.Gongji) + "点伤害,【" + s2.Name + "】剩余" + (s2.Blood - (s1.Gongji + s1.Arms.Gongji + s2.Fangyu.Gongji)) + "点血量");
                                Console.WriteLine();
                                //S2扣血
                                s2.Blood = s2.Blood - (s1.Gongji + s1.Arms.Gongji + s2.Fangyu.Gongji);
                            }
                            else
                            {
                                Console.WriteLine("" + s1.Name + "】触发【"+s1.Arms.Name+"】对【" + s2.Name + "】,造成" + (s1.Gongji+s1.Arms.Gongji) + "点伤害,【" + s2.Name + "】剩余" + (s2.Blood - s1.Gongji-s1.Arms.Gongji) + "点血量");
                                Console.WriteLine();
                                //S2扣血
                                s2.Blood=s2.Blood-s1.Gongji-s1.Arms.Gongji;
                            }
                        }
                        else
                        {
                            Console.WriteLine("" + s1.Name + "】攻击了【" + s2.Name + "】,造成" + s1.Gongji + "点伤害,【" + s2.Name + "】剩余" + (s2.Blood - s1.Gongji) + "点血量");
                            Console.WriteLine();
                            //S2扣血
                            s2.Blood -= s1.Gongji;
                        }
                    }

                    //判断战士2是否阵亡
                    if (s2.Blood <= 0)
                    {
                        Console.WriteLine("" + s2.Name + "】已阵亡!【" + s1.Name + "】是获胜者!!!");
                        break;
                    }
                }
                //战士2开始攻击

                System.Threading.Thread.Sleep(1500);
                if (r.Next(0, 101) < s1.Miss)
                {
                    Console.WriteLine(s1.Name + "躲避了此次攻击!!!");
                }
                else
                {
                    int ss3 = r.Next(0, 100);
                    int ss4 = r.Next(0, 100);

                    if (ss3 > 95)//开大招
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("" + s2.Name + "】对【" + s1.Name + "】释放了☆☆☆☆☆" + s2.Dazhao.Name + "☆☆☆☆☆,造成" + s2.Dazhao.Gongji + "点伤害,【" + s1.Name + "】剩余" + (s1.Blood - s2.Dazhao.Gongji) + "点血量");
                        Console.WriteLine();
                        //S1扣血
                        s1.Blood -= s2.Dazhao.Gongji;
                    }
                    else if (ss3 >= 80 && ss3 <= 95)//开小招
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("" + s2.Name + "】对【" + s1.Name + "】释放了☆☆" + s2.Xiaozhao.Name + "☆☆,造成" + s2.Xiaozhao.Gongji + "点伤害,【" + s1.Name + "】剩余" + (s1.Blood - s2.Xiaozhao.Gongji) + "点血量");
                        Console.WriteLine();
                        //S1扣血
                        s1.Blood -= s2.Xiaozhao.Gongji;
                    }
                    else//普通攻击
                    {
                        if (ss3 >= 60 && ss3 <= 75)
                        {
                            if (ss4 > 40)
                            {
                                Console.WriteLine("" + s2.Name + "】触发☆" + s2.Arms.Name + "☆,不幸的是【" + s1.Name + "】也触发了☆" + s1.Fangyu.Name + "☆只对【" + s1.Name + "】,造成" + (s2.Gongji + s2.Arms.Gongji + s1.Fangyu.Gongji) + "点伤害,【" + s1.Name + "】剩余" + (s1.Blood - (s2.Gongji + s2.Arms.Gongji + s1.Fangyu.Gongji)) + "点血量");
                                Console.WriteLine();
                                //S1扣血
                                s1.Blood = s1.Blood - (s2.Gongji + s2.Arms.Gongji + s1.Fangyu.Gongji);
                            }
                            else
                            {
                                Console.WriteLine("" + s2.Name + "】触发【" + s2.Arms.Name + "】对【" + s1.Name + "】,造成" + (s2.Gongji + s2.Arms.Gongji) + "点伤害,【" + s1.Name + "】剩余" + (s1.Blood - s2.Gongji - s2.Arms.Gongji) + "点血量");
                                Console.WriteLine();
                                //S1扣血
                                s1.Blood = s1.Blood - s2.Gongji - s2.Arms.Gongji;
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.WriteLine("" + s2.Name + "】攻击了【" + s1.Name + "】,造成" + s2.Gongji + "点伤害,【" + s1.Name + "】剩余" + (s1.Blood - s2.Gongji) + "点血量");
                            Console.WriteLine();
                            //S1扣血
                            s1.Blood -= s2.Gongji;
                        }
                    }

                    //判断战士2是否阵亡
                    if (s1.Blood <= 0)
                    {
                        Console.WriteLine("" + s1.Name + "】已阵亡!【" + s2.Name + "】是获胜者!!!");
                        break;
                    }
                }

                count++;
            }

            Console.ReadKey();
        }
    }
}
View Code

装备

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 对战游戏
{
    class equipment
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private int _gongji;

        public int Gongji
        {
            get { return _gongji; }
            set { _gongji = value; }
        }
    }
}

人物

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 对战游戏
{
    public class Soldier
    {
        //名字,生命值,攻击力,招式

        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private int _Blood;

        public int Blood
        {
            get { return _Blood; }
            set { _Blood = value; }
        }

        private int _gongji;

        public int Gongji
        {
            get { return _gongji; }
            set { _gongji = value; }
        }

        private zhaoshi xiaozhao;

        public zhaoshi Xiaozhao
        {
            get { return xiaozhao; }
            set { xiaozhao = value; }
        }


        private zhaoshi dazhao;

        public zhaoshi Dazhao
        {
            get { return dazhao; }
            set { dazhao = value; }
        }

        private int _Miss;

        public int Miss
        {
            get { return _Miss; }
            set { _Miss = value; }
        }
        private equipment _fangyu;

        internal equipment Fangyu
        {
            get { return _fangyu; }
            set { _fangyu = value; }
        }


        private equipment _arms;

        internal equipment Arms
        {
            get { return _arms; }
            set { _arms = value; }
        }

      
        



    }
}
View Code

技能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 对战游戏
{
    public class zhaoshi
    {
        private string _Name;

        /// <summary>
        /// 招式名称
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private int _gongji;

        /// <summary>
        /// 招式攻击
        /// </summary>
        public int Gongji
        {
            get { return _gongji; }
            set { _gongji = value; }
        }


    }
}
View Code

 

以上是关于封装练习 对战游戏的主要内容,如果未能解决你的问题,请参考以下文章

练习——对战游戏

[新星计划] Python面向对象练习 | 回合制对战游戏模拟ATMLinux用户管理类

[新星计划] Python面向对象练习 | 回合制对战游戏模拟ATMLinux用户管理类

[新星计划] Python面向对象练习 | 回合制对战游戏模拟ATMLinux用户管理类

《C#零基础入门之百识百例》(六十五)类和结构体的对比 -- 文字对战游戏

对战小游戏