Figuratively speaking, there is an image of a tank that can move in any direction. But when the tank leaves the playing field, the image of the tank disappears, and it is necessary that the tank stops near the edge of the field (the image should not disappear).

Here is the image transfer code:

private void KeyDowdDo(object sender, KeyEventArgs e) { if (e.Key == Key.Right) image.Margin = new Thickness(image.Margin.Left + 3, image.Margin.Top, 0, 0); if (e.Key == Key.Left) image.Margin = new Thickness(image.Margin.Left - 3, image.Margin.Top, 0, 0); if (e.Key == Key.Up) image.Margin = new Thickness(image.Margin.Left, image.Margin.Top - 3, 0, 0); if (e.Key == Key.Down) image.Margin = new Thickness(image.Margin.Left, image.Margin.Top + 3, 0, 0); } 

help me please!!!

  • one
    It is necessary to compare the extreme position and position of the tank, if they are equal, then after further pressing the button in this direction set the speed to 0, according to the type image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); - Komdosh
  • And how to find out the extreme position - Ilya Vityuk
  • Either initially set or experimentally measured - Komdosh
  • Can you give an example of the type of code - Illya Vityuk

1 answer 1

In the comments it is not convenient to write code, here is an example.

 if (e.Key == Key.Down) if(image.Margin.Top>0) image.Margin = new Thickness(image.Margin.Left, image.Margin.Top + 3, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0);