Как переназначить координаты в консоли (Console.SetCursorPosition(x, y)) - C#
Формулировка задачи:
Здравствуйте! У меня тестовая мини игра. Не получается смайлик сбросить координаты с начальной позиции при нажатии New game и при нажатии Continion не рисует начальный смайл в той клетке где остался после нажатия "Esc". Перепробовал всё да вот знаю мало пока что, чтоб понять где ошибка
Заранее извиняюсь за код. Изучаю C# почти месяц. Просто изучаю по вечерам и нет возможности другой а хочется знать больше на собственных примерах. Так мне проще усвоить материал.
Проект который делал на Visual Studio 2011 Beta - во вложении
Тут код, части программы где я пытаюсь поставить условие на координаты при нажатии новой игры или продолжение игры.
Вот скрин того что у меня получается. И я понимаю что можно было бы сделать через массив движение и запоминание смайлика, но я просто начал с координат курсора и хотелось бы понять где ошибка. Чтоб знать что и как на будущее. А потом создам с использованием сохранения смайлика в клетках массива.
Заранее извините за исчерпывающую информацию по моему вопросу. И заранее спасибо!
Листинг программы
- using System;
- using sc = System.Console;
- using scc = System.ConsoleColor;
- namespace Test
- {
- public class Game
- {
- Map map = new Map();
- Player player = new Player();
- CursorPosition cpplayer = new CursorPosition();
- ConsoleKeyInfo keyboard;
- bool startend = true;
- public Game()
- {
- }
- public bool GameCreate(int newcon)
- {
- map.MapCreate();
- sc.ForegroundColor = scc.Green;
- if (newcon == 1) cpplayer.CurPosPlaXY(0);
- if (newcon == 0)
- {
- cpplayer.CurPosPlaXY(11);
- cpplayer.CurPosPlaXY(0);
- }
- sc.Write(player.player011);
- if (newcon == 1) cpplayer.CurPosPlaXY(0);
- if (newcon == 0)
- {
- cpplayer.CurPosPlaXY(11);
- cpplayer.CurPosPlaXY(0);
- }
- while (startend)
- {
- if (sc.KeyAvailable == true)
- {
- keyboard = sc.ReadKey(true);
- if (keyboard.Key == ConsoleKey.Escape) return false;
- player.PlayerStep(keyboard);
- }
- }
- return true;
- }
- public int yscp { get; set; }
- }
- }
Решение задачи: «Как переназначить координаты в консоли (Console.SetCursorPosition(x, y))»
textual
Листинг программы
- using System;
- using sc = System.Console;
- using scc = System.ConsoleColor;
- using st = System.Text;
- namespace Test
- {
- public class Game
- {
- Map map = new Map();
- Player player = new Player();
- CursorPositionPlayer cpplayer = new CursorPositionPlayer();
- char player001 = st.Encoding.GetEncoding(437).GetChars(new byte[] { 1 })[0]; //
- ConsoleKeyInfo keyboard;
- ConsoleKeyInfo playerstep;
- bool startend = true;
- //int cppnc;
- public string player011;
- public Game()
- {
- //cppnc = 11;
- player011 = ((char)player001).ToString(); //
- }
- public bool GameCreate(int newcon)
- {
- map.MapCreate();
- cpplayer.CurPosPlaXY(newcon);
- sc.ForegroundColor = scc.Green;
- sc.Write(player.player011);
- sc.ForegroundColor = scc.Black;
- cpplayer.CurPosPlaXY(newcon);
- while (startend)
- {
- if (sc.KeyAvailable == true)
- {
- keyboard = sc.ReadKey(true);
- if (keyboard.Key == ConsoleKey.Escape) return false;
- playerstep = keyboard;
- if (playerstep.Key == ConsoleKey.RightArrow)
- {
- if ((cpplayer.cppX == 1 & cpplayer.cppY == 0) | (cpplayer.cppX == 1 & cpplayer.cppY == 4) | (cpplayer.cppX == 5 & cpplayer.cppY == 4))
- {
- sc.Write(" ");
- cpplayer.CurPosPlaXY(2); //CursorPosition.cs CursorPositionPlayer()
- sc.ForegroundColor = scc.Green; //Color text(Green)
- sc.Write(player001);
- sc.ForegroundColor = scc.Black; //Color text(Black)
- cpplayer.CurPosPlaXY(10);
- }
- }
- if (playerstep.Key == ConsoleKey.LeftArrow)
- {
- if ((cpplayer.cppX == 5 & cpplayer.cppY == 0) | (cpplayer.cppX == 9 & cpplayer.cppY == 4) | (cpplayer.cppX == 5 & cpplayer.cppY == 4))
- {
- sc.Write(" ");
- cpplayer.CurPosPlaXY(3); //CursorPosition.cs CursorPositionPlayer()
- sc.ForegroundColor = scc.Green; //Color text(Green)
- sc.Write(player001);
- sc.ForegroundColor = scc.Black; //Color text(Black)
- cpplayer.CurPosPlaXY(10);
- }
- }
- if (playerstep.Key == ConsoleKey.DownArrow)
- {
- 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))
- {
- sc.Write(" ");
- cpplayer.CurPosPlaXY(4); //CursorPosition.cs CursorPositionPlayer()
- sc.ForegroundColor = scc.Green; //Color text(Green)
- sc.Write(player001);
- sc.ForegroundColor = scc.Black; //Color text(Black)
- cpplayer.CurPosPlaXY(10);
- }
- }
- if (playerstep.Key == ConsoleKey.UpArrow)
- {
- 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))
- {
- sc.Write(" ");
- cpplayer.CurPosPlaXY(5); //CursorPosition.cs CursorPositionPlayer()
- sc.ForegroundColor = scc.Green; //Color text(Green)
- sc.Write(player001);
- sc.ForegroundColor = scc.Black; //Color text(Black)
- cpplayer.CurPosPlaXY(10);
- }
- }
- }
- }
- return true;
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д