I have a moving button and 4 static. This moving button should not pass through the others. But something is a problem, and it passes

if (_rectangles.All(x => !x.IntersectsWith(button2.Bounds)) || (_lastKey != Keys.None && _lastKey != e.KeyCode)) { switch (e.KeyCode) { case Keys.A: button2.Location = new Point(button2.Location.X - 5, button2.Location.Y); break; case Keys.S: if (button2.Location.Y + button2.Size.Height < this.ClientSize.Height) button2.Location = new Point(button2.Location.X, button2.Location.Y + 5); break; case Keys.W: if (button2.Location.Y + 1 > 5) button2.Location = new Point(button2.Location.X, button2.Location.Y - 5); break; case Keys.D: if (button2.Location.X + button2.Size.Width < this.ClientSize.Width) button2.Location = new Point(button2.Location.X + 5, button2.Location.Y); break; } } else _lastKey = e.KeyCode; } } 

    1 answer 1

    you need to add _lastKey = Keys.None;