鼠标移动留下图像的痕迹

时间:2011-11-27 06:53:03

标签: c# winforms

我做了一个太空入侵者游戏。起初,每当我移动鼠标时游戏都会变慢,因为我有鼠标移动事件,所以有人告诉我这是我的无效方法。我相应地改变它,游戏速度更好。但它现在还没有清除旧图像。它留下了一串图像。

请帮忙!

Mouse_move事件

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Cursor.Dispose();
        objsp.gsPos = new Point(MousePosition.X / 2 - 10, MousePosition.Y / 2 - 15);
        UpdatePosition(objsp.gsPos.X, objsp.gsPos.Y, objsp.gsImage);
    }

正在调用的UpdatePosition方法

    private void UpdatePosition(int dx, int dy, Image img)
    {            
        Point newPos = new Point(objsp.gsPos.X + dx, objsp.gsPos.Y + dy);

        //dont go out of window boundary
        newPos.X = Math.Max(0, Math.Min(ClientSize.Width - img.Width, newPos.X));
        newPos.Y = Math.Max(0, Math.Min(ClientSize.Height - img.Height, newPos.Y));

        if (newPos != objsp.gsPos)
        {
            objsp.gsPos = newPos;
            Rectangle rc = new Rectangle(objsp.gsPos, img.Size);
            Invalidate(rc);               
        }
    }

表单加载输出enter image description here

移动鼠标后输出

enter image description here

1 个答案:

答案 0 :(得分:1)

UpdatePosition中使当前和之前的鼠标位置无效。

相关问题