Python中的ASCII动画

3

我希望页面顶部的烟雾可以无限移动,想要一个简单实现方法。这是我的代码:

def welcome():

    print("               (")
    print("                 )")
    print("               (")
    print("                _)")
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print("")

    time.sleep(1)

3
如果你想要那种类型的动画,简单地实现是不太可能的——你可能需要使用像 curses 这样的模块来完全控制终端。 - user2357112
使用ANSI转义码清除屏幕:https://dev59.com/ZFoU5IYBdhLWcg3wI0hM#37778152,以及计时器和循环。 - Richard
1
@Richard:这可能会干扰用户输入的回显,并导致尴尬的闪烁。 - user2357112
@user2357112supportsMonica:可能是这样,不过我认为还有更具体的代码来操作单个字符。 - Richard
3个回答

4

欢迎新手

以下是一种不使用任何专业软件包的可能实现方法。
但是,也可以查看这些软件包:cursesasciimatics

在这个在线解释器中查看并尝试此示例。
这里有一个动画 GIF

import time
import platform    # Used by clear_screen
import subprocess  # Used by clear_screen

# System independent clear screen function
# https://dev59.com/UmMk5IYBdhLWcg3w0hI-
def clear_screen():
    command = "cls" if platform.system().lower()=="windows" else "clear"
    return subprocess.call(command) == 0

def smoke():
    # You could use the random package for a more realistic effect
    # https://docs.python.org/3/library/random.html

    shift = 15 + smoke.shift
    print(" "*(shift+2)+"(")
    print(" "*(shift  )+")")
    print(" "*(shift+2)+"(")
    print(" "*(shift  )+")")

    # Next shift using current direction
    smoke.shift += smoke.direction

    # Change direction if out of limits
    if smoke.shift>3 or smoke.shift<-2:
        smoke.direction *= -1

def house():
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print()

# MAIN CODE

smoke.shift = 0
smoke.direction = 1 # could be 1 or -1


# print('\033[2J') # One possible method to clear the screen
clear_screen()

# Infinite loop. Use CTR-C to stop
while True:   
    smoke()
    house()
    time.sleep(1)
    clear_screen()

谢谢ePi27...,但是当代码开始时,房子不断向下移动,而烟雾是否可以上下移动而不是左右移动?谢谢。 - Student.Coder
正在运行。我在在线编译器中再次尝试,烟雾开始移动了。我还在我的Mac上进行了检查。我将添加一个视频。也许在你的系统中清除屏幕不起作用。 尝试在您的系统中运行代码,然后使用随机数改进烟雾运动。 - ePi272314
我更新了代码,使用另一种与系统无关的方法来清除屏幕。 - ePi272314
谢谢,老兄!它像魔法一样运行!还有一件事,我在这之后有很多代码,但那些代码没有运行。这是有意为之吗?如果是,有什么办法可以修复吗? - Student.Coder
尝试识别您的问题(可能只有几个),并将它们分别发布在不同的问题中。请参阅此处的提问指南https://stackoverflow.com/help/how-to-ask。 - ePi272314
还有一件事,请考虑接受我的答案作为您问题的正确答案。您应该点击复选标记。 - ePi272314

1
你可能想要做的事情很简单,比如创建一个while循环并调用多个函数,使用打印语句将烟雾放置在不同的位置,例如:
def welcome2():


print("                 (")
print("               )")
print("                 (")
print("               _)")
print("     __________| |____")
print("    /                 \\")
print("   /     Welcome to    \\")
print("  /     A Horror Game   \\")
print("  |    By: A.D & T.P    |")
print("  |     ____     ___    |")
print("  |    |    |   |___|   |")
print("__|____|____|___________|__")
print("")
time.sleep(1)

或类似的东西。如果您重复调用多个函数,看起来就像是烟雾在“移动”。不过我不确定您从哪里调用这个欢迎函数。


0

我认为这是一个有趣的问题,我很喜欢它。在ePi272314的回答基础上,你可以尝试以下另一种酷炫的烟雾效果。希望能对你有所帮助!

import time
import os
from os import system, name

# define our clear function
def clear():
    os.system( 'cls' )

def welcome():

    smoke = ['               (_)','               ()', '                ()','               ()', '                ()']
    print("\n"*4)
    print("                _     ")
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print("")
    time.sleep(.6)
    clear()

    print("\n"*5)
    print (smoke[0])
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print("")
    time.sleep(.6)
    clear()

    print("\n"*4)
    print (smoke[1])
    print (smoke[0])
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print("")
    time.sleep(.6)
    clear()

    print("\n"*3)
    print (smoke[2])
    print (smoke[1])
    print (smoke[0])
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print("")
    time.sleep(.6)
    clear()

    print("\n"*2)
    print (smoke[3])
    print (smoke[2])
    print (smoke[1])
    print (smoke[0])
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print("")
    time.sleep(.6)
    clear()

    print("\n"*1)
    print (smoke[4])
    print (smoke[3])
    print (smoke[2])
    print (smoke[1])
    print (smoke[0])
    print("     __________| |____")
    print("    /                 \\")
    print("   /     Welcome to    \\")
    print("  /     A Horror Game   \\")
    print("  |    By: A.D & T.P    |")
    print("  |     ____     ___    |")
    print("  |    |    |   |___|   |")
    print("__|____|____|___________|__")
    print("")
    time.sleep(.6)
    clear()




while True:
    welcome()
    print('\033[2J')

谢谢Brack,但是房子只是复制了,每个房子都不同。这是有意为之吗? - Student.Coder
同学,没问题!我稍微修改了一下代码。请在 Windows 控制台中运行它,看看是否符合你的要求。(如果你使用的是 Mac,则需要进行一些调整。) - B-L
嗨,Student.Coder,我认为你应该试试我的代码。烟雾上下浮动正是你想要的效果。在多次迭代中打印房屋并像murky123所描述的那样清除它们,这就是我在这里所做的。我只是将代码写成长格式,以便更易于理解。 - B-L

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接