Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Animation 如何使用Python3.x在控制台应用程序中创建ASCII动画?_Animation_Python 3.x_Console Application_Ascii Art - Fatal编程技术网

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

Animation 如何使用Python3.x在控制台应用程序中创建ASCII动画?,animation,python-3.x,console-application,ascii-art,Animation,Python 3.x,Console Application,Ascii Art,我想把这个问题转移到Python(Windows+Linux+Mac Os)上 谢谢大家! Colorama:我刚刚将我的示例和动画gif从python的答案移植到ASCII动画。您需要从安装pyglet库,因为python没有内置的动画gif支持。希望你喜欢:) 简单的控制台动画,在Ubuntu的python3上测试。 addch()不喜欢那个非ascii字符,但它在addstr()中工作 @Philip Daubmeier:我在Windoze下测试过这个,但它不起作用:(.未来有三个基本

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


谢谢大家!

Colorama:

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


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


@Philip Daubmeier:我在Windoze下测试过这个,但它不起作用:(.未来有三个基本选项:(请选择)

  • 安装第三方windows curses库()
  • 将windows curses修补程序应用于python()
  • 为了别的东西而完全抛弃诅咒

  • 这正是我为之创建的应用程序类型

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

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


    我想你只是在寻找一种制作任何动画的方法,但是如果你真的想复制蒸汽火车,你可以把它转换成一个精灵,给它一个在屏幕上运行的路径,然后把它作为场景的一部分来播放。关于对象的完整解释可以在中找到。

    嗯,我成功地移植了Philip Daubmeier的solu主要问题是ord函数,需要忽略它,因为Python3-bytestring索引直接返回ASCII值,而不是该位置的字符(请参阅和..)。 我创建了一个Git回购协议,可以随意贡献(需要更好的性能->邀请pm):


    repo

    没有用Python3.x测试它,我的电脑上只有2.6。如果有人能在3.x上测试它,那就太好了。我在Python3.5.2上试过这个,但不幸的是,编译器声明有这样一个错误:TypeError:ord()需要长度为1的字符串,但找到了int。SO中的一些答案表示ord()函数应该被删除。但是当你这么做的时候,它也从同一行中断了:TypeError:元组索引必须是整数或切片,而不是浮点。所以我想我也需要有人来测试一下:)-1发布一个链接来回答一个100悬赏的问题。你安装了pyglet吗?显示了哪个错误消息?我在windows上用Python2.6和
    cmd
    控制台对它进行了测试,效果很好。顺便说一句:你不必像我在回答中看到的那样使用诅咒。
    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')
    
    #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)