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

//LevelForm.cs

using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace MyGame
{
    public partial class LevelForm : Form
    {
        //定義怪物和玩家
        Hero hero;
        TurtleMonster monster;
       
        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);
            try
            {
                #region 階段4
                //實例化hero對象
                hero = new Hero("李小俠",300,50,20,Image.FromFile("hero.gif"),this.picHero.Location,this.picHero.Size);

                ///////////設置玩家的屬性值/////////////
                //hero.HeroName = "李小俠";
                //hero.OriginalBlood = 300;
                //hero.AttackPower = 50;
                //hero.DefendPower = 20;
                //hero.Image = Image.FromFile("hero.gif");
                ////設置原始位置
                //hero.OriginalLocation = this.picHero.Location;
                ////設置當前位置-初始狀態與原始位置相同
                //hero.CurrentLocation = this.picHero.Location;
                ////設置玩家的大小
                //hero.Size = this.picHero.Size;


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

                #region 階段3
                //實例化怪物對象

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

                //monster.MonsterName = "小龜";
                //monster.OriginalBlood = 200;
                //monster.AttackPower = 50;
                //monster.DefendPower = 10;
                //monster.Image = Image.FromFile("turtle.gif");
                ////設置原始位置
                //monster.OriginalLocation = this.picMonster.Location;
                ////設置當前位置-初始狀態與原始位置相同
                //monster.CurrentLocation = this.picMonster.Location;
                ////設置玩家的大小
                //monster.Size = this.picMonster.Size;


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

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

        private void UpdateUI()
        {
            //移動到當前位置
            this.picHero.Location = hero.CurrentLocation;
            this.picMonster.Location = monster.CurrentLocation;
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            //調用玩家移動到怪物的方法
            hero.Move(monster);
            //重新顯示控件
            UpdateUI();
        }
        private void btnReturn_Click(object sender, EventArgs e)
        {
            //調用重載的返回
            hero.Move();
            //重新顯示控件
            UpdateUI();
        }

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

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

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


    }
}

 

//TurtleMonster.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

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.MonsterName = name;
            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; }
        }

        /// <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;
        }
    }
}
//Hero.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace MyGame
{
    /// <summary>
    /// MyGame中的玩家類
    /// </summary>
    public class Hero
    {
        public Hero() { }
        public Hero(string name, int originalBlood, int attackPower, int defendPower,
            Image image, Point originalLocation, Size size)
        {
            this.OriginalBlood = originalBlood;
            this.HeroName = name;
            this.AttackPower = attackPower;
            this.DefendPower = defendPower;
            this.Image = image;
            this.OriginalLocation = originalLocation;
            //當前位置賦予初始位置的參數即可
            this.CurrentLocation = originalLocation;
            this.Size = size;
        }
       
        //主角名字
        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; }
        }

        /// <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;
        }
    }
}

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