你可以使用你的终端作为“窗口”并在其中画一个“圆圈”。作为一个非常简单的(和不可靠的)“计时器”,time.sleep()
函数可用于:
#!/usr/bin/env python
"""Print red circle walking randomly in the terminal."""
import random
import time
from blessings import Terminal # $ pip install blessings colorama
import colorama; colorama.init() # for Windows support (not tested)
directions = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
t = Terminal()
with t.fullscreen(), t.hidden_cursor():
cur_y, cur_x = t.height // 2, t.width // 2 # center of the screen
nsteps = min(cur_y, cur_x)**2 # average distance for random walker: sqrt(N)
for _ in range(nsteps):
y, x = random.choice(directions)
cur_y += y; cur_x += x # update current coordinates
print(t.move(cur_y, cur_x) +
t.bold_red(u'\N{BLACK CIRCLE}')) # draw circle
time.sleep(6 * 0.017) # it may sleep both less and more time
print(t.clear) # clear screen
要尝试它,代码保存到random-walker.py
并运行它:
$ python random-walker.py
我不知道无论它在Windows上工作。
来源
2014-03-01 23:00:33
jfs
你可以发布你的脚本?没有样本讨论就很难谈论你的代码。你想在哪里显示窗口?在设备上?你能列举更详细的步骤吗? –
@MylesBaker python脚本在我的电脑上运行,我需要脚本在我的电脑上显示一个圆圈,我在终端窗口中运行脚本。理想情况下,我想制作一个窗口并绘制它。 – jmasterx
您需要选择一个绘图库。请看这里:http://stackoverflow.com/questions/326300/python-best-library-for-drawing –