【问题标题】:ASCII animation on PythonPython 上的 ASCII 动画
【发布时间】:2020-03-20 23:59:22
【问题描述】:

我希望顶部的烟雾无限移动。我正在寻找一个简单的实现。这是我的代码:

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)

【问题讨论】:

  • 如果你想要那种动画,保持简单是不可能的——你可能不得不使用像curses这样的模块来完全控制终端。
  • 使用 ANSI 转义码清除屏幕:stackoverflow.com/questions/37774983/… 和计时器和循环
  • @Richard:这可能会干扰用户输入回显,并导致尴尬的闪烁。
  • @user2357112supportsMonica:可能吧,虽然我认为有更具体的代码来操作单个字符。

标签: python ascii-art


【解决方案1】:

欢迎新人

在不使用任何专用包的可能实现之下。
不过,也请查看以下软件包:cursesasciimatics

online interpreter 中查看并使用此示例。
这是animated gif

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

# System independent clear screen function
# https://stackoverflow.com/questions/18937058/#42877403
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...,但是当代码启动时,房子不断向下移动,而不是烟雾左右移动,它有可能上下移动吗?谢谢
  • 正在工作。我在在线编译器中再次尝试,并且烟雾在移动。我还检查了我的 Mac。我将添加一个视频。也许您的系统无法正常工作的清晰屏幕。尝试让代码在您的系统中运行,然后使用随机数改善烟雾运动。
  • 我更新了代码以使用与系统无关的另一种方法清除屏幕
  • 谢谢你!它就像一个魅力!还有一件事,在此之后我有很多代码,但该代码没有运行。这是注定要发生的吗?如果是的话,有什么办法可以解决吗?
  • 尝试找出你的问题(可能你很少),然后在单独的问题中发布。看这里如何问stackoverflow.com/help/how-to-ask>。
【解决方案2】:

您可能只想做一些简单的事情,例如创建一个调用多个函数的 while 循环,其中 print 语句将烟雾放置在不同的位置,例如:

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)

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

【讨论】:

    【解决方案3】:

    我认为这是一个有趣的问题,我很喜欢。根据 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')
    

    【讨论】:

    • 谢谢布拉克,但房子只是重复的,每个房子都不一样。这是注定要发生的吗?
    • Hi Student Coder 没问题!我稍微修改了代码。继续并在 Windows 控制台中运行它,让我知道这是否是您正在寻找的。 (如果您使用的是 Mac,则需要进行一些调整)。
    • 嗨 Student.Coder,我认为你应该试一试我的代码。烟雾上升和下降,这是我认为你正在寻找的。在多次迭代中打印房屋并按照 murky123 的描述清除它们,这就是我在这里所做的。为了便于理解,我刚刚把代码写得比较长
    猜你喜欢
    • 1970-01-01
    • 2022-06-21
    • 2011-11-19
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    相关资源
    最近更新 更多