我想做一个有趣的终端图形库,看起来有很多很酷的责任,比如,asciimoo/drawille。
使用\ r \ n可以重绘当前行但是前行怎么样?
例如,我输出了三行(也许不应该使用\n
):
print("00000\n")
print("0 0\n")
print("00000\n")
//output
00000
0 0
00000
在下一帧(例如0.1秒后),我想重绘第二行和第三行。希望结果为:
00000
00 00
00 00
如何在终端中执行此操作?
答案 0 :(得分:-1)
这将根据用户输入的内容设置一个具有不同状态的控制台。现在我只能有能力上升(" w")或下去(" s")
public static void main(String[] args) {
HashMap<Integer, HashMap<String, String>> consoleStates = CreateConsoleStates();
String input = "default";
Scanner userInput = new Scanner(System.in);
do {
//draw default state of board
if(input.equals("default") || input.equals("w") || input.equals("s"))
printConsole(input, consoleStates);
//get input from user until they exit
input = userInput.nextLine().replaceAll("\n", "");
} while (!input.equals("exit"));
}//main method
public static HashMap<Integer, HashMap<String, String>> CreateConsoleStates()
{
HashMap<Integer, HashMap<String, String>> consoleStates = new HashMap<>();
HashMap<String, String> line1, line2, line3;
line1 = new HashMap<>();
line1.put("default", "00000");
line1.put("w", "00000");
line1.put("s", "00 00");
line2 = new HashMap<>();
line2.put("default", "0 0");
line2.put("w", "00 00");
line2.put("s", "00 00");
line3 = new HashMap<>();
line3.put("default", "00000");
line3.put("w", "00 00");
line3.put("s", "00000");
consoleStates.put(1, line1);
consoleStates.put(2, line2);
consoleStates.put(3, line3);
return consoleStates;
}
public static void printConsole(String state, HashMap<Integer, HashMap<String, String>> consoleStates)
{
//adding this will make it seem like a brand new console as suggested in another answer
//System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
//each number corresponds to the line in the console
System.out.println(consoleStates.get(1).get(state));
System.out.println(consoleStates.get(2).get(state));
System.out.println(consoleStates.get(3).get(state));
}
}
输出如下:
00000
0 0
00000
s
00 00
00 00
00000
w
00000
00 00
00 00
e
00000
0 0
00000
exit
注意:我使用大多数游戏都有的标准wsad控件。我推荐它。 w为up,a为left,s为down,d为right。每当按下错误的键并且&#34;退出&#34;用于退出游戏。