由于我在游戏中创建了N个类,有可能完本插入代码,Bety不够,下面就插入《单例设计模式》的代码,及个别类的代码,如果哪里有错误,希望高手指教!
下面插入一个Games类的代吗,也是游戏的主要部分《单例设计模式》
namespace WindowsFormsApplication1
{
    public class Games
    {
        private Games()
        {        }
               private static volatile Games instance;//,Volatile禁止系统微调代码间的调用。这个也很关健。
        private static object lockHelper =new object();//这个是一个辅助对象为,单例设计模式加一个双保险、
        public static Games GetInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)//双保险的实现,经过两层的过虑,还要注意上面《volatile Games instance;>>关健字,Volatile禁止系统微调代码间的调用。
                {
                    if (instance == null)
                    {
                        instance = new Games();
                    }
                }
            }
            return instance;
        }
        private static Enelmy en = null;
        public static Enelmy En
        {
            get { return en; }
            set { en = value; }
        }
        private Player1 p1 = null;        public Player1 P1
        {
            get { return p1; }
            set { p1 = value; }
        }
        #endregion
        public static List<Player1> player1 = new List<Player1>();
        private List<BloodBar> Bar = new List<BloodBar>();
         private List<MessageBoxs> messageboxs = new List<MessageBoxs>();
        public void AddElement(Element e)
        {
            if (e is Player1)
            {
                player1.Add(e as Player1);
                return;
            }
            if (e is MessageBoxs)
            {
                messageboxs.Add(e as MessageBoxs);
                return;
            }
        }
        public void DrawBar(Graphics e)
        {            for (int i = 0; i < Bar.Count; i++)
            {
                Bar[i].Draw(e);
            }
            for (int i = 0; i < enelmyBar.Count; i++)
            {
                enelmyBar[i].Draw(e);
            }
        }
        public void Draw(Graphics e)
        {
                        for (int i = 0; i < efficts.Count; i++)
            {
                efficts[i].Draw(e);
            }
            for (int i = 0; i < erroreffict.Count; i++)
            {
                erroreffict[i].Draw(e);
            }
            
            for (int i = 0; i < missile.Count; i++)
            {
                missile[i].Draw(e);
            }
                        for (int i = 0; i < skstr.Count; i++)
            {
                skstr[i].Draw(e);
            }        }
                public void KeyUp(KeyEventArgs e)    //按键松开方法
        {
            //MessageBox.Show(e.KeyValue.ToString());
            for (int i = 0; i < skstr.Count; i++)
            {
                if (e.KeyValue > 64 && e.KeyValue < 91)
                {
                    if (e.KeyValue == (skstr[i].Skstr))
                    {
                        for (int k = 0; k < player1.Count; k++)
                        {
                            if (player1[k].Isattack)
                            {
                                skstr[i].IsAlive1 = false;
                                Games.GetInstance().AddElement(new Missile(skstr[i], 20, 2, 20, 20, player1 [k].Id,player1[k].Attack));//输入正确发《《出一个子弹。。
                                isgoodStringNum++;
                                                        }
                            else if (k == player1.Count - 1)
                            {//这里主要是确定一下子弹是由哪个英雄发出来。
                                skstr[i].IsAlive1 = false;
                                Games.GetInstance().AddElement(new Missile(skstr[i], 20, 2, 20, 20, player1[k].Id, player1[k].Attack));
                            }
                        }
                        return;//如果输入正确哪么就返回。不加的话,一次会把屏上所有对的全打出来。
                    }
                    else if (e.KeyValue != (skstr[i].Skstr) && i == skstr.Count - 1)//如果输入错误,并且是所有集合都没有此字符,哪么提示出错信息。
                    { Games.GetInstance().AddElement(new ErrorEffict(skstr[i].X, skstr[i].Y, 2, skstr[i], 1, 1)); this.errornums++; return; }
                }
            }
            #region//打开选择人物的背包。。
            for (int i = 0; i < player1.Count; i++)
            {
                if (player1[i].Ischose1)
                {
                    if (e.KeyValue == 119 && !openBag)
                    {
                        openBag = true;//打开背包的标志
                        MainFrm.Getinstance().GamePause1 = true;
                        BagFrm bagf = new BagFrm();
                        bagf.ShowDialog();
                        if (bagf.DialogResult == DialogResult.Cancel)
                        {
                            openBag = !openBag;
                            MainFrm.Getinstance().GamePause1 = false;
                        }                        return;
                    }
                }
                //else if (i == player1.Count - 1) { player1[i].Ischose1 = true; return; }//可如果没有选择人物哪么,最后一个默认为选择
            }
            #endregion        }
               
        public void MouseMove(MouseEventArgs e, Graphics g)
        {
            for (int i = 0; i < wpred.Count; i++)
            {
                wpred[i].MouseMove(e,g);
            }
        }
        public void Mouse_Click(MouseEventArgs e)
        {
            for (int i = 0; i < player1.Count; i++)
            {
                player1[i].MouseClick(e);
            }
           
            for (int i = 0; i < wpred.Count; i++)
            {
                for (int j = 0; j < player1.Count; j++)
                {
                    player1[j].Get_wuping(e, wpred[i]);
                }
            }
            for (int i = 0; i < messageboxs.Count; i++)
            {
                messageboxs[i].MouseClick(e);
            }
            for (int i = 0; i < Bar.Count; i++)
            {
                Bar[i].Isselect = player1[i].Ischose1;
            }
            int Tempx = 0;
            int Tempy = 0;
            double temsum = 0;
            for (int j = 0; j < player1.Count; j++)
            {
                Tempx = Math.Abs(player1[j].X + 40 - e.X);//还是我自己的碰撞检测好用的多,用GetRectangle检测鼠标不太好用。
                Tempy = Math.Abs(player1[j].Y + 40 - e.Y);
                temsum = Math.Sqrt(Tempx * Tempx + Tempy * Tempy);
                if (temsum < 46)
                {
                    Bar[player1 .Count -1].Isselect = false;
                    Bar[j].Isselect = true;
                }
            }
        }
        public void IsWinOrLost()//游戏是否胜利
        {
            if (Games.enelmy.Count <= 0)
            {
                Games.overgames.Add(new OverGames(350, 180, 2, false, -1));
                Games.GetInstance().win = Win.YouWin;
            }
            if (Games.player1.Count < 1)
            {
                Games.GetInstance().win = Win.YouLost;
                Games.overgames.Add(new OverGames(350, 180, 2, false, -1));
                Games.LevelGames = 1;
            }
           
        }
     }