所以我当时正在制作一个Python Multiplayer Ascii游戏,我需要清除游戏框架,但是当我想使用os.system('cls')
时,它什么也没做。
import socket,time
import subprocess as sp
import keyboard,os
def Main(k):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("127.0.0.1",8000))
while True:
for Key,Command in k.items():
if keyboard.is_pressed(Key):
exec(Command)
data = s.recv(2048).decode()
data = data.replace("PlaceHolder","")
lastdata = data
s.send("-".encode())
if not data == "":
print(data+"\n"*2)
elif data != "" and lastdata == "":
sp.call('cls')
if __name__ == '__main__':
keydict = {"w":'s.send("w".encode())',
"d":'s.send("d".encode())',
"a":'s.send("a".encode())',
"s":'s.send("s".encode())',
"esc":'exit()'}
Main(keydict)
答案 0 :(得分:0)
我在我的代码中犯了错误,并纠正了这些错误。 旧的:
if not data == "":
print(data+"\n"*2)
elif data != "" and lastdata == "":
sp.call('cls')
新功能:
if not data == "":
os.system("CLS")
print(data+"\n"*2)
time.sleep(0.05)
所以我删除了无用的if语句,因为它只是没有触发os.system(“ CLS”)