[PYTHON] Simple string animation

It may be common sense, but I will write a simple way to animate with a string. You can make an animation like this.

プゲラ.gif

アヒャヒャヒャ(゚∀゚)ヒャヒャヒャ.gif

I made it with Python 3.6.1, but in principle it can be made even if the language is different. If the environment is such that the carriage return is recognized, it will be displayed as desired.

The mechanism is simple.

  1. Suppress print line breaks
  2. Display the carriage return after displaying the character string

There are only two. After that, if you adjust the display interval like an animation, it will look like that.

Is the simplest one like this?

python


import time
interval = 0.50
print("1     \r", end="")
time.sleep(interval)
print(" 2    \r", end="")
time.sleep(interval)
print("  3   \r", end="")
time.sleep(interval)
print("   4  \r", end="")
time.sleep(interval)
print("    5 \r", end="")
time.sleep(interval)
print("      \r", end="")
time.sleep(interval)

If you make it a little more programmatic, it will look like the following.

python


import time

def animation(sts, times=3, interval=0.25):
  for n in range(times):
    for st in sts:
      print(st, end="")
      time.sleep(interval)
      print("\r", end="")
  print()

sts = [
    "(・ ∀ ・)",
    "(・ ∀ ・) Nino",
    "(・ ∀ ・) Nino Nino",
    "(・ ∀ ・)",
    "(・ ∀ ・) Hi",
    "(・ ∀ ・) Pugera",
]

times = 3
interval = 0.25
animation(sts, times, interval)

Recommended Posts

Simple string animation
String format
Try drawing a simple animation in Python
String format 2
String summary 1
raw string
Python string
"Parentheses character string" Simple parser implementation example summary