ASCII анимация на Python - PullRequest
       63

ASCII анимация на Python

2 голосов
/ 21 марта 2020

Я хочу, чтобы дым вверху двигался бесконечно. Я ищу простую реализацию. Вот мой код:

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 ]

2 голосов
/ 21 марта 2020

Добро пожаловать, newb ie

Ниже приведена возможная реализация без использования какого-либо специализированного пакета.
Однако также обратите внимание на следующие пакеты: curses и asciimatics .

Смотрите и играйте с этим примером в этом онлайн-переводчике .
Вот анимированный 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()
1 голос
/ 21 марта 2020

Вы, вероятно, захотите сделать что-то простое, например, создать while l oop, которое вызывает несколько функций с инструкциями печати, размещающими дым в разных местах, например:

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 голосов
/ 21 марта 2020

Я думаю, что это интересный вопрос, я копаю его. Основываясь на ответе 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')
...