为了编写游戏,我需要在终端的不同位置写一些字符.我用了
println!("{c:>width$}", c="*", width=x);
Run Code Online (Sandbox Code Playgroud)
这个x位置几乎没问题,但是y当我按下时我想改变位置space.有什么办法吗?
对于终端控制,我建议使用像Termion这样的箱子.使用Termion,它看起来像:
fn main() {
let mut stdout = stdout().into_raw_mode().unwrap();
writeln!(stdout, "{}Placed at 3,7",
termion::cursor::Goto(3, 7));
}
Run Code Online (Sandbox Code Playgroud)
查看示例.
您可以使用crossterm_cursor来实现此目的,它为您提供了一种处理光标移动和许多其他跨平台内容的方法。
use crossterm::cursor;
let mut cursor = cursor();
/// Moving the cursor
// Set the cursor to position X: 10, Y: 5 in the terminal
cursor.goto(10,5);
// Move the cursor up,right,down,left 3 cells.
cursor.move_up(3);
cursor.move_right(3);
cursor.move_down(3);
cursor.move_left(3);
/// Safe the current cursor position to recall later
// Goto X: 5 Y: 5
cursor.goto(5,5);
// Safe cursor position: X: 5 Y: 5
cursor.save_position();
// Goto X: 5 Y: 20
cursor.goto(5,20);
// Print at X: 5 Y: 20.
print!("Yea!");
// Reset back to X: 5 Y: 5.
cursor.reset_position();
// Print 'Back' at X: 5 Y: 5.
print!("Back");
// hide cursor
cursor.hide();
// show cursor
cursor.show();
// blink or not blinking of the cursor (not widely supported)
cursor.blink(true)
Run Code Online (Sandbox Code Playgroud)
您也可以使用crossterm来执行此操作,但这也将包括非光标相关的功能。另一种可能性是使用命令api。请查看示例以获取有关光标功能的更多信息。
| 归档时间: |
|
| 查看次数: |
1191 次 |
| 最近记录: |