夜光带你走进C# 游戏开发等(五十六)擅长的领域

夜光序言:

 

情商高的人,总让我觉得不真实,仿佛他们是一颗光滑的球体,毫无瑕疵与破绽,像一个假人,像一台机器,没有灵性,他们望你一眼,让我心里咯噔一下,汗毛直立。

 

 

 

 

 

 

 

 

 

正文:

每个账户应该都是拥有私有变量

整数的默认值就是0,我们没有赋值的情况下

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

namespace ConsoleApp1
{

    class BankAccount
    {
        public int balance = 10; //表示余额的意思

        /// <summary>
        /// 夜光:存钱方法
        /// </summary>
        /// <param name="m">money 金额</param>
        public void Deposit(int m)  
        {
            balance += m;
        }


        /// <summary>
        /// 夜光:取钱方法
        /// </summary>
        /// <param name="m">取款的金额</param>
        /// <returns>取款是否成功</returns>
        public bool Withdraw(int m)
        {
            if(balance < m) //先进行一次判断
            {
                return false;
            }
            balance -= m;
            return true;
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            //夜光
            BankAccount xiaoyan = new BankAccount();
            Console.WriteLine(xiaoyan.balance);

            xiaoyan.Deposit(1000000); //存一个100W
            xiaoyan.Withdraw(100); //取100买个小玩意
            Console.WriteLine(xiaoyan.balance);

            Console.ReadLine();
        }
    }
}

 

 

 

 

 

 

 

 

 

 

比如一辆车,我们把一些方法写成私有的,对外的只有几个方法

 

 

 

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

namespace ConsoleApp3
{
    class Pet
    {
        public int jl;
        public int tl;
        public int kx;

        public int maxValue;  //属性的最大值
        public int recoverSpeed;  //属性的回复速度,我们定义一个
        public int downSpeed;

        /// <summary>
        /// 设置很多属性,我们传进去三个参数
        /// </summary>
        public Pet(int _maxValue, int _recoverSpeed, int _downSpeed)
        {
            maxValue = _maxValue;
            recoverSpeed = _recoverSpeed;
            downSpeed = _downSpeed;

            //初始状态下,属性为最大值
            tl = jl = kx = maxValue;
        }


        public void Play()
        {
            kx += recoverSpeed;
        }

        public void Eat()
        {
            tl += recoverSpeed;
        }

        public void Sleep()
        {
            jl += recoverSpeed;
        }
        public void Stay()
        {
            kx -= downSpeed;
            jl -= downSpeed;
            tl -= downSpeed;
        }

        public bool IsDead()
        {
            if (tl <= 0 || jl <= 0 || kx <= 0)  //判定死亡条件
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void print()
        {
            Console.SetCursorPosition(0, 0);
            if (IsDead())
            {
                Console.WriteLine("{0,40}", "已经死亡"); //"{0,40}"表示空出40个字符
            }
            else
            {
                Console.WriteLine("体力:{0},精力:{1},开心:{2}", tl, jl, kx);
            }
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            Pet pFree = new Pet(100, 1, 2); //免费宠物,放入到构造器里面去
/*            pFree.SetProperties(100, 1, 2);*/

            Pet pCharge = new Pet(150, 3, 1);  // 收费宠物
/*            pCharge.SetProperties(150, 3, 1);  //就类似王者荣耀里面的皮肤加成属性*/


            Pet pUse;  //夜光:指向当前正在使用的宠物

            #region
            //先设置一下初始的值
            /*            p.tl = 100;
                        p.jl = 100;
                        p.kx = 100;*/

            /*            p.SetProperties(100, 1, 2);*/
            #endregion

            //下面我会这么做
            Console.WriteLine("请选择要使用的宠物~~: (1 = 免费,2 = 收费)");
            //选择完之后,我们清一个屏

            string s = Console.ReadLine();

            Console.Clear();

            //我们保存到一个变量s

            if (s == "1")
            {
                pUse = pFree;
            }
            else if (s == "2")
            {
                pUse = pCharge;
            }
            else
            {
                Console.WriteLine("输入错误~~");
                Console.ReadLine();
                return;  //main方法,我们也可以写一个return
            }






            for (; ; )  //先写一个循环,判断玩家有没有对宠物进行操作
            {
                if (pUse.IsDead() == false)
                {
                    if (Console.KeyAvailable)  //按键
                    {
                        ConsoleKey key = Console.ReadKey(true).Key;  //在里面添加一个参数

                        if (key == ConsoleKey.Q)
                        {
                            pUse.Play();
                        }
                        else if (key == ConsoleKey.F1)
                        {
                            pUse.Eat();
                        }
                        else if (key == ConsoleKey.E)
                        {
                            pUse.Sleep();
                        }
                    }

                    else
                    {
                        pUse.Stay();
                    }
                }

                pUse.print();
                Thread.Sleep(1000); //休眠1秒
            }

        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值