C#定义玩家和敌人类玩家攻击敌人,敌人扣血判断如果敌人的血为0,则输出游戏结束

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

namespace wanjia
{
    class Program
    {
        static void Main(string[] args)
        {
            Player wanjia = new Player("玩家",123456);
            wanjia._name = "和C#抗争到底";
            Console.WriteLine("名字:" + wanjia._name);
            Console.WriteLine("血条:" + wanjia.hp);
            Console.WriteLine("魔法值:" + wanjia.mp);
            Console.WriteLine("攻击力:" + wanjia.atp);
            Console.WriteLine("攻击的方法:" + wanjia.AttackDamage);
            Console.WriteLine("受攻击的方法:" + wanjia.TakeDamage);

            Console.WriteLine(" ");

            Enemy diren = new Enemy("敌人",123456);
            diren._name = "C#真难学";
            Console.WriteLine("名字:" + diren._name);
            Console.WriteLine("血条:" + diren.hp);
            Console.WriteLine("魔法值:" + diren.mp);
            Console.WriteLine("攻击力:" + diren.atp);
            Console.WriteLine("攻击的方法:" + diren.AttackDamage);
            Console.WriteLine("受攻击的方法:" + diren.TakeDamage);

            while (diren.Health > 0)
            {
                wanjia.Attack(diren);
                Console.WriteLine("玩家:" + "‘" + wanjia._name + "’" + "会心一击!");
                Console.WriteLine("敌人:" + "‘" + diren._name + "’" + "受到:" + wanjia.atp + "伤害");
                Console.WriteLine("敌人:" + "‘" + diren._name + "’" + "剩余生命值:" + diren.Health);
                Console.WriteLine(" ");
            }

            Console.WriteLine("敌人“C#真难学”死亡,游戏结束");
        }
    }

    public class Player : Person
    {
        public Player(string name,int health) : base(name,health)
        {
            _name = name;
            Health = health;
        }
        public string _name = "lulin";
        public int hp = 1000000;
        public bool isAlive = true;
        public float money = 88888;
        public int atp = 99999;
        public int mp = 66666;
        public string TakeDamage = "击打";
        public string AttackDamage = "冲刺";
        public void Attack(Person person)
        {
             person.ReceiveDamage(54321);
        }
    }
    public class Enemy : Person
    {
        public Enemy(string name,int health) : base(name,health)
        {
            _name = name;
            Health = health;
        }
        public string _name = "敌人";
        public int hp = 10000000;
        public bool isAlive = true;
        public float money = 8888;
        public int atp = 9999;
        public int mp = 666;
        public string TakeDamage = "击打";
        public string AttackDamage = "冲刺";
    }

    public class Person
    {
       
        public string Name { get; set; }
        public int Health { get; set; }

        public Person(string name, int health)
        {
            Name = name;
            Health = health; 
        }
        public void ReceiveDamage(int damage)
        {
            Health -= damage;
            if (Health < 0) Health = 0; //防止生命为负
        }


    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值