我做了一個太空入侵者遊戲。起初,當我移動鼠標時,遊戲運行緩慢,因爲我有一個鼠標移動事件,所以有人告訴我,這是我的無效方法。我相應地改變了它,遊戲速度更好。但它現在不能清除舊圖像。它留下了一些圖像。鼠標移動離開第th圖像路徑
請幫忙!
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);
}
正被調用
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);
}
}
窗體加載輸出
一旦UpdatePosition方法鼠標移動輸出
我敢打賭,你的油漆處理程序未重繪它被要求重繪 –
**私人無效Form1_Paint(對象發件人,PaintEventArgs的E){ e.Graphics.DrawImage(objsp.gsImage,objsp.gsPos.X, objsp.gsPos.Y,objsp.gsImage.Width,objsp.gsImage.Height); } ** –
看起來您並未更新整個區域 –