Figuratively speaking, there is an image of a tank that can move in any direction using the arrow buttons on the keyboard + the picture of the tank does not crawl over the edges (stops at the edge) + while the picture itself rotates, but I have manually drawn obstacles ( not a picture ) and it is necessary that when it touches the obstacles to stop

below the code as it goes and stops in front of the window + turns (not perfect but in my version works as it should)

if (e.Key == Key.Up) { TransformGroup tg; System.Windows.Media.RotateTransform rt; tg = image.RenderTransform as System.Windows.Media.TransformGroup; if (tg == null) { tg = new System.Windows.Media.TransformGroup(); image.RenderTransform = tg; } rt = tg.Children.FirstOrDefault(x => x is System.Windows.Media.RotateTransform) as System.Windows.Media.RotateTransform; if (rt == null) { rt = new System.Windows.Media.RotateTransform(); tg.Children.Add(rt); } rt.Angle = 90; } if (e.Key == Key.Right) { TransformGroup tg; System.Windows.Media.RotateTransform rt; tg = image.RenderTransform as System.Windows.Media.TransformGroup; if (tg == null) { tg = new System.Windows.Media.TransformGroup(); image.RenderTransform = tg; } rt = tg.Children.FirstOrDefault(x => x is System.Windows.Media.RotateTransform) as System.Windows.Media.RotateTransform; if (rt == null) { rt = new System.Windows.Media.RotateTransform(); tg.Children.Add(rt); } rt.Angle = 180; } if (e.Key == Key.Down) { TransformGroup tg; System.Windows.Media.RotateTransform rt; tg = image.RenderTransform as System.Windows.Media.TransformGroup; if (tg == null) { tg = new System.Windows.Media.TransformGroup(); image.RenderTransform = tg; } rt = tg.Children.FirstOrDefault(x => x is System.Windows.Media.RotateTransform) as System.Windows.Media.RotateTransform; if (rt == null) { rt = new System.Windows.Media.RotateTransform(); tg.Children.Add(rt); } rt.Angle = -90; } if (e.Key == Key.Left) { TransformGroup tg; System.Windows.Media.RotateTransform rt; tg = image.RenderTransform as System.Windows.Media.TransformGroup; if (tg == null) { tg = new System.Windows.Media.TransformGroup(); image.RenderTransform = tg; } rt = tg.Children.FirstOrDefault(x => x is System.Windows.Media.RotateTransform) as System.Windows.Media.RotateTransform; if (rt == null) { rt = new System.Windows.Media.RotateTransform(); tg.Children.Add(rt); } rt.Angle = 360; } if (e.Key == Key.Left) if (image.Margin.Left > 10) image.Margin = new Thickness(image.Margin.Left - 15, image.Margin.Top, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); if (e.Key == Key.Right) if (image.Margin.Left < 1230) image.Margin = new Thickness(image.Margin.Left + 15, image.Margin.Top, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); if (e.Key == Key.Up) if (image.Margin.Top > 50) image.Margin = new Thickness(image.Margin.Left, image.Margin.Top - 15, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); if (e.Key == Key.Down) if (image.Margin.Top < 660) image.Margin = new Thickness(image.Margin.Left, image.Margin.Top + 15, 0, 0); else image.Margin = new Thickness(image.Margin.Left, image.Margin.Top, 0, 0); 

Please make a sample code !!!

  • 3
    You should separate the content from the presentation. Create an internal representation of the field and objects, with each step, check its possibility. In short, you need to write a lot more. - VladD
  • But at least Ilya Vityuk
  • "I have hand-drawn obstacles (not a picture)" - on the monitor with a felt-tip pen chtol? - Dmitry Chistik

1 answer 1

Well, here's an example.

 public void Move(ref int x, ref int y, int a, bool[][]map) { int nX = x + (a == 1 ? -1 : a == 3 ? 1 : 0); int nY = y + (a == 2 ? 1 : a == 4 ? -1 : 0); if(nX>=0 && nX<map.length) if(nY>=0 && nY<map[nX].length) if (!map[nX][nY]) { x = nX; y = nY; } } 

PS:

a : 1-Left; 2-up; 3-right; 4-Down;

map : obstacle map;