快速移动图片框导致闪烁的C#

时间:2018-11-01 23:15:50

标签: c# forms picturebox

我正在尝试快速移动图片框,因为它代表子弹。但是,这会产生闪烁效果,并且会模糊图像,很难看到子弹移动。我尝试过使用双重缓冲和使图片框无效,然后再将其移动,但无济于事。有什么建议么?也许即时通讯使用双缓冲错误? (我将其设置为在加载表单时启用。)

代码

在表单上:

public void Shoot(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBox bulletImage = new PictureBox();
                DoubleBuffered = true;
                StandardBullet bullet = new StandardBullet(PB_CHARA.Location.X, PB_CHARA.Location.Y, FRM_GAME.MousePosition.X, FRM_GAME.MousePosition.Y, this.ClientRectangle, bulletImage);
                Controls.Add(bulletImage);
            }
        }

在Standard Bullet类中:

public class StandardBullet
    {
        public string ImageName = "DataBaseMod.Properties.Resources.StandardBullet_3x";
        public int sizeX = 15;
        public int sizeY = 19;
        public int x = 0;
        public int y = 0;
        int charaPostitionX;
        int charaPostitionY;
        PictureBox bulletPoint;
        public int[] vector = new int[2];
        private System.Timers.Timer bulletTimer;
        private System.Timers.Timer RemoveTimer;
        System.Drawing.Rectangle FRMBounds;
        //public delegate void UpdateControlsDelegate();
        public StandardBullet(int charaPostiX, int charaPostiY, int MousePostiX, int MousePostiY, System.Drawing.Rectangle FRMboundaries, PictureBox bulletImage)
        {
            FRMBounds = FRMboundaries;
            bulletPoint = bulletImage;
            bulletPoint.Name = ImageName;
            string filename = ImageName;
            bulletPoint.BackgroundImage = DataBaseMod.Properties.Resources.StandardBullet_3x;
            var size = new System.Drawing.Size(sizeX, sizeY);
            bulletPoint.Size = size;
            bulletPoint.BackgroundImageLayout = ImageLayout.Stretch;
            charaPostitionX = charaPostiX;
            charaPostitionY = charaPostiY;
            x = charaPostiX;
            y = charaPostiY;
            vector[0] = charaPostiX - MousePostiX;
            vector[1] = charaPostiY - MousePostiY;
            vectorCalc();
            bulletTimer = new System.Timers.Timer(10);
            RemoveTimer = new System.Timers.Timer(100);
            bulletTimer.Elapsed += TickHandler;
            bulletTimer.AutoReset = true;
            bulletTimer.Enabled = true;
            RemoveTimer.Elapsed += removeTickHandler;
            RemoveTimer.Enabled = true;
            RemoveTimer.AutoReset = true;

        }
        public void TickHandler(object sender, ElapsedEventArgs e)
        {
            x = x + vector[0];
            y = y + vector[1];
            moveBullet();

        }
        public void removeTickHandler(object sender, ElapsedEventArgs e)
        {
            RemoveBullet();
        }
        public void moveBullet()
        {
            bulletPoint.BeginInvoke(new MethodInvoker(() => { bulletPoint.Location = new System.Drawing.Point(x, y); }));  
        }
        public void vectorCalc()
        {
            if (vector[0] >= 1)
            {
                vector[0] = -10;
            }
            else if (vector[0] <= -1)
            {
                vector[0] = 10;
            }
            if (vector[1] >= 1)
            {
                vector[1] = -10;
            }
            else if (vector[1] <= -1)
            {
                vector[1] = 10;
            }
        }
        public void RemoveBullet()
        {
            if (
                (FRMBounds.Left >= bulletPoint.Bounds.Left) || 
                ( FRMBounds.Right <= bulletPoint.Bounds.Right) || 
                (FRMBounds.Top >= bulletPoint.Bounds.Top) || 
                (FRMBounds.Bottom <= bulletPoint.Bounds.Bottom)
               )
            {
                Death();
                return;
            }

        }
        public void Death()
        {
            try
            {
                bulletTimer.Enabled = false;
                bulletPoint.Invoke(new MethodInvoker(() => { FRM_GAME.KillBullet(bulletPoint); }));
                RemoveTimer.Enabled = false;
            }
            catch(Exception e)
            {

            }

        }
    }

谢谢!

编辑:我要删除此错误,因为我认为该错误可能是由我的计算机引起的。我在运行另外两个游戏,而我认为这可能会导致较差的渲染。今天早上跑了我的代码,一切都很好。抱歉。

1 个答案:

答案 0 :(得分:2)

将WinForms用于动态图形不是一个好主意。代替控件,尝试使用“图形”并在窗体(或面板或任何您想要的地方)上绘制所需的对象。