一步步教學生開發簡單遊戲(二)

//Hero.cs

namespace MyGame
{
    /// <summary>
    /// 武器+傷害
    /// </summary>
    public enum Weapon
    {
        Knife = 5, Sword = 10, Hammer = 15
    }   

/// <summary>
    /// MyGame中的玩家類
    /// </summary>
    public class Hero
    {
        public Hero() { }
        public Hero(string name, int originalBlood, int attackPower, int defendPower,
            Image image, Point originalLocation, Size size,Weapon weapon)
        {
            this.OriginalBlood = originalBlood;
            //初始狀態原始生命和當前聲明相同
            this.CurrentBlood = originalBlood;
            this.HeroName = name;
            this.AttackPower = attackPower;
            this.DefendPower = defendPower;
            this.Image = image;
            this.OriginalLocation = originalLocation;
            //當前位置賦予初始位置的參數即可
            this.CurrentLocation = originalLocation;
            this.Size = size;
            this.Weapon = weapon;
        }
       
        //主角名字
        private string heroName;
        public string HeroName
        {
            get { return heroName; }
            set { heroName = value; }
        }
        //原始生命值
        private int originalBlood;
        public int OriginalBlood
        {
            get { return originalBlood; }
            set { originalBlood = value; }
        }
        //攻擊力
        private int attackPower;
        public int AttackPower
        {
            get { return attackPower; }
            set { attackPower = value; }
        }
        //防禦力
        private int defendPower;
        public int DefendPower
        {
            get { return defendPower; }
            set { defendPower = value; }
        }
        //玩家的圖片
        private Image image;
        public Image Image
        {
            get { return image; }
            set { image = value; }
        }
        //原始位置
        private Point originalLocation;
        public Point OriginalLocation
        {
            get { return originalLocation; }
            set { originalLocation = value; }
        }
        //當前位置
        private Point currentLocation;
        public Point CurrentLocation
        {
            get { return currentLocation; }
            set { currentLocation = value; }
        }
        // 大小
        private Size size;
        public Size Size
        {
            get { return size; }
            set { size = value; }
        }

        //第三章添加的屬性 玩家持有的武器
        private Weapon weapon;
        public Weapon Weapon
        {
            get { return weapon; }
            set { weapon = value; }
        }

        //怪物的當前生命值
        private int currentBlood;
        public int CurrentBlood
        {
            get { return currentBlood; }
            set
            {
                if (value <= 0)
                {
                    currentBlood = 0;
                }
                else
                {
                    currentBlood = value;
                }
            }
        }

        /// <summary>
        /// 移動到怪物的左下角
        /// </summary>
        /// <param name="monster">玩家所移動到的怪物對象</param>
        public void Move(TurtleMonster monster)
        {
            //移動到怪物左下角
            this.CurrentLocation = new Point(
                monster.OriginalLocation.X,
                monster.OriginalLocation.Y + monster.Size.Height);
        }

        /// <summary>
        /// 重載Move方法  返回原始位置
        /// </summary>
        public void Move()
        {
            //返回是將原始位置設爲當前位置
            this.CurrentLocation = this.OriginalLocation;
        }

        /// <summary>
        /// 攻擊怪物,並輸出信息
        /// </summary>
        /// <param name="monster">攻擊的怪物</param>
        /// <returns>返回message</returns>
        public string Attack(TurtleMonster monster)
        {
            //輸出message
            string message = string.Format("我是{0},看招。", this.HeroName);
            //受到的傷害值
            int totalDemage;
            //判斷攻擊力是否小於防禦力
            if ((int)this.Weapon + this.AttackPower < monster.DefendPower)
            {
                //如果小於防禦力傷害值設爲0
                totalDemage = 0;
            }
            else
            {
                //大於防禦力,傷害值設爲武器攻擊力+攻擊力-防禦力
                totalDemage = (int)this.Weapon + this.AttackPower - monster.DefendPower;
            }
            //計算當前的生命值
            monster.CurrentBlood = monster.CurrentBlood - totalDemage;
            return message;
        }


        /// <summary>
        /// 移動到怪物的左下角
        /// </summary>
        /// <param name="monster">玩家所移動到的怪物對象</param>
        public void Move(StructMonster monster)
        {
            //移動到怪物左下角
            this.CurrentLocation = new Point(
                monster.OriginalLocation.X,
                monster.OriginalLocation.Y + monster.Size.Height);
        }

        /// <summary>
        /// 攻擊怪物,並輸出信息
        /// </summary>
        /// <param name="monster">攻擊的怪物</param>
        /// <returns>返回message</returns>
        public string Attack(StructMonster monster)
        {
            //輸出message
            string message = string.Format("我是{0},看招。", this.HeroName);
            //受到的傷害值
            int totalDemage;
            //判斷攻擊力是否小於防禦力
            if ((int)this.Weapon + this.AttackPower < monster.DefendPower)
            {
                //如果小於防禦力傷害值設爲0
                totalDemage = 0;
            }
            else
            {
                //大於防禦力,傷害值設爲武器攻擊力+攻擊力-防禦力
                totalDemage = (int)this.Weapon + this.AttackPower - monster.DefendPower;
            }
            //計算當前的生命值
            monster.CurrentBlood = monster.CurrentBlood - totalDemage;
            return message;
        }
    }
}
//TurtleMonster.cs

namespace MyGame
{
    /// <summary>
    /// MyGame中的怪物類
    /// </summary>
    public class TurtleMonster
    {
        public TurtleMonster() { }
        public TurtleMonster(string name,Image image, Point originalLocation, Size size)
        {
            this.OriginalBlood = 200;
            this.AttackPower = 50;
            this.DefendPower = 10;

            //初始狀態原始生命和當前聲明相同
            this.CurrentBlood = this.OriginalBlood;
            this.MonsterName = name;
            this.AttackPower = attackPower;
            this.DefendPower = defendPower;
            this.Image = image;
            this.OriginalLocation = originalLocation;
            this.CurrentLocation = originalLocation;
            this.Size = size;
        }
       
        //怪物名字
        private string monsterName;
        public string MonsterName
        {
            get { return monsterName; }
            set { monsterName = value; }
        }
        //原始生命值
        private int originalBlood;
        public int OriginalBlood
        {
            get { return originalBlood; }
            set { originalBlood = value; }
        }
        //攻擊力
        private int attackPower;
        public int AttackPower
        {
            get { return attackPower; }
            set { attackPower = value; }
        }
        //防禦力
        private int defendPower;
        public int DefendPower
        {
            get { return defendPower; }
            set { defendPower = value; }
        }
        //怪物的圖片
        private Image image;
        public Image Image
        {
            get { return image; }
            set { image = value; }
        }
        //原始位置
        private Point originalLocation;
        public Point OriginalLocation
        {
            get { return originalLocation; }
            set { originalLocation = value; }
        }
        //當前位置
        private Point currentLocation;
        public Point CurrentLocation
        {
            get { return currentLocation; }
            set { currentLocation = value; }
        }
        // 大小
        private Size size;
        public Size Size
        {
            get { return size; }
            set { size = value; }
        }

        //怪物的當前生命值
        private int currentBlood;
        public int CurrentBlood
        {
            get { return currentBlood; }
            set
            {
                if (value <= 0)
                {
                    currentBlood = 0;
                }
                else
                {
                    currentBlood = value;
                }
            }
        }
        /// <summary>
        /// 怪物移動的方法
        /// </summary>
        /// <param name="hero">移動到玩家</param>
        public void Move(Hero hero)
        {
            //移動到玩家
            this.CurrentLocation = new Point(
                hero.OriginalLocation.X,
                hero.OriginalLocation.Y - hero.Size.Height
                );
        }
        /// <summary>
        /// 回到原位置方法
        /// </summary>
        public void Move()
        {
            //返回是將原始位置設爲當前位置
            this.CurrentLocation = this.OriginalLocation;
        }

        /// <summary>
        /// 攻擊玩家,並輸出信息
        /// </summary>
        /// <param name="hero">攻擊的玩家</param>
        /// <returns>返回message</returns>
        public string Attack(Hero hero)
        {
            string message = string.Format("吃我{0}一招!", this.MonsterName);
            int totalDemage;
            //判斷攻擊力是否小於防禦力
            if (this.AttackPower < hero.DefendPower)
            {
                //如果小於防禦力傷害值設爲0
                totalDemage = 0;
            }
            else
            {
                //如果大於防禦力,傷害值設爲攻擊力-防禦力
                totalDemage = this.AttackPower - hero.DefendPower;
            }
            //當前血格不能減爲0
            hero.CurrentBlood = hero.CurrentBlood - totalDemage;
            return message;
        }

        /// <summary>
        /// 攻擊玩家,並輸出信息
        /// </summary>
        /// <param name="hero">攻擊的玩家</param>
        /// <returns>返回message</returns>
        public string Attack(StructHero hero)
        {
            string message = string.Format("吃我{0}一招!", this.MonsterName);
            int totalDemage;
            //判斷攻擊力是否小於防禦力
            if (this.AttackPower < hero.DefendPower)
            {
                //如果小於防禦力傷害值設爲0
                totalDemage = 0;
            }
            else
            {
                //如果大於防禦力,傷害值設爲攻擊力-防禦力
                totalDemage = this.AttackPower - hero.DefendPower;
            }
            //當前血格不能減爲0
            hero.CurrentBlood = hero.CurrentBlood - totalDemage;
            return message;
        }
        /// <summary>
        /// 怪物移動的方法
        /// </summary>
        /// <param name="hero">移動到玩家</param>
        public void Move(StructHero hero)
        {
            //移動到玩家
            this.CurrentLocation = new Point(
                hero.OriginalLocation.X,
                hero.OriginalLocation.Y - hero.Size.Height
                );
        }
    }
}

 

//levelForm.cs

namespace MyGame
{
    public partial class LevelForm : Form
    {
        //定義怪物和玩家
        Hero hero;

        TurtleMonster monster;

        //StructHero hero;
       
        public LevelForm()
        {
            InitializeComponent();
        }

        private void LevelForm_Load(object sender, EventArgs e)
        {
            this.lblHeroBlood.Size = new Size(0, 20);
            this.lblMonsterBlood.Size = new Size(0, 20);
            //初始化標籤
            this.lblHeroCurBlood.Size = new Size(0, 20);
            this.lblMonCurBlood.Size = new Size(0, 20);

            this.lblMessage.Text = "";

            try
            {

                hero = new Hero("李小俠", 300, 50, 20, Image.FromFile("hero.gif"),
                    this.picHero.Location, this.picHero.Size, Weapon.Sword);

       

                //將生命值顯示到標籤
                this.lblHeroBlood.Width = hero.OriginalBlood;
                this.lblHeroBlood.Height = 20;
                //用於顯示受傷的標籤
                this.lblHeroCurBlood.Width = hero.CurrentBlood;
                this.lblHeroCurBlood.Height = 20;
                //將圖片顯示到PictureBox
                this.picHero.Image = hero.Image;

                //怪物對象
                monster = new TurtleMonster("小龜",Image.FromFile("turtle.gif"),
                    this.picMonster.Location, this.picMonster.Size);

                              //將生命值顯示到標籤
                this.lblMonsterBlood.Width = monster.OriginalBlood;
                this.lblMonsterBlood.Height = 20;
                this.lblMonCurBlood.Width = monster.CurrentBlood;
                this.lblMonCurBlood.Height = 20;
                //將圖片顯示到PictureBox
                this.picMonster.Image = monster.Image;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void UpdateUI()
        {
            //移動到當前位置
            this.picHero.Location = hero.CurrentLocation;
            this.picMonster.Location = monster.CurrentLocation;
            //玩家生命值變化條
            this.lblHeroCurBlood.Width = hero.CurrentBlood;
            this.lblHeroCurBlood.Height = 20;
            //怪物生命值變化條
            this.lblMonCurBlood.Width = monster.CurrentBlood;
            this.lblMonCurBlood.Height = 20;
        }

        private void btnAttack_Click(object sender, EventArgs e)
        {
            //string message = null;
            ////調用玩家移動到怪物的方法
            //hero.Move(monster);
            ////重新顯示控件
            //UpdateUI();
            ////調用玩家攻擊方法
            //message = hero.Attack(monster);
            ////重新顯示控件
            //UpdateUI();
            //this.lblMessage.Text = message;
        }
        private void btnReturn_Click(object sender, EventArgs e)
        {
            ////調用重載的返回
            //hero.Move();
            ////重新顯示控件
            //UpdateUI();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnMonsterAttack_Click(object sender, EventArgs e)
        {
            string message = null;
            monster.Move(hero);
            UpdateUI();
            message = monster.Attack(hero);
            UpdateUI();
            this.lblMessage.Text = message;
        }

        private void btnMonsterReturn_Click(object sender, EventArgs e)
        {
            monster.Move();
            UpdateUI();
        }


    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章