1 回答

TA貢獻1853條經驗 獲得超18個贊
有兩種方法可以做到這一點。一種是使用win32console and win32con
,另一種是使用 ANSI 轉義序列。實際上,您要做的就是將光標移動到控制臺上的 0,0 處/進行打印。這是方法一:
import win32console, win32con, time
myConsole = win32console.CreateConsoleScreenBuffer(DesiredAccess = win32con.GENERIC_READ | win32con.GENERIC_WRITE, ShareMode=0, SecurityAttributes=None, Flags=1) # create screen buffer
myConsole.SetConsoleActiveScreenBuffer() # set this buffer to be active
myConsole.WriteConsole("Hello World!") # Effectively the print func.
myConsole.WriteConsoleOutputCharacter(Characters="Hello World!\0", WriteCoord=win32console.PyCOORDType(5,5)) # Print at coordinates
time.sleep(3) # this must be called or you won't see results. This is because after running the code (because there is no loop) the cmd.exe takes the console over.
這應該可以在您的控制臺 FPS 中正常工作(使用 WriteConsoleOutputCharacter)。只需將命令放在 javid 放置的位置即可,這樣就可以正常工作。第二種方法是使用 colorama/ANSI 序列將光標返回到起始位置。這種方法可以概括為:os.system("echo \033[0;0H")。您不能使用該print函數使用轉義序列,它們不受支持(使用colorama除外)。這是一個例子:
from colorama import init
from os import system
init() # I bother with this because I use colors too. Echo prints slower than print, so don't use echo if you can avoid it.
# ... game
# ... generate string to print
print(frame) # or just use you method of printing each line.
system("echo \033[0;0H") # return cursor to home position, ready for next frame.
添加回答
舉報