C#基础练习

  • 一个 Gun 基类,有攻击力 ap 字段和开火 Fire() 方法,DesertEagle 和 AK47 是它的子类
  • 一个 Player 玩家类,有装备 weapon 字段,有换枪,和开枪方法
  • 在游戏中,可以通过按键盘 1 装备 AK47,按 2 装备 DesertEagle,换装备时,应该在控制台输出;
  • 在游戏中,可以通过鼠标左键开枪,并在控制台输出是什么枪进行的射击,伤害是多少

枪类:父类

 public abstract class Guns
    {
        private int _ap;

        public int Ap { get => _ap; set => _ap = value; }

        private string _name;
        public string Name { get => _name; set => _name = value; }
        private int shengyuzidan;
        public int Shengyuzidan { get => shengyuzidan; set => shengyuzidan = value; }

  
        public Guns()
        {

        }
        public Guns(string _name, int _ap, int shengyuzidan)
        {
            this._name = _name;
            this._ap = _ap;
            this.Shengyuzidan = shengyuzidan;
        }


        public abstract void Fire();

    }

子类:

 internal class AK : Guns
    {
        public AK(string _name, int _ap, int shengyuzidan)
        {
            this.Name = _name;
            this.Ap = _ap;
            this.Shengyuzidan = shengyuzidan;
        }
        public override void Fire()
        {
            this.Shengyuzidan -= 1;
            Debug.Log($"AK开枪了,你的弹匣剩余为{this.Shengyuzidan}\n+" +
                $"你的手臂发麻并且对面对造成了{this.Ap}");
        }
    }

玩家类:

public class Player 
    {


        private int hp;
        private int level;
        private int maxhp;
        private Guns weapon;
        public int Hp { get => hp; set => hp = value; }
        public int Level { get => level; set => level = value; }
        public int Maxhp { get => maxhp; set => maxhp = value; }
        internal Guns Weapon { get => weapon; set => weapon = value; }

        public Player(int hp, int level, int maxhp)
        {
            Hp = hp;
            Level = level;
            Maxhp = maxhp;

        }
        public void ChangeGuns(Guns gun)
        {
            this.weapon = gun;
            Debug.Log($"换上了{gun.Name}");
        }
        public void Shoot()
        {
            weapon.Fire();
        }
    }

管理类:

public class playercontroll : MonoBehaviour
{
    private Player player;
    private Guns gun1;
    private Guns gun2;
    // Start is called before the first frame update
    public void initPlayer()
    {
        player = new Player(10, 1, 10);
        player.Weapon = gun1;
        Debug.Log($"当前武器为{player.Weapon.Name}");

    }
    void Start()
    {
        gun1 =new DesertEagle("沙鹰",75,6);
        gun2 = new AK("AK", 45, 30);
        this.initPlayer();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            player.Weapon = gun1;

            Debug.Log($"当前武器为{player.Weapon.Name}");
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            player.Weapon = gun2;
            Debug.Log($"当前武器为{player.Weapon.Name}");
        }
        if (Input.GetKeyDown(KeyCode.K))
        {

            player.Shoot();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值