Как переназначить координаты в консоли (Console.SetCursorPosition(x, y)) - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте! У меня тестовая мини игра. Не получается смайлик сбросить координаты с начальной позиции при нажатии New game и при нажатии Continion не рисует начальный смайл в той клетке где остался после нажатия "Esc". Перепробовал всё да вот знаю мало пока что, чтоб понять где ошибка Заранее извиняюсь за код. Изучаю C# почти месяц. Просто изучаю по вечерам и нет возможности другой а хочется знать больше на собственных примерах. Так мне проще усвоить материал. Проект который делал на Visual Studio 2011 Beta - во вложении Тут код, части программы где я пытаюсь поставить условие на координаты при нажатии новой игры или продолжение игры.
Листинг программы
  1. using System;
  2. using sc = System.Console;
  3. using scc = System.ConsoleColor;
  4. namespace Test
  5. {
  6. public class Game
  7. {
  8. Map map = new Map();
  9. Player player = new Player();
  10. CursorPosition cpplayer = new CursorPosition();
  11. ConsoleKeyInfo keyboard;
  12. bool startend = true;
  13. public Game()
  14. {
  15. }
  16. public bool GameCreate(int newcon)
  17. {
  18. map.MapCreate();
  19. sc.ForegroundColor = scc.Green;
  20. if (newcon == 1) cpplayer.CurPosPlaXY(0);
  21. if (newcon == 0)
  22. {
  23. cpplayer.CurPosPlaXY(11);
  24. cpplayer.CurPosPlaXY(0);
  25. }
  26. sc.Write(player.player011);
  27. if (newcon == 1) cpplayer.CurPosPlaXY(0);
  28. if (newcon == 0)
  29. {
  30. cpplayer.CurPosPlaXY(11);
  31. cpplayer.CurPosPlaXY(0);
  32. }
  33. while (startend)
  34. {
  35. if (sc.KeyAvailable == true)
  36. {
  37. keyboard = sc.ReadKey(true);
  38. if (keyboard.Key == ConsoleKey.Escape) return false;
  39. player.PlayerStep(keyboard);
  40. }
  41. }
  42. return true;
  43. }
  44.  
  45. public int yscp { get; set; }
  46. }
  47. }
Вот скрин того что у меня получается. И я понимаю что можно было бы сделать через массив движение и запоминание смайлика, но я просто начал с координат курсора и хотелось бы понять где ошибка. Чтоб знать что и как на будущее. А потом создам с использованием сохранения смайлика в клетках массива. Заранее извините за исчерпывающую информацию по моему вопросу. И заранее спасибо!

Решение задачи: «Как переназначить координаты в консоли (Console.SetCursorPosition(x, y))»

textual
Листинг программы
  1. using System;
  2. using sc = System.Console;
  3. using scc = System.ConsoleColor;
  4. using st = System.Text;
  5.  
  6. namespace Test
  7. {
  8.     public class Game
  9.     {
  10.         Map map = new Map();
  11.         Player player = new Player();
  12.         CursorPositionPlayer cpplayer = new CursorPositionPlayer();
  13.  
  14.         char player001 = st.Encoding.GetEncoding(437).GetChars(new byte[] { 1 })[0];     //
  15.  
  16.         ConsoleKeyInfo keyboard;
  17.         ConsoleKeyInfo playerstep;
  18.  
  19.         bool startend = true;
  20.  
  21.         //int cppnc;
  22.  
  23.         public string player011;
  24.  
  25.         public Game()
  26.         {
  27.             //cppnc = 11;
  28.  
  29.             player011 = ((char)player001).ToString();     //
  30.         }
  31.  
  32.         public bool GameCreate(int newcon)
  33.         {
  34.             map.MapCreate();
  35.  
  36.             cpplayer.CurPosPlaXY(newcon);
  37.             sc.ForegroundColor = scc.Green;
  38.             sc.Write(player.player011);
  39.             sc.ForegroundColor = scc.Black;
  40.             cpplayer.CurPosPlaXY(newcon);
  41.  
  42.             while (startend)
  43.             {
  44.                 if (sc.KeyAvailable == true)
  45.                 {
  46.                     keyboard = sc.ReadKey(true);
  47.                     if (keyboard.Key == ConsoleKey.Escape) return false;
  48.  
  49.                     playerstep = keyboard;
  50.  
  51.                     if (playerstep.Key == ConsoleKey.RightArrow)
  52.                     {
  53.                         if ((cpplayer.cppX == 1 & cpplayer.cppY == 0) | (cpplayer.cppX == 1 & cpplayer.cppY == 4) | (cpplayer.cppX == 5 & cpplayer.cppY == 4))
  54.                         {
  55.                             sc.Write(" ");
  56.                             cpplayer.CurPosPlaXY(2);     //CursorPosition.cs CursorPositionPlayer()
  57.                             sc.ForegroundColor = scc.Green;     //Color text(Green)
  58.                             sc.Write(player001);
  59.                             sc.ForegroundColor = scc.Black;     //Color text(Black)
  60.                             cpplayer.CurPosPlaXY(10);
  61.                         }
  62.                     }
  63.  
  64.                     if (playerstep.Key == ConsoleKey.LeftArrow)
  65.                     {
  66.                         if ((cpplayer.cppX == 5 & cpplayer.cppY == 0) | (cpplayer.cppX == 9 & cpplayer.cppY == 4) | (cpplayer.cppX == 5 & cpplayer.cppY == 4))
  67.                         {
  68.                             sc.Write(" ");
  69.                             cpplayer.CurPosPlaXY(3);     //CursorPosition.cs CursorPositionPlayer()
  70.                             sc.ForegroundColor = scc.Green;     //Color text(Green)
  71.                             sc.Write(player001);
  72.                             sc.ForegroundColor = scc.Black;     //Color text(Black)
  73.                             cpplayer.CurPosPlaXY(10);
  74.                         }
  75.                     }
  76.  
  77.                     if (playerstep.Key == ConsoleKey.DownArrow)
  78.                     {
  79.                         if ((cpplayer.cppX == 5 & cpplayer.cppY == 0) | (cpplayer.cppX == 5 & cpplayer.cppY == 2) | (cpplayer.cppX == 1 & cpplayer.cppY == 2) | (cpplayer.cppX == 9 & cpplayer.cppY == 2))
  80.                         {
  81.                             sc.Write(" ");
  82.                             cpplayer.CurPosPlaXY(4);     //CursorPosition.cs CursorPositionPlayer()
  83.                             sc.ForegroundColor = scc.Green;     //Color text(Green)
  84.                             sc.Write(player001);
  85.                             sc.ForegroundColor = scc.Black;     //Color text(Black)
  86.                             cpplayer.CurPosPlaXY(10);
  87.                         }
  88.                     }
  89.  
  90.                     if (playerstep.Key == ConsoleKey.UpArrow)
  91.                     {
  92.                         if ((cpplayer.cppX == 5 & cpplayer.cppY == 2) | (cpplayer.cppX == 5 & cpplayer.cppY == 4) | (cpplayer.cppX == 1 & cpplayer.cppY == 4) | (cpplayer.cppX == 9 & cpplayer.cppY == 4))
  93.                         {
  94.                             sc.Write(" ");
  95.                             cpplayer.CurPosPlaXY(5);     //CursorPosition.cs CursorPositionPlayer()
  96.                             sc.ForegroundColor = scc.Green;     //Color text(Green)
  97.                             sc.Write(player001);
  98.                             sc.ForegroundColor = scc.Black;     //Color text(Black)
  99.                             cpplayer.CurPosPlaXY(10);
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.            
  105.             return true;
  106.         }
  107.     }
  108. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.545 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы