C# 入门第十课面向对象的文字游戏

面向对象的文字游戏

Players.cs

using System;


namespace _7._21
{
    class Player
    {
        //名称
        public string name = Game.givename;
        //public string Name { get; }
        //最大血量
        public int maxHp = Tools.Random(90, 110);
        //成长血量
        public int g_Hp = Tools.Random(90, 110);
        //当前血量
        private int currentHp = 0;
        //属性
        public int CurrentHp
        {
            set
            {
                currentHp = value < 0 ? 0 : value ;
                currentHp = value > maxHp ? maxHp : value;
            }
            get
            {
                return currentHp;
            }
        }
        //攻击力
        public int atk = Tools.Random(10, 15);
        //成长攻击力
        public int g_atk = Tools.Random(10, 15);
        //速度
        public int speed = Tools.Random(10, 15);
        //成长速度
        public int g_speed = Tools.Random(10, 15);
        //闪避
        public int miss = Tools.Random(5, 10);
        //暴击
        public int satk = Tools.Random(5, 10);
        //经验
        public int exp = 0;
        //等级  n*exp
        public int level = 1;
        //钱
        public int money = 0;

        public Player()
        {
            CurrentHp = maxHp;
            Debug.Log("角色创建成功!", ConsoleColor.Blue);
        }
        //显示信息
        public void ShowInfo()
        {
            string str = string.Format("{0}当前属性:", name);
            Debug.Log(str, ConsoleColor.Green);
            str = string.Format("血量:{0}/{1},攻击:{2},速度:{3},暴击:{4},闪避:{5}", currentHp, maxHp, atk, speed, satk, miss);
            Debug.Log(str, ConsoleColor.Green);
            str = string.Format("等级:{0},经验:{1}/{2},金钱:{3}", level, exp, level * 100, money);
            Debug.Log(str, ConsoleColor.Green);
        }


        //增加经验
        public bool AddExp(int exp)
        {
            this.exp += exp;
            bool isLevelUp = false;
            //判断升级
            while(this.exp>=100*level)
            {
                this.exp -= 100 * level;
                level++;
                //增加属性
                maxHp += g_Hp;
                atk += g_atk;
                speed += g_speed;
                //血量回满
                currentHp = maxHp;
                //打印
                Debug.Log("恭喜等级提升为" + level + "级!", ConsoleColor.Yellow, 0);
                ShowInfo();
                isLevelUp = true;
            }
            return isLevelUp;
        }

        //增加金钱
        public void AddMoney(int money)
        {
            this.money += money;
        }

        //减少金钱
        public bool SubMoney(int money)
        {
            if(this.money >=money)
            {
                this.money -= money;
                return true;
            }
            return false;
        }

        public void AddAtk(int atk)
        {
            this.atk += atk;
        }



        //攻击
        public bool Attack(Enemy enemy)
        {
            return enemy.GetAttack(atk);
        }

        //受到攻击
        public bool GetAttack(Enemy enemy)
        {
            //判断是否命中
            if(Tools.Random(0, 100) < miss)
            {
                Debug.Log("你躲过了" + enemy.name + "的攻击");
                return false;
            }
            //判断是否暴击
            int num = enemy.atk;
            if(Tools.Random(0, 100) < enemy.satk)
            {
                num = enemy.atk * 2;
            }
            CurrentHp -= num;
            Debug.Log("你受到" + enemy.name + num+"点伤害。");
            if (CurrentHp <= 0)
            {
                //死亡
                Debug.Log("你被" + enemy.name + "无情的杀死了!");
                return true;
            }
            return false;
        }
    }
}

Enemy.cs

namespace _7._21
{
    class Enemy
    {
        public int id = 0;
        public string name = "I型崩坏兽";
        public int hp = 30;
        public int atk = 20;
        public int speed = 10;
        public int satk = 5;
        public int miss = 5;
        public int exp = 50;
        public int money = 5;

        //攻击
        public bool Attack()
        {
            return Game.player.GetAttack(this);
        }

        //受到攻击
        public bool GetAttack(int damage)
        {
            if(Tools .Random(0, 100) < miss)
            {
                Debug.Log(name + "躲过了你的攻击");
                return false;
            }
            if(Tools.Random(0, 100) < Game.player.satk)
            {
                damage *= 2;
            }
            hp -= damage;
            Debug.Log("你对" + name + "造成了" + damage + "点伤害");
            bool res = false;
            if (hp <= 0)
            {
                res = true;
                Debug.Log("你击败了" + name + "!经验+" + exp + ",金钱+" + money);
                Game.player.AddExp(exp);
                Game.player.AddMoney(money);
            }
            return res;
        }
    }
}

Tools.cs

using System;

namespace _7._21
{
    class Tools
    {
        public static int Random(int min,int max)
        {
            return new Random().Next(min, max + 1);
        }
    }
}

Debug.cs

using System;

using System.Threading;

namespace _7._21
{
    //输出调试类
    class Debug
    {
        //输出
        public static void Log(string text)
        {
            Console.WriteLine(text);
            Thread.Sleep(1000);
        }

        public static void Log(int a)
        {
            Console.WriteLine(a);
            Thread.Sleep(1000);
        }

        public static void Log()
        {
            for (int a = 0; a < 6; a++)
            {
                Console.Write("·");
                Thread.Sleep(1000);
            }
            Console.WriteLine();
        }

        //颜色输出
        public static void Log(string text,ConsoleColor color)
        {
            Console.ForegroundColor = color;
            Console.WriteLine(text);
            Thread.Sleep(1000);
            Console.ForegroundColor = ConsoleColor.White;
        }

        //输出
        public static void Log(string text,int time)
        {
            Console.WriteLine(text);
            Thread.Sleep(time);
        }

        //颜色输出
        public static void Log(string text, ConsoleColor color, int time)
        {
            Console.ForegroundColor = color;
            Console.WriteLine(text);
            Thread.Sleep(time);
            Console.ForegroundColor = ConsoleColor.White;
        }
    }
}

program.cs

namespace _7._21
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Game
            Game game = new Game();
            //运行游戏
            game.Run();
        }
    }
}

Game.cs

using System;


namespace _7._21
{
    class Game
    {
        //创建玩家
        public static Player player;
        public static string givename;
        public void Start()
        {
            while (true)
            {
                //创建玩家
                Debug.Log("输入你的角色昵称:", ConsoleColor.Blue);
                givename = Console.ReadLine();
                player = new Player();
                //执行评分内容
                PingFen pingfen = new PingFen();
                pingfen.Run();
                Debug.Log("1:开始游戏,2:重新创建");
                string str = Console.ReadLine();
                if (str == "1")
                {
                    break;
                }
            }
            Debug.Log("************************************", ConsoleColor.Cyan);
        }


        //运行游戏
        public void Run()
        {
            //创建角色
            Start();
            //执行研究室内容
            YanJiuShi yjs = new YanJiuShi();
            yjs.Run();
            //执行迷宫内容
            MiGong migong = new MiGong();
            migong.Run();
            migong.Run2();
            //执行花海内容
            FlowerSea flowersea = new FlowerSea();
            flowersea.enemyStep = 5;
            flowersea.Run();
        }
    }
}

PingFen.cs

namespace _7._21
{
    
    class PingFen
    {
        
        public void Run()
        {
            int num1 = Game.player.maxHp + Game.player.atk + Game.player.speed + Game.player.satk + Game.player.miss;
            int num2 = Game.player.g_Hp + Game.player.g_atk + Game.player.g_speed;
            Debug.Log("您的角色初始属性评分为:"+num1+"(初始范围:120-160)");
            Debug.Log("您的角色初始天赋评分为:" + num2+"(初始范围:110-140)");
        }
    }
}

YanJiuShi.cs

using System;

namespace _7._21
{
    class YanJiuShi
    {
        public static NPC npc = new NPC();


        //研究室
        public void Run()
        {
            Debug.Log("哈…………");
            Debug.Log("这一觉睡得可真舒服呀,咦,我这是在哪儿?");
            Debug.Log();
            Debug.Log("这里好像是一个研究室。");
            while (true)
            {
                
                Debug.Log("1:查看你的属性,2:导航精灵派蒙,3:走出研究室");
                string s = Console.ReadLine();
                if (s == "1")
                {
                    Game.player.ShowInfo();
                }
                else if (s == "2")
                {
                    npc.Chat();
                }
                else
                {
                    break;
                }
            }
        }
    }
}

NPC.cs

namespace _7._21
{
    class NPC
    {
        public string name = "派蒙";

        //是否对话过
        public bool isChat = false;

        //对话
        public void Chat()
        {
            if (isChat == false)
            {
                isChat = true;
                Debug.Log("“你就是总部新派来的实习生吗?这里很危险,不要乱走动。”");
                Debug.Log("导航精灵派蒙说道:“我的名字叫做派蒙,你初来乍到,这是20金币,你先拿着。”");
                //给钱
                Debug.Log("你获得了20金币");
                Game.player.AddMoney(20);
                Game.player.ShowInfo();
            }
            else
            {
                Debug.Log("派蒙:‘你又来了呀,我没有钱了哦。’");
            }
            

        }
    }
}

MiGong.cs

using System;


namespace _7._21
{
    class MiGong
    {
        public void Run()
        {
            Debug.Log("这的路怎么这么复杂呀,看来只能试试运气了。");
            int x = 1, y = 1;//记录小人初始位置
            int[,] map = new int[20, 20]
            {
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                {1,2,1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1},
                {1,0,1,0,1,1,0,0,0,1,0,0,0,0,0,1,0,1,0,1},
                {1,0,1,0,1,0,0,1,0,1,0,1,1,0,1,0,0,0,0,1},
                {1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,1,1,0,1},
                {1,0,1,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,1},
                {1,0,0,0,1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,1},
                {1,1,1,1,1,0,1,0,1,0,1,1,0,1,0,0,0,0,1,1},
                {1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,1,0,0,1},
                {1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,1,1,1,0,1},
                {1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,1},
                {1,0,1,0,1,1,1,1,1,1,0,0,0,1,1,0,1,1,0,1},
                {1,0,0,1,0,0,0,1,0,0,1,1,0,1,0,0,0,0,0,1},
                {1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,0,1,1},
                {1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,1},
                {1,0,1,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1},
                {1,0,0,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1},
                {1,0,1,1,0,0,0,0,0,0,1,1,0,1,0,0,1,1,1,1},
                {1,0,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,3,1},
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
            };

            //打印初始图
            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    if (map[i, j] == 0)
                    {
                        Console.Write("  ");
                    }
                    else if (map[i, j] == 1)
                    {
                        Console.Write("■");
                    }
                    else if (map[i, j] == 2)
                    {
                        Console.Write("♀");
                    }
                    else if (map[i, j] == 3)
                    {
                        Console.Write("→");
                    }
                }
                Console.WriteLine();
            }

            //在键盘接受指令,对指令分析运算,输出数据
            while (true)//无数次执行循环体
            {

                ConsoleKeyInfo s = Console.ReadKey();//在键盘接受指令
                int t = map[x, y];

                if (s.Key.ToString() == "RightArrow")//若接受指令为“→”,
                {
                    if (map[x, y + 1] != 1)
                    {
                        map[x, y] = map[x, y + 1];
                        map[x, y + 1] = t;
                        y++;
                    }
                }
                else if (s.Key.ToString() == "LeftArrow")
                {
                    if (map[x, y - 1] != 1)
                    {
                        map[x, y] = map[x, y - 1];
                        map[x, y - 1] = t;
                        y--;
                    }
                }
                else if (s.Key.ToString() == "UpArrow")
                {
                    if (map[x - 1, y] != 1)
                    {
                        map[x, y] = map[x - 1, y];
                        map[x - 1, y] = t;
                        x--;
                    }
                }
                else if (s.Key.ToString() == "DownArrow")
                {
                    if (map[x + 1, y] != 1)
                    {
                        map[x, y] = map[x + 1, y];
                        map[x + 1, y] = t;
                        x++;
                    }
                }
                Console.Clear();
                for (int i = 0; i < 20; i++)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        if (map[i, j] == 0)
                        {
                            Console.Write("  ");
                        }
                        else if (map[i, j] == 1)
                        {
                            Console.Write("■");
                        }
                        else if (map[i, j] == 2)
                        {
                            Console.Write("♀");
                        }
                        else if (map[i, j] == 3)
                        {
                            Console.Write("→");
                        }
                    }
                    Console.WriteLine();
                }
                if (map[18, 18] == 2)
                {
                    break;
                }
            }
        }

        public void Run2()
        {
            Debug.Log("终于走出迷宫了,哇,这里的景色真的好美!");
            int[,] map = new int[20, 20]
            {
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
            };

            //打印初始图
            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    if (map[i, j] == 0)
                    {
                        Console.Write("★");
                    }
                    else if (map[i, j] == 1)
                    {
                        Console.Write("■");
                    }
                    else if (map[i, j] == 2)
                    {
                        Console.Write("♀");
                    }
                }
                Console.WriteLine();
            }
        }
    }
}

FlowerSea.cs

using System;


namespace _7._21
{
    class FlowerSea
    {
        public static NPC npc = new NPC();
        public int enemyStep;
        public int currentStep;
        public void Run()
        {
            if (enemyStep <= 0)
            {
                while (true)
                {

                    Debug.Log("1:查看你的属性,2:导航精灵派蒙,3:走出花海");
                    string s = Console.ReadLine();
                    if (s == "1")
                    {
                        Game.player.ShowInfo();
                    }
                    else if (s == "2")
                    {
                        npc.Chat();
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                while (true)
                {
                    Debug.Log("按任意键移动",ConsoleColor.Red);
                    Console.ReadKey();
                    currentStep++;
                    if (currentStep >= Tools.Random(enemyStep - 2, enemyStep + 2))
                    {
                        currentStep = 0;
                        //遇敌
                        Debug.Log("你遇到了I型崩坏兽。");
                        Game.player.ShowInfo();
                        Debug.Log("1:消耗5金币回满血量,2:跳过");
                        string str = Console.ReadLine();
                        if (str == "1"&&Game.player.money>=5)
                        {
                            Game.player.money -= 5;
                            Game.player.CurrentHp = Game.player.maxHp;
                            Game.player.ShowInfo();
                        }
                        Enemy enemy = new Enemy();
                        bool isPlayerAtk = Game.player.speed > enemy.speed;
                        while (true)
                        {
                            if (isPlayerAtk)
                            {
                                //如果玩家出手
                                isPlayerAtk = false;
                                bool res = Game.player.Attack(enemy);
                                if (res == true)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                //僵尸出手
                                isPlayerAtk = true;
                                bool res = enemy.Attack();
                                if (res == true)
                                {
                                    Debug.Log("游戏失败!", ConsoleColor.Red);
                                    Environment.Exit(0);
                                }
                            }
                        }
                    }
                }
            } 
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zangyuepiaoling/article/details/107519191