Как я могу сделать анимацию в сценарии оболочки? - PullRequest
1 голос
/ 07 июня 2019

Я недавно начал кодировать и сделал этот код броска монеты. Сейчас я хочу сделать для него анимацию, но понятия не имею, как сделать такую ​​анимацию. У кого-нибудь есть идеи, как мне начать это делать? Какие программы?

Я еще ничего не пробовал, но этот код используется для подбрасывания монеты.

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 ]

3 голосов
/ 07 июня 2019

Один из способов сделать это - поместить все кадры анимации в отдельный текстовый файл (например, см. здесь ). Тогда то, что вы просто делаете:

  • для каждого из кадров:
    • очистить экран и нарисовать текущий кадр

Подробнее см. : https://github.com/hugomd/parrot.live

0 голосов
/ 13 июня 2019

правильный код:

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
0 голосов
/ 08 июня 2019

На основании ответа Вальтера А:

#!/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
0 голосов
/ 07 июня 2019

простая анимация:

#!/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

EDIT:
Если вы хотите удалить монету из выходных данных, добавьте

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

после цикла.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...