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 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葬月飘零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值