日期:2014-05-20  浏览次数:20948 次

坦克大战补遗-爆炸效果
原帖
http://topic.csdn.net/u/20091012/16/f4538fa6-b5fb-4dfb-993f-cbf4b5c27a50_3.html

定义爆炸类
C# code

        class Explosion : RunObject
        {
            Size TextSize = TextRenderer.MeasureText("BOMB!", new Font("宋体", 10));

            public Explosion(Point Postion, Color ForeColor, Color BackColor)
                : base(ForeColor, BackColor)
            {
                ObjBmp = new Bitmap(100, 20);
                this.X = Postion.X - TextSize.Width / 2;
                this.Y = Postion.Y - TextSize.Height / 2;
                this.SumStep = 15;
            }

            public override void Draw(ref Bitmap Bmp)
            {
                using (Graphics G = Graphics.FromImage(ObjBmp))
                {
                    G.FillRectangle(new SolidBrush(BackColor), 0, 0, TextSize.Width, TextSize.Height);
                    // 震荡效果
                    G.DrawString("BOMB!", new Font("宋体", 10),
                        new SolidBrush(ForeColor), new PointF(0 + Step % 2 * 2 - 1, Step % 2 * 2 - 1));
                }
                base.Draw(ref Bmp);
            }

            public override void Move()
            {
                if (Step > SumStep)
                {
                    Step = -1;
                    return;
                }
                Step++;
            }
        }


然后定义全局列表变量
C# code

List<Explosion> Explosions = new List<Explosion>();


最后修改线程
C# code
        void RunThread()
        {
            try
            {
                int Start = Environment.TickCount;
                Random R = new Random();
                int KillCount = 0, DeathCount = 0;

                while (true)
                    if (Environment.TickCount - Start > 100)
                    {
                        Bitmap CacheBmp = new Bitmap(OrgBmp);

                        for (int i = 0; i < Tanks.Count; i++)
                        {
                            Tanks[i].Move();
                            Tanks[i].Draw(ref CacheBmp);
                            if (R.Next(10) == 0) // 电脑发子弹是10分之一的可能
                                Bullets.Add(new Bullet(Color.Red, Tanks[i]));
                        }
                        UTank.Draw(ref CacheBmp);
                        List<Bullet> TempBullets = new List<Bullet>();
                        for (int i = 0; i < Bullets.Count; i++)
                        {
                            if (Bullets[i].ObjStep != -1)
                            {
                                Rectangle Test = new Rectangle(Bullets[i].Postion.X - 10, Bullets[i].Postion.Y - 10, 20, 20);
                                bool IsKilled = false;
                                for (int j = 0; j < Tanks.Count; j++)
                                    if (Test.Contains(Tanks[j].Postion))
                                    {
                                        if (Bullets[i].Owner == UTank)
                                        {
                                            KillCount++;
                                            IsKilled = true;
                                            // ---------------------------------
                                            // 爆炸效果增加代码
                                            Explosions.Add(new Explosion(Tanks[j].Postion, Color.Maroon, this.BackColor));
                                            //------------------------------------
                                            Tanks[j] = new Tank(Color.Blue, this.BackColor);
                                        }
                                        break;
                                    }
                                if (!IsKilled)
                                    if (Test.Contains(UTank.Postion))
                                        if (Bullets[i].Owner != UTank)
                                        {
                                            DeathCount++;
                                            IsKilled = true;
                                            // ---------------------------------
                                            // 爆炸效果增加代码
                                            Explosions.Add(new Explosion(UTank.Postion, Color.Gold, this.BackColor));
                                            //------------------------------------
                                            break;
                                        }
                                if (!IsKilled)
                                    TempBullets.Add(Bullets[i]);
                            }
                        }
                        // ---------------------------------
                        // 爆炸效果增加代码
                        List<Explosion> TempExplosions = new List<Explosion>();
                        for (int i = 0; i < Explosions.Count; i++)
                        {
                            if (Explosions[i].ObjStep != -1)
                            {
                                Explosions[i].Move();
                                Explosions[i].Draw(ref CacheBmp);
                                TempExplosions.Add(Explosions[i]);
                            }
                        }
                        Explosions = new List<Explosion>(TempExplosions);
                        //------------------------------------
                        Bullets = new List<Bullet>(TempBullets);
                        for (int i = 0; i < Bullets.Count; i++)
                        {
                            Bullets[i].Move();
                            Bullets[i].Draw(ref CacheBmp);
                        }
                        Monitor.Enter(CacheBmp);
                        using (Graphics G = Graphics.FromImage(CacheBmp))
                        {
                            G.DrawString("杀敌数: " + KillCount.ToString() + " 死亡数: " + DeathCount.ToString(), this.Font,
                                Brushes.Black, new PointF(0, 0));
                        }
                        Monitor.Exit(CacheBmp);
                        this.Invoke(new SetImage(DoSetImage), new Object[] { CacheBmp });
                        Start = Environment.TickCount;
                    }
            }
            catch
            {
                // 忽略程序退出异常
            }
        }