如何在Shell脚本中制作动画?

时间:2019-06-07 08:36:15

标签: bash

我最近开始编码并制作了该硬币翻转码。现在我想为其制作动画,但是我不知道如何制作这样的动画。有谁知道我如何开始做这个的?什么程序?

我还没有尝试过任何东西,但这是用于掷硬币的代码。

printf "(H) heads or (T) tails"
read user_choice
if [ $user_choice != H ] && [ $user_choice != T ]; then
  echo invalid choice defaulting to heads
 $user_choice=H=1
 $user_choice=T=2
fi
#value of 1 is heads, 2 is Tails
computer_choice=$(($RANDOM% 2 + 1))
if [ $computer_choice == 1 ]; then
  echo computer chooses heads
elif [ $computer_choice == 2 ]; then
echo computer chooses tails
fi
if [ $computer_choice == 1 ] && [ $user_choice = H ]; then
  echo you win!
else
if [ $computer_choice == 2 ] && [ $user_choice = T ]; then
    echo you win!
  else
    echo you lose
  fi
fi

4 个答案:

答案 0 :(得分:3)

一种实现方法是将动画的所有帧都放在单独的文本文件中(有关示例,请参见here)。 然后,您只需做的是:

  • 对于每个框架:
    • 清除屏幕并绘制当前框架

有关更多详细信息,请参见thishttps://github.com/hugomd/parrot.live

答案 1 :(得分:0)

简单动画:

#!/bin/bash

for ((i=0;i<10;i++)); do
   printf "\r  |  "
   sleep 0.2
   printf "\r ( )  "
   sleep 0.2
   printf "\r( H )"
   sleep 0.2
   printf "\r ( )  "
   sleep 0.2
   printf "\r  |  "
   sleep 0.2
   printf "\r ( )  "
   sleep 0.2
   printf "\r( T ) "
   sleep 0.2
   printf "\r ( )  "
   sleep 0.2
done

编辑:
如果要在完成后从输出中删除硬币,请添加

printf "\r    \r"

在循环之后。
如果您想随机完成T或H,请添加

   if (( $RANDOM % 2 )); then
      printf "\r( H )\n"
      computer_choice=1
   else
      printf "\r( T )\n"
      computer_choice=2
   fi

循环之后。

答案 2 :(得分:0)

基于Walter A的回答:

#!/bin/bash

# begin config
minsteps=12
maxsteps=20
sleepytime=0.2
# end config

frames=(
    '  |  '
    ' ( ) '
    '( S )'
 )

sides=(H T)

side=$(($RANDOM % 2))

read -r -p "(H) heads or (T) tails" user_choice

user_choice=${user_choice^^}    # upper case the input (not available in Bash 3.2)

if [[ $user_choice == H ]]
then
    user_choice=0
elif [[ $user_choice == T ]]
then
    user_choice=1
else
    printf '%s\n' "invalid choice, defaulting to heads"
    user_choice=0
fi

for (( step = 0; step < maxsteps; step++ ))
do
    for (( frame = 0; frame < 3; frame++ ))
    do
        if (( frame == 2 ))
        then
            f=${frames[frame]/S/${sides[side]}}    # swap H or T for S (template char)
            computer_choice=$side
            (( side ^= 1 ))    # toggle H/T
        else
            f=${frames[frame]}
        fi
        printf '\r%s' "$f"
        # if showing a side and some flips have taken place and a 50% chance
        if (( frame == 2 && step > minsteps && RANDOM > 16383 ))
        then
            break 2    # exit both loops, we're done
        fi
        sleep "$sleepytime"
    done
done

printf '\n'

if [[ $user_choice == $computer_choice ]]
then
  printf '%s\n' "you win!"
else
  printf '%s\n' "you lose"
fi

答案 3 :(得分:0)

正确的代码:

printf "(H) heads or (T) tails"
read user_choice
if [ $user_choice != H ] && [ $user_choice != T ]; then
  echo invalid choice defaulting to heads
 $user_choice=H
 $user_choice=T
fi
#value of 1 is heads, 2 is Tails
# begin config
minsteps=6
maxsteps=10
sleepytime=0.125
# end config

frames=(
    '  |  '
    ' ( ) '
    '( S )'
 )

sides=(H T)

side=$(($RANDOM% 2 ))

for (( step = 0; step < maxsteps; step++ ))
do
   for (( frame = 0; frame < 3; frame++ ))
    do
        if (( frame == 2 ))
        then
            f=${frames[frame]/S/${sides[side]}}    # swap H or T for S (templat$
            (( side ^= 1 ))    # toggle H/T
        else
            f=${frames[frame]/S/${sides[side]}}
            (( side ^= 2 ))    # toggle H/T
        fi
        printf '\r%s' "$f"
        # if showing a side and some flips have taken place and a 50% chance
        if (( frame == 2 && step > minsteps && RANDOM > 16383 ))
        then
            break 2    # exit both loops, we're done
        fi
        sleep "$sleepytime"
    done
done

printf '\n'
if [ ${sides[side]} == T ] && [ $user_choice = H ]; then
  echo you win!
else
if [ ${sides[side]} == H ] && [ $user_choice = T ]; then
    echo you win!
  else
    echo you lose
  fi
fi

printf  "would you like to play again? (Y) or (N)"
read Y_N
if [ $Y_N == Y ]; then
exec bash coin.sh
else
echo ok good bye
fi