首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Python 3.x在控制台应用程序中创建ASCII动画?

如何使用Python 3.x在控制台应用程序中创建ASCII动画?
EN

Stack Overflow用户
提问于 2010-04-28 09:49:41
回答 5查看 22.7K关注 0票数 9

我想把这个问题移植到Python (Windows + Linux + Mac Os)上

How to create ASCII animation in Windows Console application using C#?

谢谢!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-05-07 09:13:59

我刚刚将我的示例从我的答案here移植到了python。您将需要从here安装pyglet库,因为不幸的是python没有内置的动画gif支持。希望您喜欢:)

代码语言:javascript
运行
复制
import pyglet, sys, os, time

def animgif_to_ASCII_animation(animated_gif_path):
    # map greyscale to characters
    chars = ('#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ')
    clear_console = 'clear' if os.name == 'posix' else 'CLS'

    # load image
    anim = pyglet.image.load_animation(animated_gif_path)

    # Step through forever, frame by frame
    while True:
        for frame in anim.frames:

            # Gets a list of luminance ('L') values of the current frame
            data = frame.image.get_data('L', frame.image.width)

            # Built up the string, by translating luminance values to characters
            outstr = ''
            for (i, pixel) in enumerate(data):
                outstr += chars[(ord(pixel) * (len(chars) - 1)) / 255] + \
                          ('\n' if (i + 1) % frame.image.width == 0 else '')

            # Clear the console
            os.system(clear_console)

            # Write the current frame on stdout and sleep
            sys.stdout.write(outstr)
            sys.stdout.flush()
            time.sleep(0.1)

# run the animation based on some animated gif
animgif_to_ASCII_animation(u'C:\\some_animated_gif.gif')
票数 12
EN

Stack Overflow用户

发布于 2015-09-04 07:11:48

这正是我创建asciimatics所针对的应用程序类型。

它是一个跨平台的控制台API,支持从丰富的文本效果集生成动画场景。它已经被证明可以在各种风格的CentOS、Windows和OSX上运行。

可以从gallery获得可能的示例。这是一个类似于其他答案中提供的动画GIF代码的示例。

我假设你只是在寻找一种方法来制作任何动画,但如果你真的想复制蒸汽火车,你可以将它转换为Sprite,并为其提供一条在屏幕上运行它的路径,然后将其作为场景的一部分播放。这些对象的完整解释可以在docs中找到。

票数 8
EN

Stack Overflow用户

发布于 2010-05-07 14:39:15

简单的控制台动画,在Ubuntu的python3上测试过。addch()不喜欢这个非ascii字符,但它可以在addstr()中工作。

代码语言:javascript
运行
复制
#this comment is needed in windows:
#  encoding=latin-1
def curses(win):
    from curses import use_default_colors, napms, curs_set
    use_default_colors()
    win.border()
    curs_set(0)

    row, col = win.getmaxyx()
    anim = '.-+^°*'
    y = int(row / 2)
    x = int((col - len(anim))/2)
    while True:
        for i in range(6):
            win.addstr(y, x+i, anim[i:i+1])
            win.refresh()
            napms(100)
            win.addch(y, x+i, ' ')

if __name__ == "__main__":
    from curses import wrapper
    wrapper(curses)

@Philip Daubmeier:我已经在Windoze下测试过了,它不能工作:(.接下来有三个基本选项:(请选择)

为python (http://bugs.python.org/msg94309)

  • Abandon curses安装第三方-curses库(http://adamv.com/dev/python/curses/)
  1. Apply windows-curses补丁。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2726343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档