如何使输出更快?

2024-09-28 22:58:36 发布

您现在位置:Python中文网/ 问答频道 /正文

import time
import sys
try:                                                                        
    shell = sys.stdout.shell
except AttributeError:
    raise RuntimeError("you must run this program in IDLE")

Grid=[[0,0,0,0,0,1,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,]]
counterX=0
counterY=0
while (counterX<=10 and counterY<=19):
    if(Grid[counterY-1][counterX-1]==0):
        if (counterX!=10):
            shell.write("⬛")
        else:
            shell.write("⬛\n")
    if(Grid[counterY-1][counterX-1]==1):
        if (counterX!=10):
            print("⬛", end="")
        else:
            print("⬛")
    if(counterX!=10):
        counterX+=1
    else:
        counterX=0
        counterY+=1
i=0
for i in range (0,44):
    print("")
    i+=1

我给自己设置了一个使用ASCII创建GUI游戏的挑战——在这样做的过程中,我会打印出每个游戏状态的游戏网格(我打印它,使它看起来像是取代了上一个游戏状态,而实际上它已经打印了足够多的行,所以看起来是在同一个地方)。我单独打印每个磁贴,因为我需要我的对象用不同的颜色来区分背景和物体。(我还没有添加'if value 1 make the colour red')

不管怎样-当打印出来时,你可以看到它一行一行地打印。速度很快,但很明显。如何一次打印全部内容?你知道吗

新代码将在全屏shell窗口上进行测试


Tags: inimport游戏iftime状态sysshell
1条回答
网友
1楼 · 发布于 2024-09-28 22:58:36

如果从命令行直接使用python而不是IDLE运行代码,那么打印输出可能会更快。如果一次打印整行,而不是一次打印一个字符,则显示速度也会更快。如果您将所有行计算为一个字符串,并使用一个print调用一次打印所有行,则速度可能会更快。你知道吗

相关问题 更多 >